-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为触发配置文件。