2015-12-10 57 views
2

这是我使用Maven的构建配置文件的第一天。我有以下文件模式:Maven设置和POM中的相同配置文件名称

  1. 的pom.xml
  2. Maven的设置(%USER_HOME%/平米/ 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>) 

我经历this文档和它说,

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

所以,我假设我的部署应该由于Maven设置中的密码不正确而失败。我在这里错过了什么?

+0

您使用的是哪个版本的maven 3? –

+0

apache-maven-3.3.9 –

+0

我在maven 3.1.1和3.3.9上试过,实际上设置配置文件总是覆盖样本pom中具有相同名称的配置文件的属性。所以文件是正确的,你有没有进一步了解你所报告的行为? –

回答

2

下面是一个简单的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和来自pom配置文件的hello2)。
因此,这两个配置文件同时处于活动状态,它们的属性被合并(因为hello共享相同的名称),设置中的属性优先于来自POM的属性,然后POM的附加属性得到正确使用。

因此,我无法重现您提到的场景。我会建议仔细检查设置和POM并添加一个额外的属性来玩。

相关问题