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

在继续学习本教程的内容之前,假定您已经安装了Maven。如果你还没有,请按照我的这篇博客逐步进行操作。

要在Maven中指定配置文件,您需要使用pom.xml文件中的activeProfiles或profiles元素 。pom.xml在运行时被修改 。

有3种构建配置文件类型。

  1. 每个项目
    在pom.xml文件中定义
  2. 每个用户
    在Maven设置xml文件(%USER_HOME%/.m2/settings.xml)中定义
  3. 全局
    在Maven全局设置xml文件(%M2_HOME%/conf/settings.xml)中定义

如何构建Maven配置文件?有两种方法:

  1. 终端–本教程涵盖
  2. Maven设置–本教程涵盖
  3. 环境变量–在本教程中涵盖
  4. 操作系统设置
  5. 存在或丢失的文件

使用终端激活配置文件

创建您的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个配置文件,但是我们也可以在标签中添加更多的标签。

覆盖所需的插件之后,该运行我们的配置文件了。通过在命令行中输入**mvn test -P **来运行它

在我们的例子中,我们需要编写**mvn test -Ptest1,**因为我们创建的配置文件将其ID为“ test1”。

现在转到项目的文件夹位置,然后键入 **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.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
Results :
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.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 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本地存储库中,可以阅读我的这一篇文章。