1. 程式人生 > >maven下的pom.xml中定義的profile

maven下的pom.xml中定義的profile

一、定義Profile

  • pom.xml 中的profiles 元素,它包含了一個或者多個profile 元素。由於profile 覆蓋了pom.xml 中的
    預設設定,profiles 通常是pom.xml 中的最後一個元素。
  • 每個 profile 必須要有一個id 元素。這個id 元素包含的名字將在命令列呼叫profile 時被用到。我們可以
    通過傳給Maven 一個-P<profile_id>引數來呼叫profile。
  • 一個 profile 元素可以包含很多其它元素,只要這些元素可以出現在POM XML 文件的project 元素下
    面。

Maven profile 可以覆蓋幾乎所有pom.xml 中的配置。Maven POM 包含一個名為profiles 的元素,它包含了項
目的替代配置,在這個元素下面,每個profile 元素定義了一個單獨的profile。每個profile 必須要有一個id,除此之外,它可以包含幾乎所有你能在project 下看到的元素。

一個Profile 可以覆蓋專案構件的最終名稱,專案依賴,外掛配置以影響構建行為。Profile 還可以覆蓋分發配置;例如,如果你通過一個staging profile 釋出一個構件到staging 伺服器上,你就可以建立一個profile 然後在裡面定義distributionManagement 元素。

Xml程式碼  收藏程式碼
  1. <project>  
  2.     <profiles>  
  3.         <profile>  
  4.             <build>  
  5.                 <defaultGoal>
    ...</defaultGoal>  
  6.                 <finalName>...</finalName>  
  7.                 <resources>...</resources>  
  8.                 <testResources>...</testResources>  
  9.                 <plugins>...</plugins>  
  10.             </build>  
  11.             <reporting
    >...</reporting>  
  12.             <modules>...</modules>  
  13.             <dependencies>...</dependencies>  
  14.             <dependencyManagement>...</dependencyManagement>  
  15.             <distributionManagement>...</distributionManagement>  
  16.             <repositories>...</repositories>  
  17.             <pluginRepositories>...</pluginRepositories>  
  18.             <properties>...</properties>  
  19.         </profile>  
  20.     </profiles>  
  21. </project>  

二、啟用Profile

Maven 提供了一種針對不同環境引數“啟用”一個profile 的方式,這就叫做profile 啟用。啟用配置元素下可以包含一個或者多個選擇器:包含JDK 版本,作業系統引數,檔案,以及屬性。當所有標準都被滿足的時候一個profile 才會被啟用。例如,一個profile可以要求作業系統家族為Windoes,JDK 版本為1.4,那麼該profile 只有當構建在Windows 機器上的Java 1.4 上執行的時候才會被啟用。如果該profile 被啟用,那麼它定義的所有配置都會覆蓋原來POM 中對應層次的元素,就像使用命令列引數-P 引入該profile 一樣。 

例如:

Xml程式碼  收藏程式碼
  1. <project>  
  2.     ...  
  3.     <profiles>  
  4.         <profile>  
  5.             <id>dev</id>  
  6.             <activation>  
  7.                 <activeByDefault>false</activeByDefault>  
  8.                 <jdk>1.5</jdk>  
  9.                 <os>  
  10.                     <name>Windows XP</name>  
  11.                     <family>Windows</family>  
  12.                     <arch>x86</arch>  
  13.                     <version>5.1.2600</version>  
  14.                 </os>  
  15.                 <property>  
  16.                     <name>mavenVersion</name>  
  17.                     <value>2.0.5</value>  
  18.                 </property>  
  19.                 <file>  
  20.                     <exists>file2.properties</exists>  
  21.                     <missing>file1.properties</missing>  
  22.                 </file>  
  23.             </activation>  
  24.             ...  
  25.         </profile>  
  26.     </profiles>  
  27. </project>  

你可以基於一個屬性如environment.type 的值來啟用一個profile。當environment.type 等於dev 的時候啟用development profile,或者當environment.type 等於prod 的時候啟用production profile。你也可以通過一個屬
性的缺失來啟用一個profile。下面的配置中,只有在Maven 執行過程中屬性environment.type 不存在profile 才被啟用。  例如:

Xml程式碼  收藏程式碼
  1. <project>  
  2.     ...  
  3.     <profiles>  
  4.         <profile>  
  5.             <id>development</id>  
  6.             <activation>  
  7.                 <property>  
  8.                     <name>!environment.type</name>  
  9.                 </property>  
  10.             </activation>  
  11.         </profile>  
  12.     </profiles>  
  13. </project>  

三、外部Profiles檔案

要定義名部的Profiles檔案,只需要在${baseDir}下定義一個profiles.xml檔案即可,檔案的內容結構大約是:

Xml程式碼  收藏程式碼
  1. <profiles>  
  2.     <profile>  
  3.         <id>development</id>  
  4.         <build>  
  5.             <plugins>  
  6.                 <plugin>  
  7.                     <groupId>org.apache.maven.plugins</groupId>  
  8.                     <artifactId>maven-compiler-plugin</artifactId>  
  9.                     <configuration>  
  10.                     <debug>true</debug>  
  11.                     <optimize>false</optimize>  
  12.                     </configuration>  
  13.                 </plugin>  
  14.             </plugins>  
  15.         </build>  
  16.     </profile>  
  17.     <profile>  
  18.         <id>production</id>  
  19.         <build>  
  20.             <plugins>  
  21.                 <plugin>  
  22.                     <groupId>org.apache.maven.plugins</groupId>  
  23.                     <artifactId>maven-compiler-plugin</artifactId>  
  24.                     <configuration>  
  25.                     <debug>false</debug>  
  26.                     <optimize>true</optimize>  
  27.                     </configuration>  
  28.                 </plugin>  
  29.             </plugins>  
  30.         </build>  
  31.     </profile>  
  32. </profiles>  

三、使用者特定的Profile和全域性的Profile

要定義使用者特定的Profile,只需要配置系統使用者目錄下的~/.m2/settings.xml;要配置全域性Profile,需要配置Maven_Home/conf/settings.xml。

四、列出活動的Profile

使用者可以在以下四個地方配置Profile

  1. pom.xml
  2. profiles.xml
  3. ~/.m2/settings.xml
  4. Maven_Home/conf/settings.xml

可以通過命令: 

C程式碼  收藏程式碼
  1. $ mvn help:active-profiles  

得到所有活動的profile