为什么我无法从另一个配置文件激活Maven2配置文件?
我有一个多模块Maven2项目,它构建了一个Web应用程序。 该应用程序连接到后端服务器和数据库。 在我们的环境中部署了几个服务器实例,并且还有多个用于开发,UAT,生产等的后端和数据库实例。实际上,每个应用程序配置都需要这3个坐标:
我正在努力统一和自动化应用程序配置。 将这些不同的配置表示为Maven中的配置文件是很容易和明显的。 然后,我可以通过激活每个组中的一个配置文件来创建特定的配置,例如
mvn -Pserver.Server1,backend.prod,db.uat clean install
这种输入方式有点繁琐且容易出错 - 如果某个特定的服务器配置错误而无法连接到错误的数据库,价格可能很高。 解决此问题的一个显而易见的方法是将所有有用的配置文件组合放入脚本文件中。
但我认为我可以通过直接从服务器配置文件激活必要的后端和数据库配置文件来实现这一点。 服务器配置文件在主要的pom中,例如
<profile>
    <id>server.myserver</id>
    <properties>
        <jboss.home>D:Programsjboss-4.2.1.GA</jboss.home>
        <server.name>NightlyBuild</server.name>
        <hosttobind>192.168.1.100</hosttobind>
        <servlet.port>8080</servlet.port>
        ...
        <db>dev02</db>
    </properties>
</profile>
后端和DB配置文件位于Config子模块的pom中,例如
<profile>
    <id>db.dev02</id>
    <activation>
        <property>
            <name>db</name>
            <value>dev02</value>
        </property>
    </activation>
    <properties>
        <jdbc.address>jdbc:oracle:thin:@192.168.0.101:1521:dbdev02</jdbc.address>
    </properties>
</profile>
  因此理论上,由于server.myserver配置文件将db属性设置为dev02 ,因此应该触发子pom中db.dev02配置文件的激活。  但是,这不会发生。  (如果这两个配置文件是在同一个pom中,btw)。  如果我用命令行设置属性 
mvn -Ddb=dev02 help:active-profiles
然后配置文件被激活,所以显然我没有拼写任何东西。
我忽略了什么? 有没有其他的方式来完成这项工作?
  我发现存在类似的问题:我可以让一个maven配置文件激活另一个吗? 
  但是,恕我直言,这不是重复的 - 我看到我的方法不工作,我想明白为什么。  (我已经阅读了参考文献,但我可能忽略了一些明显的内容)。 
该功能根本不存在。 属性激活器使用传入属性,而不是配置文件设置的任何东西(否则,如果没有更复杂的逻辑,它将不知道以什么顺序激活它们)。
您使用的解决方案具有相同的属性以激活您想要一起完成的任务,这是最佳解决方案。 我意识到这可能并不总是令人满意 - 在这种情况下,你所能做的就是回到使个人简档尽可能简单,以便可以在命令行中以所需的方式组合它们,而无需在它们之间重复。
  涵盖此功能的问题是:https://issues.apache.org/jira/browse/MNG-3309 
  涉及财产激活的问题是:https://issues.apache.org/jira/browse/MNG-2276 
Brett提到的MNG-2276问题已在maven 3.x中解决,因此您现在可以在settings.xml中定义属性来触发您的pom中的配置文件。 这里是一个例子:
在settings.xml中:
<profile>
    <id>localDist</id>
    <activation>
        <property><name>localDist</name></property>
    </activation>
    <properties>
        <doReleaseTasks>true</doReleaseTasks>
    </properties>
</profile>
在你的pom中(或者更好的是,在你的父pom中):
<profile>
    <id>doReleaseTasks</id>
    <activation>
        <property><name>doReleaseTasks</name></property>
    </activation>
    <build>
        <plugins>
            ... mvn -DlocalDist will activate these plugins
        </plugins>
    </build>
</profile>
好主意使用执行者插件强制mvn 3.0或更高版本:
<build>
    <plugins>
        <plugin>
            <artifactId>maven-enforcer-plugin</artifactId>
            <executions>
                <execution>
                    <id>enforce-maven</id>
                    <goals> <goal>enforce</goal> </goals>
                    <configuration>
                        <rules>
                            <requireMavenVersion>
                                <version>[3.0,)</version>
                                <message>
*** Maven 3.x required to allow cascading profiles to be activated in settings.xml (MNG-2276)
                                </message>
                            </requireMavenVersion>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
上一篇: Why can't I activate a Maven2 profile from another profile?
下一篇: Activate different Maven profiles depending on current module?
