-

Maven - Build Profiles

What is Build Profile?

A Build profile is a set of configuration values which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments.

Profiles are specified in pom.xml file using its activeProfiles / profiles elements and are triggered in variety of ways. Profiles modify the POM at build time, and are used to give parameters different target environments (for example, the path of the database server in the development, testing, and production environments).

Types of Build Profile

Build profiles are majorly of three types

Type Where it is defined
Per Project Defined in the project POM file, pom.xml
Per User Defined in Maven settings xml file (%USER_HOME%/.m2/settings.xml)
Global Defined in Maven global settings xml file (%M2_HOME%/conf/settings.xml)

Profile Activation

A Maven Build Profile can be activated in various ways.

Profile Activation Examples

Let us assume following directory structure of your project:

Maven创建配置文件

Now, under src/main/resources there are three environment specific files:

File Name Description
env.properties default configuration used if no profile is mentioned.
env.test.properties test configuration when test profile is used.
env.prod.properties production configuration when prod profile is used.

显式配置文件激活

在下面的例子中,我们将附加maven-antrun-plugin:run目标到测试阶段。这将允许我们回显不同配置文件的短信。我们将使用pom.xml来定义不同的配置文件,并使用maven命令在命令控制台上激活配置文件。

假设我们在C: MVN project文件夹中创建了以下pom.xml。

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <profiles>
      <profile>
      <id>test</id>
      <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</version>
            <executions>
               <execution>
                  <phase>test</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                  <tasks>
                     <echo>Using env.test.properties</echo>
            <copy file="src/main/resources/env.test.properties" tofile
		    ="${project.build.outputDirectory}/env.properties"/>
                  </tasks>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
      </build>
      </profile>
   </profiles>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

假设我们在C: MVN project src resources文件夹中创建了以下属性文件。

env.properties

environment=debug

env.test.properties

environment=test

env.prod.properties

environment=prod

现在打开命令控制台,转到包含pom.xml的文件夹,然后执行以下mvn命令。使用-P选项传递配置文件名称作为参数。

C:MVNproject>mvn test -Ptest

Maven将开始处理并显示测试创建配置文件的结果。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project --
-
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pr
oject ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:MVNprojectsrc	est
esources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ project
 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ project ---
[INFO] Surefire report directory: C:MVNproject	argetsurefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.companyname.bank.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (default) @ project ---
[INFO] Executing tasks
     [echo] Using env.test.properties
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.953 s
[INFO] Finished at: 2015-09-27T11:54:45+05:30
[INFO] Final Memory: 9M/247M
[INFO] ------------------------------------------------------------------------

现在作为练习,您可以执行以下步骤

现在打开命令控制台,转到包含pom.xml的文件夹,然后执行以下mvn命令。使用-P选项将配置文件名称作为参数传递。

C:MVNproject>mvn test -Pnormal
C:MVNproject>mvn test -Pprod

检查创建的输出以查看差异。

通过Maven设置配置文件激活

在%USER_HOME%/。m2目录中打开Maven settings.xml文件,其中%USER_HOME%表示用户主目录。如果settings.xml文件不存在,请创建一个新的。

使用activeProfiles节点将测试配置文件添加为活动配置文件,如下例所示

<settings xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/settings-1.0.0.xsd">
   <mirrors>
      <mirror>
         <id>maven.dev.snaponglobal.com</id>
         <name>Internal Artifactory Maven repository</name>
         <url>http://repo1.maven.org/maven2/</url>
         <mirrorOf>*</mirrorOf>
      </mirror>
   </mirrors>
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>

现在打开命令控制台,转到包含pom.xml的文件夹,然后执行以下mvn命令。不要使用-P选项传递配置文件名称.Maven将显示测试配置文件为活动配置文件的结果。

C:MVNproject>mvn test

通过环境变量配置文件激活

现在删除maven settings.xml中的活动配置文件,并更新pom.xml中提到的测试配置文件。将激活元素添加到配置文件元素,如下所示。

当系统属性“env”用值“test”指定时,测试配置文件将触发。创建环境变量“env”并将其值设置为“test”。

<profile>
   <id>test</id>
   <activation>
      <property>
         <name>env</name>
         <value>test</value>
      </property>
   </activation>
</profile>

让我们打开命令控制台,转到包含pom.xml的文件夹,然后执行下面的mvn命令。

C:MVNproject>mvn test

通过操作系统配置文件激活

激活元素包括os细节,如下所示。当系统是Windows XP时,此测试配置文件将触发。

<profile> <id> test </ id> <activation> <os> <name> Windows XP </ name> <family> Windows </ family> <arch> x86 </ arch> <version> 5.1.2600 <版本> </ os> </ activation> </ profile>
   
   
      
         
         
         
         
      
   

现在打开命令控制台,转到包含pom.xml的文件夹,然后执行以下mvn命令。不要使用-P选项传递配置文件名称.Maven将显示测试配置文件为活动配置文件的结果。

C:MVNproject>mvn test

配置文件激活通过现在/缺少文件

现在激活元素包括os细节如下所示。target / generated-sources / axistools / wsdl2java / com / companyname / group丢失时,测试配置文件将触发

<profile>
   <id>test</id>
   <activation>
      <file>
         <missing>target/generated-sources/axistools/wsdl2java/
		 com/companyname/group</missing>
      </file>
   </activation>
</profile>

现在打开命令控制台,转到包含pom.xml的文件夹,然后执行以下mvn命令。不要使用-P选项传递配置文件名称.Maven将显示测试配置文件为活动配置文件的结果。

C:MVNproject>mvn test