简而言之, Maven 配置文件是一组覆盖默认值的配置值。 通过使用它,您可以为不同的环境(生产/开发)创建自定义版本。

在继续学习本教程的内容之前,假定您已经安装了 Maven。 如果您不这样做,请按照本教程 的逐步指南进行操作。
要在 Maven 中指定配置文件,您需要使用pom.xml文件中的activeProfiles或配置文件元素。pom.xml在运行时时被修改。
有 3 种构建配置文件类型。
每个项目
- 在
pom.xml文件中定义
- 在
每位使用者
- 在 Maven 设置 xml 文件(
%USER_HOME%/.m2/settings.xml)中定义
- 在 Maven 设置 xml 文件(
全球
- 在 Maven 全局设置 xml 文件(
%M2_HOME%/conf/settings.xml)中定义
- 在 Maven 全局设置 xml 文件(
如何提示 Maven Build 配置文件? 有两种方法:
- 终端–本教程涵盖
- Maven 设置–本教程涵盖
- 环境变量–在本教程中涵盖
- 操作系统设置
- 存在或缺少文件
显式激活配置文件
创建您的 Maven 项目(如果尚未创建),然后创建第一个简单的配置文件test1。
这是我添加的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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>mavenprofilesdemo</groupId><artifactId>mavenprofilesdemo</artifactId><packaging>jar</packaging><version>0.0.1-SNAPSHOT</version><name>Maven Quick Start Archetype</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><profiles><profile><id>test1</id><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><fork>true</fork><compilerVersion>1.5</compilerVersion></configuration></plugin></plugins></build></profile></profiles></project>
<profiles> -> <profile> -> id;那是我们指定如何引用配置文件的地方。 不要错过该行,这很重要,因为它不仅是必填项,而且如果您省略它,则将无法访问您的个人资料。
我们在pom.xml文件中所做的是,我们已覆盖了编译器插件设置。 我们已将编译器的版本设置为 1.5,并将fork设置为true。
请记住,在这种情况下,我们仅创建了 1 个配置文件,但是我们也可以在<profiles>标签内添加更多<profile>标签。
覆盖所需的插件之后,该运行我们的配置文件了。 您可以通过在命令行中输入mvn test -P <id>来运行它
在我们的例子中,我们需要编写mvn test -Ptest1,因为我们创建的个人资料给我们提供了值为test1的 ID。
现在转到项目的文件夹位置,然后输入mvn test -P <您的配置文件 ID>。如果我在上面的示例中运行此命令,则得到的结果是:
[INFO] Scanning for projects...[INFO][INFO] ----------------< mavenprofilesdemo:mavenprofilesdemo >-----------------[INFO] Building Maven Quick Start Archetype 0.0.1-SNAPSHOT[INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenprofilesdemo ---[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory D:\Eclipse Projects\mavenprofilesdemo\src\main\resources[INFO][INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavenprofilesdemo ---[INFO] Nothing to compile - all classes are up to date[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenprofilesdemo ---[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory D:\Eclipse Projects\mavenprofilesdemo\src\test\resources[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavenprofilesdemo ---[INFO] Nothing to compile - all classes are up to date[INFO][INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenprofilesdemo ---[INFO] Surefire report directory: D:\Eclipse Projects\mavenprofilesdemo\target\surefire-reports-------------------------------------------------------T E S T S-------------------------------------------------------Running mavenprofilesdemo.mavenprofilesdemo.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.072 s[INFO] Finished at: 2019-08-18T09:15:55+01:00[INFO] ------------------------------------------------------------------------
使用 Maven 设置激活配置文件
转到您的用户主目录,然后打开.m2文件夹。 如果那里没有settings.xml文件,请创建一个。
然后将我们创建的配置文件添加为活动配置文件。 使用以下代码:
<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.0http://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 test。
使用环境变量激活配置文件
删除settings.xml文件并在name标签中添加env值。 像这样:
<profile><id>test</id><activation><property><name>env</name><value>test1</value></property></activation></profile>
您必须创建一个称为env的环境变量,并将其值设置为test1。
转到包含pom.xml的文件夹,然后键入mvn test。
如果您希望将自定义库包含到 maven 本地存储库中,可以遵循本文。
