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程式碼- <project>
- <profiles>
- <profile>
- <build>
-
<defaultGoal>
- <finalName>...</finalName>
- <resources>...</resources>
- <testResources>...</testResources>
- <plugins>...</plugins>
- </build>
-
<reporting
- <modules>...</modules>
- <dependencies>...</dependencies>
- <dependencyManagement>...</dependencyManagement>
- <distributionManagement>...</distributionManagement>
- <repositories>...</repositories>
- <pluginRepositories>...</pluginRepositories>
- <properties>...</properties>
- </profile>
- </profiles>
- </project>
二、啟用Profile
Maven 提供了一種針對不同環境引數“啟用”一個profile 的方式,這就叫做profile 啟用。啟用配置元素下可以包含一個或者多個選擇器:包含JDK 版本,作業系統引數,檔案,以及屬性。當所有標準都被滿足的時候一個profile 才會被啟用。例如,一個profile可以要求作業系統家族為Windoes,JDK 版本為1.4,那麼該profile 只有當構建在Windows 機器上的Java 1.4 上執行的時候才會被啟用。如果該profile 被啟用,那麼它定義的所有配置都會覆蓋原來POM 中對應層次的元素,就像使用命令列引數-P 引入該profile 一樣。
例如:
Xml程式碼- <project>
- ...
- <profiles>
- <profile>
- <id>dev</id>
- <activation>
- <activeByDefault>false</activeByDefault>
- <jdk>1.5</jdk>
- <os>
- <name>Windows XP</name>
- <family>Windows</family>
- <arch>x86</arch>
- <version>5.1.2600</version>
- </os>
- <property>
- <name>mavenVersion</name>
- <value>2.0.5</value>
- </property>
- <file>
- <exists>file2.properties</exists>
- <missing>file1.properties</missing>
- </file>
- </activation>
- ...
- </profile>
- </profiles>
- </project>
你可以基於一個屬性如environment.type 的值來啟用一個profile。當environment.type 等於dev 的時候啟用development profile,或者當environment.type 等於prod 的時候啟用production profile。你也可以通過一個屬
性的缺失來啟用一個profile。下面的配置中,只有在Maven 執行過程中屬性environment.type 不存在profile 才被啟用。 例如:
- <project>
- ...
- <profiles>
- <profile>
- <id>development</id>
- <activation>
- <property>
- <name>!environment.type</name>
- </property>
- </activation>
- </profile>
- </profiles>
- </project>
三、外部Profiles檔案
要定義名部的Profiles檔案,只需要在${baseDir}下定義一個profiles.xml檔案即可,檔案的內容結構大約是:
Xml程式碼- <profiles>
- <profile>
- <id>development</id>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <debug>true</debug>
- <optimize>false</optimize>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </profile>
- <profile>
- <id>production</id>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <debug>false</debug>
- <optimize>true</optimize>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </profile>
- </profiles>
三、使用者特定的Profile和全域性的Profile
要定義使用者特定的Profile,只需要配置系統使用者目錄下的~/.m2/settings.xml;要配置全域性Profile,需要配置Maven_Home/conf/settings.xml。
四、列出活動的Profile
使用者可以在以下四個地方配置Profile
- pom.xml
- profiles.xml
- ~/.m2/settings.xml
- Maven_Home/conf/settings.xml
可以通過命令:
C程式碼- $ mvn help:active-profiles
得到所有活動的profile