Maven 简明教程
Maven - Build Profiles
What is Build Profile?
构建配置文件是一组配置值,可用于设置或覆盖 Maven 构建的默认值。使用构建配置文件,可以针对不同环境(如生产环境与开发环境)自定义构建。
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.
配置文件在 pom.xml 文件中使用其 activeProfiles/profiles 元素指定,并且以多种方式触发。配置文件在构建时修改 POM,并且用于向不同的目标环境提供参数(例如,开发、测试和生产环境中的数据库服务器的路径)。
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
Maven 构建配置文件可以通过多种方式激活。
A Maven Build Profile can be activated in various ways.
-
Explicitly using command console input.
-
Through maven settings.
-
Based on environment variables (User/System variables).
-
OS Settings (for example, Windows family).
-
Present/missing files.
Profile Activation Examples
让我们假设项目的目录结构如下 −
Let us assume the following directory structure of your project −
现在,在 src/main/resources 下,有三个特定于环境的文件 −
Now, under src/main/resources, there are three environment specific files −
Sr.No. |
File Name & Description |
1 |
env.properties default configuration used if no profile is mentioned. |
2 |
env.test.properties test configuration when test profile is used. |
3 |
env.prod.properties production configuration when prod profile is used. |
Explicit Profile Activation
在以下示例中,我们将附加 maven-antrun-plugin:run 目标以测试阶段。这将允许我们为不同的配置文件回显文本消息。我们将使用 pom.xml 定义不同的配置文件,并将使用 maven 命令在命令台中激活配置文件。
In the following example, we will attach maven-antrun-plugin:run goal to test the phase. This will allow us to echo text messages for different profiles. We will be using pom.xml to define different profiles and will activate profile at command console using maven command.
假设我们已在 C:\MVN\project 文件夹中创建了以下 pom.xml。
Assume, we’ve created the following pom.xml in C:\MVN\project folder.
<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>
</project>
现在打开命令台,转到包含 pom.xml 的文件夹,并执行以下 mvn 命令。使用 -P 选项将配置文件名传递为参数。
Now open the command console, go to the folder containing pom.xml and execute the following mvn command. Pass the profile name as argument using -P option.
C:\MVN\project>mvn test -Ptest
Maven 将开始处理并显示测试构建配置文件的结果。
Maven will start processing and displaying the result of test build profile.
C:\MVN>mvn test -Ptest
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.projectgroup:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[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] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ project ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ project ---
[INFO] No tests to run.
[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: 2.011 s
[INFO] Finished at: 2021-12-10T20:29:39+05:30
[INFO] ------------------------------------------------------------------------
C:\MVN>
现在作为一项练习,您可以执行以下步骤:
Now as an exercise, you can perform the following steps −
-
Add another profile element to profiles element of pom.xml (copy existing profile element and paste it where profile elements ends).
-
Update id of this profile element from test to normal.
-
Update task section to echo env.properties and copy env.properties to target directory.
-
Again repeat the above three steps, update id to prod and task section for env.prod.properties.
-
That’s all. Now you’ve three build profiles ready (normal/test/prod).
现在打开命令台,转到包含 pom.xml 的文件夹,并执行以下 mvn 命令。使用 -P 选项将配置文件名称作为参数传递。
Now open the command console, go to the folder containing pom.xml and execute the following mvn commands. Pass the profile names as argument using -P option.
C:\MVN\project>mvn test -Pnormal
C:\MVN\project>mvn test -Pprod
检查构建的输出以查看差异。
Check the output of the build to see the difference.
Profile Activation via Maven Settings
打开位于 %USER_HOME%/.m2 目录中的 Maven settings.xml 文件,其中 %USER_HOME% 表示用户主目录。如果 settings.xml 文件不存在,则创建一个新文件。
Open Maven settings.xml file available in %USER_HOME%/.m2 directory where %USER_HOME% represents the user home directory. If settings.xml file is not there, then create a new one.
添加测试配置文件,并使用活动配置文件节点将其作为活动配置文件,如下例所示。
Add test profile as an active profile using active Profiles node as shown below in example.
<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 将显示测试配置文件成为活动配置文件的结果。
Now open command console, go to the folder containing pom.xml and execute the following mvn command. Do not pass the profile name using -P option. Maven will display result of test profile being an active profile.
C:\MVN\project>mvn test
Profile Activation via Environment Variables
现在从 maven settings.xml 中删除活动配置文件,并更新 pom.xml 中提到的测试配置文件。将 activation 元素添加到配置文件元素,如下所示。
Now remove active profile from maven settings.xml and update the test profile mentioned in pom.xml. Add activation element to profile element as shown below.
当使用值“测试”指定系统属性"env"时,测试配置文件将触发。创建环境变量“env”,并将它的值设置为“测试”。
The test profile will trigger when the system property "env" is specified with the value "test". Create an environment variable "env" and set its value as "test".
<profile>
<id>test</id>
<activation>
<property>
<name>env</name>
<value>test</value>
</property>
</activation>
</profile>
让我们打开命令控制台,转到包含 pom.xml 的文件夹,然后执行以下 mvn 命令。
Let’s open command console, go to the folder containing pom.xml and execute the following mvn command.
C:\MVN\project>mvn test
Profile Activation via Operating System
激活元素将操作系统详细信息包含在内,如下所示。当系统是 Windows XP 时,此测试配置文件将触发。
Activation element to include os detail as shown below. This test profile will trigger when the system is windows XP.
<profile>
<id>test</id>
<activation>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
</profile>
现在打开命令控制台,转到包含 pom.xml 的文件夹,然后执行以下 mvn 命令。不使用 -P 选项传递配置文件名。Maven 将显示测试配置文件是处于活动状态的结果。
Now open command console, go to the folder containing pom.xml and execute the following mvn commands. Do not pass the profile name using -P option. Maven will display result of test profile being an active profile.
C:\MVN\project>mvn test
Profile Activation via Present/Missing File
现在激活元素将操作系统详细信息包含在内,如下所示。当 target/generated-sources/axistools/wsdl2java/com/companyname/group 缺失时,测试配置文件将触发。
Now activation element to include OS details as shown below. The test profile will trigger when target/generated-sources/axistools/wsdl2java/com/companyname/group is missing.
<profile>
<id>test</id>
<activation>
<file>
<missing>target/generated-sources/axistools/wsdl2java/
com/companyname/group</missing>
</file>
</activation>
</profile>
现在打开命令控制台,转到包含 pom.xml 的文件夹,然后执行以下 mvn 命令。不使用 -P 选项传递配置文件名。Maven 将显示测试配置文件是处于活动状态的结果。
Now open the command console, go to the folder containing pom.xml and execute the following mvn commands. Do not pass the profile name using -P option. Maven will display result of test profile being an active profile.
C:\MVN\project>mvn test