代码之家  ›  专栏  ›  技术社区  ›  Péter Török

为什么我不能从另一个配置文件激活Maven2配置文件?

  •  15
  • Péter Török  · 技术社区  · 16 年前

    我有一个多模块Maven2项目,它构建了一个web应用程序。应用程序连接到后端服务器和数据库。我们的环境中部署了多个服务器实例,还有多个用于开发、UAT、生产等的后端和DB实例。因此实际上,每个应用程序配置都需要以下3个坐标:

    • 前端服务器
    • 后端服务器

    我正致力于统一和自动化应用程序配置。在Maven中将这些不同的配置表示为概要文件是很容易和明显的。然后我可以通过从每个组激活一个配置文件来创建一个特定的配置,例如。

    mvn -Pserver.Server1,backend.prod,db.uat clean install
    

    这是一个有点乏味的类型和容易出错-如果一个特定的服务器配置错误,连接到错误的数据库,价格可能会很高。解决这个问题的一个明显方法是将所有有用的概要文件组合放入脚本文件中。

    <profile>
        <id>server.myserver</id>
        <properties>
            <jboss.home>D:\Programs\jboss-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>
    

    后端和数据库配置文件在配置子模块的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 ,这将触发 db.dev02

    mvn -Ddb=dev02 help:active-profiles
    

    然后配置文件被激活,所以很明显我没有拼错任何东西。

    我忽略了什么吗?有没有别的办法让这件事成功?

    我看到有一个类似的问题: Can I make one maven profile activate another?

    2 回复  |  直到 9 年前
        1
  •  23
  •   Helder Pereira Arjit    9 年前

    这个功能根本不存在。property activator使用传入的属性,而不是由概要文件设置的任何内容(否则,如果没有更复杂的逻辑,它将不知道以什么顺序激活它们)。

    涉及此功能的问题是: https://issues.apache.org/jira/browse/MNG-3309
    涉及属性激活的问题是: https://issues.apache.org/jira/browse/MNG-2276

        2
  •  5
  •   russtman    12 年前

    Issue MNG-2276 Brett提到的问题在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>
    

    <profile>
        <id>doReleaseTasks</id>
        <activation>
            <property><name>doReleaseTasks</name></property>
        </activation>
        <build>
            <plugins>
                ... mvn -DlocalDist will activate these plugins
            </plugins>
        </build>
    </profile>
    

    使用enforcer插件强制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>
    
    推荐文章