maven 打包時自定義變量的使用


  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>

以上參考:http://stackoverflow.com/questions/17332857/how-to-use-the-mvn-d-to-set-multiple-properties-in-maven-via-command-line

 

-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://stackoverflow.com/questions/17332857/how-to-use-the-mvn-d-to-set-multiple-properties-in-maven-via-command-line

http://books.sonatype.com/mvnref-book/reference/running-sect-options.html#running-sect-define-prop


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM