Maven的构建配置文件(Build Profiles)
注意:這里的構(gòu)建配置文件并不是一個(gè)真正的文件,而是通過(guò)指定參數(shù)來(lái)做特定的事。
以下內(nèi)容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_build_profiles.html:
當(dāng)時(shí)此教程的例子是在2.0版本,而新的3.0版本只是增加了一點(diǎn),具體可以參考官網(wǎng)http://maven.apache.org/guides/introduction/introduction-to-profiles.html
什么是構(gòu)建配置文件?
構(gòu)建配置文件(A Build profile)?是一系列的配置項(xiàng)的值,可以用來(lái)設(shè)置或者覆蓋Maven構(gòu)建默認(rèn)值。使用構(gòu)建配置文件,你可以為不同的環(huán)境,比如說(shuō)生產(chǎn)環(huán)境(Producation)和開(kāi)發(fā)(Development)環(huán)境,定制構(gòu)建方式。
配置文件在pom.xml文件中使用activeProfiles或者profiles元素指定,并且可以通過(guò)各種方式觸發(fā)。配置文件在構(gòu)建時(shí)修改POM,并且用來(lái)給參數(shù)設(shè)定不同的目標(biāo)環(huán)境(比如說(shuō),開(kāi)發(fā)(Development)、測(cè)試(Testing)和生產(chǎn)環(huán)境(Producation)中數(shù)據(jù)庫(kù)服務(wù)器的地址)。
構(gòu)建配置文件的類型
構(gòu)建配置文件大體上有三種類型
| 項(xiàng)目級(jí)(Per Project) | 定義在項(xiàng)目的POM文件pom.xml中 |
| 用戶級(jí) (Per User) | 定義在Maven的設(shè)置xml文件中 (%USER_HOME%/.m2/settings.xml) |
| 全局(Global) | 定義在Maven全局的設(shè)置xml文件中 (%M2_HOME%/conf/settings.xml) |
配置文件激活
Maven的構(gòu)建配置文件可以通過(guò)多種方式激活。
- 使用命令控制臺(tái)輸入顯式激活。
- 通過(guò)maven設(shè)置。
- 基于環(huán)境變量(用戶或者系統(tǒng)變量)。
- 操作系統(tǒng)設(shè)置(比如說(shuō),Windows系列)。
- 文件的存在或者缺失。
官方配置文件激活示例
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
http://maven.apache.org/ref/2.2.1/maven-profile/profiles.html
實(shí)踐配置文件激活示例
新建的項(xiàng)目結(jié)構(gòu)如下:
其中在src/main/resources文件夾下有三個(gè)用于測(cè)試文件:
| env.properties | 如果未指定配置文件時(shí)默認(rèn)使用的配置。 |
| env.test.properties | 當(dāng)測(cè)試配置文件使用時(shí)的測(cè)試配置。 |
| env.prod.properties | 當(dāng)生產(chǎn)配置文件使用時(shí)的生產(chǎn)配置。 |
注意:這三個(gè)配置文件并不是代表構(gòu)建配置文件的功能,而是用于本次測(cè)試的目的;比如,我指定了構(gòu)建配置文件為prod時(shí),項(xiàng)目就使用envprod.properties文件。
注意:下面的例子仍然是使用AntRun插件,因?yàn)榇瞬寮芙壎∕aven生命周期階段,并通過(guò)Ant的標(biāo)簽不用編寫一點(diǎn)代碼即可輸出信息、復(fù)制文件等,經(jīng)此而已。其余的與本次構(gòu)建配置文件無(wú)關(guān)。
1、顯示配置文件激活
pom.xml配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jsoft.test</groupId><artifactId>testproject</artifactId><packaging>jar</packaging><version>0.1-SNAPSHOT</version><name>testproject</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><profiles><profile><id>test</id><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.8</version><executions><execution><phase>test</phase><goals><goal>run</goal></goals><configuration><tasks><echo>Using env.test.properties</echo><copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/></tasks></configuration></execution></executions></plugin></plugins></build></profile><profile><id>normal</id><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.8</version><executions><execution><phase>test</phase><goals><goal>run</goal></goals><configuration><tasks><echo>Using env.properties</echo><copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/></tasks></configuration></execution></executions></plugin></plugins></build></profile><profile><id>prod</id><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.8</version><executions><execution><phase>test</phase><goals><goal>run</goal></goals><configuration><tasks><echo>Using env.prod.properties</echo><copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/></tasks></configuration></execution></executions></plugin></plugins></build></profile></profiles> </project>注意:構(gòu)建配置文件采用的是<profiles>節(jié)點(diǎn)。
說(shuō)明:上面新建了三個(gè)<profiles>,其中<id>區(qū)分了不同的<profiles>執(zhí)行不同的AntRun任務(wù);而AntRun的任務(wù)可以這么理解,AntRun監(jiān)聽(tīng)test的Maven生命周期階段,當(dāng)Maven執(zhí)行test時(shí),就除了發(fā)AntRun的任務(wù),任務(wù)里面為輸出文本并復(fù)制文件到指定的位置;而至于要執(zhí)行哪個(gè)AntRun任務(wù),此時(shí)構(gòu)建配置文件起到了傳輸指定的作用,比如,通過(guò)命令行參數(shù)輸入指定的<id>。
執(zhí)行命令:
mvn test -Ptest提示:第一個(gè)test為Maven生命周期階段,第2個(gè)test為為構(gòu)建配置文件指定的<id>參數(shù),這個(gè)參數(shù)通過(guò)-P來(lái)傳輸,當(dāng)然,它可以是prod或者normal這些由你定義的<id>。
運(yùn)行的結(jié)果如下:
可以看出成功的觸發(fā)了AntRun的任務(wù)。并且是對(duì)應(yīng)構(gòu)建配置文件下的<id>為test的任務(wù)。
再測(cè)試其余兩個(gè)命令,結(jié)果如下:
2、通過(guò)Maven設(shè)置激活配置文件
打開(kāi)%USER_HOME%/.m2目錄下的settings.xml文件,其中%USER_HOME%代表用戶主目錄。如果setting.xml文件不存在就直接拷貝%M2_HOME%/conf/settings.xml到.m2目錄,其中%M2_HOME%代表Maven的安裝目錄。對(duì)于為什么可以這樣做,參考:http://www.cnblogs.com/EasonJim/p/6827058.html
配置setting.xml文件,增加<activeProfiles>屬性:
<settings xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/settings-1.0.0.xsd">...<activeProfiles><activeProfile>test</activeProfile></activeProfiles> </settings>執(zhí)行命令:
mvn test提示:此時(shí)不需要使用-Ptest來(lái)輸入?yún)?shù)了,上面的setting.xml文件的<activeprofile>已經(jīng)指定了test參數(shù),代替了。
提示2:同樣可以使用在%M2_HOME%/conf/settings.xml的文件進(jìn)行配置,效果一致。
執(zhí)行結(jié)果:
3、通過(guò)環(huán)境變量激活配置文件
先把上一步測(cè)試的setting.xml值全部去掉。
然后在pom.xml里面的<id>為test的<profile>節(jié)點(diǎn),加入<activation>節(jié)點(diǎn):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jsoft.test</groupId><artifactId>testproject</artifactId><packaging>jar</packaging><version>0.1-SNAPSHOT</version><name>testproject</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><profiles><profile><id>test</id><activation><property><name>env</name><value>test</value></property></activation><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.8</version><executions><execution><phase>test</phase><goals><goal>run</goal></goals><configuration><tasks><echo>Using env.test.properties</echo><copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/></tasks></configuration></execution></executions></plugin></plugins></build></profile><profile><id>normal</id><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.8</version><executions><execution><phase>test</phase><goals><goal>run</goal></goals><configuration><tasks><echo>Using env.properties</echo><copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/></tasks></configuration></execution></executions></plugin></plugins></build></profile><profile><id>prod</id><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.8</version><executions><execution><phase>test</phase><goals><goal>run</goal></goals><configuration><tasks><echo>Using env.prod.properties</echo><copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/></tasks></configuration></execution></executions></plugin></plugins></build></profile></profiles> </project>執(zhí)行命令:
mvn test -Denv=test提示:上面使用-D傳遞環(huán)境變量,其中evn對(duì)應(yīng)剛才設(shè)置的<name>值,test對(duì)應(yīng)<value>。
提示2:在Windows 10上測(cè)試了系統(tǒng)的環(huán)境變量,但是不生效,所以,只能通過(guò)-D傳遞。
執(zhí)行結(jié)果:
4、通過(guò)操作系統(tǒng)激活配置文件
5、通過(guò)文件的存在或者缺失激活配置文件
6、通過(guò)JDK的版本激活配置文件
...
更多激活配置,可以參考官方的例子:http://maven.apache.org/guides/introduction/introduction-to-profiles.html
?
測(cè)試工程:https://github.com/easonjim/5_java_example/tree/master/maventest/test4/test4/testproject
==>如有問(wèn)題,請(qǐng)聯(lián)系我:easonjim#163.com,或者下方發(fā)表評(píng)論。<==總結(jié)
以上是生活随笔為你收集整理的Maven的构建配置文件(Build Profiles)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 十一周进度
- 下一篇: 微信客服推送信息接口报错 45015