maven打包時,如果有一些變量在多個地方使用,可以使用-D的方式或者properties的方式。
使用-D的方式或者properties的方式定義的變量,在Dockfile或者shell腳本或者pom中都可以被動態的替換為期望的值。
原文地址:https://blog.csdn.net/feier7501/article/details/24060089?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
Maven中的-D(Properties屬性)和-P(Profiles配置文件)
-D代表(Properties屬性)
使用命令行設置屬性-D的正確方法是:
mvn -DpropertyName=propertyValue clean package
-
如果
propertyName
不存在pom.xml
,它將被設置。 -
如果
propertyName
已經存在pom.xml
,其值將被作為參數傳遞的值覆蓋-D
。
要發送多個變量,請使用多個空格分隔符加-D
:
mvn -DpropA=valueA -DpropB=valueB -DpropC=valueC clean package
例:
如果你的pom.xml如下
:
<properties> <theme>myDefaultTheme</theme> </properties>
那么在這個執行過程中mvn -Dtheme=halloween clean package
會覆蓋theme
的值,具有如下效果:
<properties> <theme>halloween</theme> </properties>
-P代表(Profiles配置文件)
也就是說在<profiles>指定的<id>中,可以通過-P進行傳遞或者賦值。
例:
如果你的pom.xml如下:
<profiles>
<profile>
<id>test</id>
...
</profile>
</profiles>
執行mvn test -Ptest為觸發配置文件。
或者
<profile>
<id>test</id>
<activation>
<property>
<name>env</name>
<value>test</value>
</property>
</activation>
...
</profile>
執行mvn test -Penv=test為觸發配置文件。
具體實踐例子,參考:http://www.cnblogs.com/EasonJim/p/6828743.html
總結:
更多的參考Maven幫助文檔:
http://books.sonatype.com/mvnref-book/reference/running-sect-options.html#running-sect-define-prop