Maven 構建配置檔案
構建配置檔案是一系列的配置項的值,可以用來設定或者覆蓋 Maven 構建預設值。
使用構建配置檔案,你可以為不同的環境,比如說生產環境(Production)和開發(Development)環境,定製構建方式。
配置檔案在 pom.xml 檔案中使用 activeProfiles 或者 profiles 元素指定,並且可以通過各種方式觸發。配置檔案在構建時修改 POM,並且用來給引數設定不同的目標環境(比如說,開發(Development)、測試(Testing)和生產環境(Production)中資料庫伺服器的地址)。
構建配置檔案的型別
構建配置檔案大體上有三種型別:
型別 |
在哪定義 |
專案級(Per Project) |
定義在專案的POM檔案pom.xml中 |
使用者級 (Per User) |
定義在Maven的設定xml檔案中 (%USER_HOME%/.m2/settings.xml) |
全域性(Global) |
定義在 Maven 全域性的設定 xml 檔案中 (%M2_HOME%/conf/settings.xml) |
配置檔案啟用
Maven的構建配置檔案可以通過多種方式啟用。
- 使用命令控制檯輸入顯式啟用。
- 通過 maven 設定。
- 基於環境變數(使用者或者系統變數)。
- 作業系統設定(比如說,Windows系列)。
- 檔案的存在或者缺失。
配置檔案啟用例項
假定專案結構如下:

其中在src/main/resources資料夾下有三個用於測試檔案:
檔名 |
描述 |
env.properties |
如果未指定配置檔案時預設使用的配置。 |
env.test.properties |
當測試配置檔案使用時的測試配置。 |
env.prod.properties |
當生產配置檔案使用時的生產配置。 |
注意:這三個配置檔案並不是代表構建配置檔案的功能,而是用於本次測試的目的;比如,我指定了構建配置檔案為 prod 時,專案就使用 env.prod.properties檔案。
注意:下面的例子仍然是使用 AntRun 外掛,因為此外掛能繫結 Maven 生命週期階段,並通過 Ant 的標籤不用編寫一點程式碼即可輸出資訊、複製檔案等,經此而已。其餘的與本次構建配置檔案無關。
1、配置檔案啟用
profile 可以讓我們定義一系列的配置資訊,然後指定其啟用條件。這樣我們就可以定義多個 profile,然後每個 profile 對應不同的啟用條件和配置資訊,從而達到不同環境使用不同配置資訊的效果。
以下例項,我們將 maven-antrun-plugin:run 目標新增到測試階段中。這樣我們可以在不同的 profile 中輸出文字資訊。我們將使用 pom.xml 來定義不同的 profile,並在命令控制檯中使用 maven 命令啟用 profile。
pom.xml 檔案如下:
<
project
xmlns
=
"
"
xmlns:xsi
=
"
"
xsi:schemaLocation
=
"
"
>
<
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
>
</
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
>
注意:
構建配置檔案採用的是
<profiles> 節點。
說明:上面新建了三個
<profiles>,其中
<id> 區分了不同的
<profiles> 執行不同的 AntRun 任務;而 AntRun 的任務可以這麼理解,AntRun 監聽 test 的 Maven 生命週期階段,當 Maven 執行 test 時,就觸發了 AntRun 的任務,任務裡面為輸出文字並複製檔案到指定的位置;而至於要執行哪個 AntRun 任務,此時
構建配置檔案起到了傳輸指定的作用,比如,通過命令列引數輸入指定的
<id>。
執行命令:
mvn test -Ptest
提示:第一個 test 為 Maven 生命週期階段,第 2 個 test 為
構建配置檔案指定的 <id> 引數,這個引數通過
-P 來傳輸,當然,它可以是 prod 或者 normal 這些由你定義的
<id>。
執行的結果如下:

可以看出成功的觸發了AntRun的任務。並且是對應
構建配置檔案下的 <id> 為 test 的任務。
再測試其餘兩個命令,結果如下:


2、通過Maven設定啟用配置檔案
開啟
%USER_HOME%/.m2 目錄下的
settings.xml 檔案,其中
%USER_HOME% 代表使用者主目錄。如果 setting.xml 檔案不存在就直接拷貝
%M2_HOME%/conf/settings.xml 到 .m2 目錄,其中
%M2_HOME% 代表 Maven 的安裝目錄。
配置 setting.xml 檔案,增加
<activeProfiles>屬性:
<
settings
xmlns
=
"
"
xmlns:xsi
=
"
"
xsi:schemaLocation
=
"
/xsd/settings-1.0.0.xsd
"
>
...
<
activeProfiles
>
<
activeProfile
>
test
</
activeProfile
>
</
activeProfiles
>
</
settings
>
執行命令:
mvn test
提示 1:此時不需要使用 -Ptest 來輸入引數了,上面的 setting.xml 檔案的 <activeprofile> 已經指定了 test 引數代替了。
提示 2:同樣可以使用在
%M2_HOME%/conf/settings.xml 的檔案進行配置,效果一致。
執行結果:

3、通過環境變數啟用配置檔案
先把上一步測試的 setting.xml 值全部去掉。
然後在 pom.xml 裡面的 <id> 為 test 的 <profile> 節點,加入 <activation> 節點:
<
project
xmlns
=
"
"
xmlns:xsi
=
"
"
xsi:schemaLocation
=
"
"
>
<
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
>
</
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
>
執行命令:
mvn test -Denv=test
提示 1:上面使用 -D 傳遞環境變數,其中 env 對應剛才設定的 <name> 值,test 對應<value>。
提示 2:在 Windows 10 上測試了系統的環境變數,但是不生效,所以,只能通過
-D 傳遞。
執行結果:

4、通過作業系統啟用配置檔案
activation 元素包含下面的作業系統資訊。當系統為 windows XP 時,test Profile 將會被觸發。
<
profile
>
<
id
>
test
</
id
>
<
activation
>
<
os
>
<
name
>
Windows XP
</
name
>
<
family
>
Windows
</
family
>
<
arch
>
x86
</
arch
>
<
version
>
5.1.2600
</
version
>
</
os
>
</
activation
>
</
profile
>
現在開啟命令控制檯,跳轉到 pom.xml 所在目錄,並執行下面的 mvn 命令。不要使用 -P 選項指定 Profile 的名稱。Maven 將顯示被啟用的 test Profile 的結果。
mvn test
5、通過檔案的存在或者缺失啟用配置檔案
現在使用 activation 元素包含下面的作業系統資訊。當 target/generated-sources/axistools/wsdl2java/com/companyname/group 缺失時,test Profile 將會被觸發。
<
profile
>
<
id
>
test
</
id
>
<
activation
>
<
file
>
<
missing
>
target/generated-sources/axistools/wsdl2java/
com/companyname/group
</
missing
>
</
file
>
</
activation
>
</
profile
>
現在開啟命令控制檯,跳轉到 pom.xml 所在目錄,並執行下面的 mvn 命令。不要使用 -P 選項指定 Profile 的名稱。Maven 將顯示被啟用的 test Profile 的結果。
mvn test
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70006413/viewspace-2906349/,如需轉載,請註明出處,否則將追究法律責任。