1. 程式人生 > >Maven中的-D(Properties屬性)和-P(Profiles配置檔案)

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為觸發配置檔案。

總結:

更多的參考Maven幫助文件:

Google搜尋關鍵字:maven d flag