1. Overview
1.概述
Maven profiles can be used to create customized build configurations, like targeting a level of test granularity or a specific deployment environment.
Maven配置文件可用于创建定制的构建配置,如针对测试粒度水平或特定的部署环境。
In this tutorial, we’ll learn how to work with Maven profiles.
在本教程中,我们将学习如何使用Maven配置文件。
2. A Basic Example
2.一个基本的例子
Normally when we run mvn package, the unit tests are executed as well. But what if we want to quickly package the artifact and run it to see if it works?
通常,当我们运行mvn打包时,单元测试也会被执行。但是如果我们想快速打包工件并运行它以查看它是否工作呢?
First, we’ll create a no-tests profile that sets the maven.test.skip property to true:
首先,我们要创建一个no-test配置文件,将maven.test.skip属性设为true:。
<profile>
<id>no-tests</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
Next, we’ll execute the profile by running the mvn package -Pno-tests command. Now the artifact is created and the tests are skipped. In this case the mvn package -Dmaven.test.skip command would have been easier.
接下来,我们将通过运行mvn package -Pno-tests命令来执行该配置文件。现在工件已经创建,测试被跳过。在这种情况下,mvn package -Dmaven.test.skip命令会更简单。
However, this was just an introduction to Maven profiles. Let’s take a look at some more complex setups.
然而,这只是对Maven配置文件的一个介绍。让我们来看看一些更复杂的设置。
3. Declaring Profiles
3.申报概况
In the previous section, we saw how to create one profile. We can configure as many profiles as we want by giving them unique ids.
在上一节中,我们看到如何创建一个配置文件。我们可以通过赋予它们唯一的ID来配置我们想要的许多配置文件。
Let’s say we wanted to create a profile that only ran our integration tests and another for a set of mutation tests.
假设我们想创建一个配置文件,只运行我们的集成测试,另一个用于一组突变测试。
We would begin by specifying an id for each one in our pom.xml file:
我们将首先为我们的pom.xml文件中的每个人指定一个id。
<profiles>
<profile>
<id>integration-tests</id>
</profile>
<profile>
<id>mutation-tests</id>
</profile>
</profiles>
Within each profile element, we can configure many elements such as dependencies, plugins, resources, finalName.
在每个profile元素中,我们可以配置许多元素,如dependencies、plugins、resources、finalName。
So, for the example above, we could add plugins and their dependencies separately for integration-tests and mutation-tests.
因此,对于上面的例子,我们可以为集成测试和突变测试分别添加插件和它们的依赖关系。
Separating tests into profiles can make the default build faster by having it focus, say, on just the unit tests.
将测试分离到配置文件中,可以使默认的构建速度更快,例如,让它只关注单元测试。
3.1. Profile Scope
3.1.简介范围
Now, we just placed these profiles in our pom.xml file, which declares them only for our project.
现在,我们只是把这些配置文件放在我们的pom.xml文件中,它只为我们的项目声明。
But, in Maven 3, we can actually add profiles to any of three locations:
但是,在Maven 3中,我们实际上可以将配置文件添加到三个位置中的任何一个。
- Project-specific profiles go into the project’s pom.xml file
- User-specific profiles go into the user’s settings.xml file
- Global profiles go into the global settings.xml file
Note that Maven 2 did support a fourth location, but this was removed in Maven 3.
请注意,Maven 2确实支持第四个位置,但在Maven 3中被删除。
We try to configure profiles in the pom.xml whenever possible. The reason is that we want to use the profiles both on our development machines and on the build machines. Using the settings.xml is more difficult and error-prone as we have to distribute it across build environments ourselves.
我们尽可能在pom.xml中配置配置文件。原因是我们希望在我们的开发机器和构建机器上都使用这些配置文件。使用settings.xml更困难,也更容易出错,因为我们必须自己在构建环境中分发。
4. Activating Profiles
4.激活配置文件
After we create one or more profiles we can start using them, or in other words, activating them.
在我们创建了一个或多个配置文件之后,我们可以开始使用它们,或者换句话说,激活它们。
4.1. Seeing Which Profiles Are Active
4.1.查看哪些配置文件是活动的
Let’s use the help:active-profiles goal to see which profiles are active in our default build:
让我们使用help:active-profiles 目标来查看在我们的默认构建中哪些配置文件是活跃的。
mvn help:active-profiles
Actually, since we haven’t activated anything yet, we get:
实际上,由于我们还没有激活任何东西,我们得到的是。
The following profiles are active:
Well, nothing.
嗯,没什么。
We’ll activate them in just a moment. But quickly, another way to see what is activated is to include the maven-help-plugin in our pom.xml and tie the active-profiles goal to the compile phase:
我们一会儿就会激活它们。但很快,查看激活内容的另一种方法是:在我们的pom.xml 中包含maven-help-plugin,并将active-profiles目标与编译阶段挂钩:。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>show-profiles</id>
<phase>compile</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now, let’s get to using them! We’ll look at a few different ways.
现在,让我们开始使用它们!我们将看一下几个不同的方法。
4.2. Using -P
4.2.使用-P
Actually, we already saw one way at the beginning, which is that we can activate profiles with the -P argument.
实际上,我们在一开始已经看到了一种方法,那就是我们可以用-P参数激活配置文件。
So let’s begin by enabling the integration-tests profile:
因此,让我们从启用集成-测试配置文件开始。
mvn package -P integration-tests
If we verify the active profiles, with the maven-help-plugin or the mvn help:active-profiles -P integration-tests command we’ll get the following result:
如果我们用maven-help-plugin或mvn help:active-profiles -P integration-tests命令验证活动配置文件,我们会得到以下结果。
The following profiles are active:
- integration-tests
In case we want to activate multiple profiles at the same time, we use a comma-separated list of profiles:
如果我们想同时激活多个配置文件,我们使用逗号分隔的配置文件列表。
mvn package -P integration-tests,mutation-tests
4.3. Active by Default
4.3.默认情况下是活跃的
If we always want to execute a profile, we can make one active by default:
如果我们总是想执行一个配置文件,我们可以让一个配置文件默认为活动的。
<profile>
<id>integration-tests</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
Then, we can run mvn package without specifying the profiles, and we can verify that the integration-test profile is active.
然后,我们可以运行mvn package而不指定配置文件,我们可以验证集成-测试配置文件是激活的。
However, if we run the Maven command and enable another profile then the activeByDefault profile is skipped. So when we run mvn package -P mutation-tests then only the mutation-tests profile is active.
然而,如果我们运行Maven命令并启用另一个配置文件,那么activeByDefault配置文件就会被跳过。所以当我们运行mvn package -P mutation-test时,只有mutation-test配置文件是激活的。
When we activate in other ways, the activeByDefault profile is also skipped as we’ll see in the next sections.
当我们以其他方式激活时,activeByDefault配置文件也会被跳过,我们将在接下来的章节中看到。
4.4. Based on a Property
4.4.基于一个属性
We can activate profiles on the command-line. However, sometimes it’s more convenient if they’re activated automatically. For instance, we can base it on a -D system property:
我们可以在命令行上激活配置文件。然而,有时自动激活它们会更方便。例如,我们可以基于-D 系统属性:。
<profile>
<id>active-on-property-environment</id>
<activation>
<property>
<name>environment</name>
</property>
</activation>
</profile>
We now activate the profile with the mvn package -Denvironment command.
我们现在用mvn package -Denvironment命令激活配置文件。
It’s also possible to activate a profile if a property is not present:
如果一个属性不存在,也有可能激活一个配置文件。
<property>
<name>!environment</name>
</property>
Or we can activate the profile if the property has a specific value:
或者我们可以激活配置文件,如果该属性有一个特定的值:。
<property>
<name>environment</name>
<value>test</value>
</property>
We can now run the profile with mvn package -Denvironment=test.
我们现在可以用mvn package -Denvironment=test.运行配置文件。
Lastly, we can activate the profile if the property has a value other than the specified value:
最后,如果该属性的值不是指定的值,我们可以激活配置文件。
<property>
<name>environment</name>
<value>!test</value>
</property>
4.5. Based on the JDK Version
4.5.基于JDK版本
Another option is to enable a profile based on the JDK running on the machine. In this case, we want to enable the profile if the JDK version starts with 11:
另一个选项是根据机器上运行的JDK来启用配置文件。在本例中,如果JDK版本以11开头,我们要启用配置文件。
<profile>
<id>active-on-jdk-11</id>
<activation>
<jdk>11</jdk>
</activation>
</profile>
We can also use ranges for the JDK version as explained in Maven Version Range Syntax.
我们还可以为JDK版本使用范围,如Maven版本范围语法中所解释的。
4.6. Based on the Operating System
4.6.基于操作系统的
Alternatively, we can activate the profile based on some operating system information.
另外,我们可以根据一些操作系统信息来激活配置文件。
And if we aren’t sure of that, we can first use the mvn enforcer:display-info command which gives the following output on my machine:
如果我们不确定,我们可以先使用mvn enforcer:display-info命令,在我的机器上有如下输出。
Maven Version: 3.5.4
JDK Version: 11.0.2 normalized as: 11.0.2
OS Info: Arch: amd64 Family: windows Name: windows 10 Version: 10.0
After that, we can configure a profile that is activated only on Windows 10:
之后,我们可以配置一个只在Windows 10上激活的配置文件。
<profile>
<id>active-on-windows-10</id>
<activation>
<os>
<name>windows 10</name>
<family>Windows</family>
<arch>amd64</arch>
<version>10.0</version>
</os>
</activation>
</profile>
4.7. Based on a File
4.7.基于一个文件
Another option is to run a profile if a file exists or is missing.
另一个选择是在文件存在或缺失的情况下,运行一个配置文件。
So, let’s create a test profile that only executes if the testreport.html is not yet present:
因此,让我们创建一个测试配置文件,只在testreport.html还没有出现时执行。
<activation>
<file>
<missing>target/testreport.html</missing>
</file>
</activation>
5. Deactivating a Profile
5.停用一个配置文件
We’ve seen many ways to activate profiles, but sometimes we need to disable one as well.
我们已经看到许多激活配置文件的方法,但有时我们也需要禁用一个配置文件。
To disable a profile we can use the ‘!’ or ‘-‘.
要禁用一个配置文件,我们可以使用’!’或’-‘。。
So, to disable the active-on-jdk-11 profile we execute the mvn compile -P -active-on-jdk-11 command.
因此,为了禁用active-on-jdk-11配置文件,我们执行mvn compile -P -active-on-jdk-11命令。
6. Conclusion
6.结语
In this article, we’ve seen how to work with Maven profiles, so we can create different build configurations.
在本文中,我们看到了如何使用Maven配置文件,因此我们可以创建不同的构建配置。
The profiles help to execute specific elements of the build when we need them. This optimizes our build process and helps to give faster feedback to developers.
配置文件有助于在我们需要时执行构建的特定元素。这优化了我们的构建过程,并有助于向开发人员提供更快的反馈。
Feel free to have a look at the finished pom.xml file over on GitHub.
请随时查看GitHub上完成的pom.xml文件。