23.md 7.3 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# 如何使用 Maven 配置文件

> 原文: [https://javatutorial.net/how-to-use-maven-profiles](https://javatutorial.net/how-to-use-maven-profiles)

简而言之, [Maven](https://javatutorial.net/how-to-install-maven-on-windows-linux-and-mac) 配置文件是一组覆盖默认值的配置值。 通过使用它,您可以为不同的环境(生产/开发)创建自定义版本。

![java-featured-image](img/e0db051dedc1179e7424b6d998a6a772.jpg)

在继续学习本教程的内容之前,假定您已经安装了 Maven。 如果您不这样做,请按照本教程 的逐步指南进行操作。

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

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

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

如何提示 Maven Build 配置文件? 有两种方法:

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

W
wizardforcel 已提交
30
### 显式激活配置文件
W
init  
wizardforcel 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

创建您的 Maven 项目(如果尚未创建),然后创建第一个简单的配置文件 test1。

这是我添加的 pom.xml 代码

```java
<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>

```

配置文件-&gt;配置文件-&gt; **id** ; 那是我们指定如何引用配置文件的地方。 不要错过该行,这很重要,因为它不仅是必填项,而且如果您省略它,则将无法访问您的个人资料。

我们在 pom.xml 文件中所做的是,我们已覆盖了编译器插件设置。 我们已将编译器的版本设置为 1.5,并将 fork 设置为 true。

请记住,在这种情况下,我们仅创建了 1 个配置文件,但是我们也可以在&lt;配置文件&gt;标签内添加更多&lt;配置文件&gt;标签。

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

在我们的例子中,我们需要编写 **mvn test -Ptest1** ,因为我们创建的个人资料给我们提供了值为“ test1”的 ID。

现在转到项目的文件夹位置,然后输入 **mvn test -P &lt;您的配置文件 ID &gt;。** 如果我在上面的示例中运行此命令,则得到的结果是:

```java
[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 文件,请创建一个。

然后将我们创建的配置文件添加为活动配置文件。 使用以下代码:

```java
<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 测试。**

### 使用环境变量激活配置文件

删除 settings.xml 文件并在**名称**标签中添加 **env** 值。 像这样:

```java
<profile>
   <id>test</id>
   <activation>
      <property>
         <name>env</name>
         <value>test1</value>
      </property>
   </activation>
</profile>
```

您必须创建一个称为 env 的环境变量,并将其值设置为“ test1”。

导航到包含 pom.xml 的文件夹,然后键入 **mvn test。**

如果您希望将自定义库包含到 maven 本地存储库中,可以遵循[本文](https://javatutorial.net/how-to-include-custom-library-into-maven-local-repository)