4

这是我使用 Maven 构建配置文件的第一天。我在以下文件中有个人资料:

  1. pom.xml
  2. Maven 设置 (%USER_HOME%/.m2/settings.xml)

出于好奇,我在两个文件中创建了一个具有相同 id(local_deploy) 的配置文件,唯一的区别在于一个属性(例如 tomcat.pwd)。

POM 中的配置文件如下所示:

<profile>
        <id>local_deploy</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <tomcat.host>localhost</tomcat.host>
            <tomcat.port>8080</tomcat.port>
            <tomcat.url>http://${tomcat.host}:${tomcat.port}/manager/text</tomcat.url>
            <tomcat.user>admin</tomcat.user>
            <tomcat.pwd>admin</tomcat.pwd>
        </properties>
    </profile>

Maven 设置中的配置文件如下所示:

<profile>
    <id>local_deploy</id>
    <properties>
      <tomcat.host>localhost</tomcat.host>
      <tomcat.port>8080</tomcat.port>
      <tomcat.url>http://${tomcat.host}:${tomcat.port}/manager/text</tomcat.url>
      <tomcat.user>admin</tomcat.user>
      <tomcat.pwd>wrongpwd</tomcat.pwd>
    </properties>
  </profile>

请注意,Maven 设置中的配置文件未列在<activeProfiles>.

当我尝试使用以下命令安装我的应用程序时

mvn clean install -P local_deploy help:active-profiles

我的应用程序在控制台上使用以下输出进行部署:

The following profiles are active:
local_deploy (source: external)
local_deploy (source: <my groupId>:<my artifactId><version>)

我正在浏览这个文档,它说

Take note that profiles in the settings.xml takes higher priority than profiles in the POM

所以,我假设我的部署应该因为 Maven 设置中的密码不正确而失败。我在这里想念什么?

4

1 回答 1

4

这是我使用的示例 pom:

    <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.sample</groupId>
    <artifactId>profiles-sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>print-hello</id>
                        <phase>test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <property name="msg" value="${hello}" />
                                <property name="msg2" value="${hello2}" />
                                <echo message="hello from build: ${msg}, ${msg2}" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>p2</id>
            <properties>
                <hello>from-pom</hello>
                <hello2>from-pom-again</hello2>
            </properties>
        </profile>
    </profiles>
</project>

在我的设置中定义:

<profile>
    <id>p2</id>
    <properties>
        <hello>from-settings</hello>
    </properties>
</profile>

因此,请注意:在 POM 和设置上具有相同名称的两个配置文件定义了相同的hello属性。但是,POM 中的那个定义了一个附加属性,hello2

然后,运行:

mvn test -Pp2 help:active-profiles

作为构建输出的一部分,我得到了:

[INFO] --- maven-antrun-plugin:1.5:run (print-hello) @ profiles-sample ---
[INFO] Executing tasks

main:
     [echo] hello from build: from-settings, from-pom-again
[INFO] Executed tasks
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building profiles-sample 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-help-plugin:2.2:active-profiles (default-cli) @ profiles-sample ---
[INFO] 
Active Profiles for Project 'com.sample:profiles-sample:jar:0.0.1-SNAPSHOT': 

The following profiles are active:

 - p2 (source: external)
 - p2 (source: com.sample:profiles-sample:0.0.1-SNAPSHOT)

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

因此,从 Maven 帮助插件中,我们实际上知道两个配置文件都是活动的,这是真的,因为作为 Antrun 的一部分,我们获得了这两个属性(hello来自设置hello2的配置文件和 pom 的配置文件)。
因此,两个配置文件同时处于活动状态,它们的属性被合并(因为hello共享相同的名称),设置中的属性优先于 POM 中的属性,然后 POM 的附加属性正确进入为出色地。

所以,我无法重现你提到的场景。我建议仔细检查设置和 pom 并添加一个额外的属性来玩。

于 2015-12-16T16:33:49.687 回答