Why can't I activate a Maven2 profile from another profile?

浪尽此生 提交于 2019-11-26 09:52:24

问题


I have a multimodule Maven2 project which builds a web application. The application is connected to a backend server and a DB. There are several server instances deployed in our environment, and there are also multiple backend and DB instances for development, UAT, production, etc. So practically, each application configuration needs these 3 coordinates:

  • front-end server
  • back-end server
  • DB

I am working on unifying and automating the application configuration. It is easy and obvious to represent these different configurations as profiles in Maven. Then I can create a specific configuration by activating one profile from each group, e.g.

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

This is a bit tedious to type and error-prone - if a specific server is misconfigured to connect to the wrong DB, the price can be high. One obvious way to fix this would be to put all useful profile combinations into script files.

But I thought I could be more clever than that by activating the necessary back-end and DB profile directly from the server profile. The server profiles are in the main pom, e.g.

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

And the backend and DB profiles are in the pom of the Config submodule, e.g.

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

So in theory, since the server.myserver profile sets the db property to dev02, this should trigger the activation of the db.dev02 profile in the child pom. However, this does not happen. (Nor if the two profiles are in the same pom, btw). If I set the property from the command line with

mvn -Ddb=dev02 help:active-profiles

then the profile is activated though, so apparently I haven\'t misspelled anything.

Have I overlooked something? Is there any other way to make this work?

I see that there exists a similar question: Can I make one maven profile activate another?
However, IMHO this is not a duplicate - I see that my approach is not working and I would like to understand why. (I have read the reference, but I might have overlooked something obvious).


回答1:


The feature simply doesn't exist. The property activator uses the incoming properties, not anything set by the profiles (as otherwise it wouldn't know what order to activate them in without some more complex logic).

The solution you used, of have identical properties to activate the things you want to do together, is the best solution. I realise that may not always be satisfactory - in that case all you can do is fall back to making the individual profiles as simple as possible so that you can combine them in the ways you want on the command line, without duplicating things across them.

The issue covering this feature is: https://issues.apache.org/jira/browse/MNG-3309
The issue covering the property activation is: https://issues.apache.org/jira/browse/MNG-2276




回答2:


Issue MNG-2276 mentioned by Brett was resolved in maven 3.x, so you are now allowed to define properties in settings.xml to trigger profiles in your pom. Here is an example:

In settings.xml:

<profile>
    <id>localDist</id>
    <activation>
        <property><name>localDist</name></property>
    </activation>
    <properties>
        <doReleaseTasks>true</doReleaseTasks>
    </properties>
</profile>

In your pom (or better yet, in your parent pom):

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

Good idea to use enforcer plugin to force mvn 3.0 or greater:

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


来源:https://stackoverflow.com/questions/2246033/why-cant-i-activate-a-maven2-profile-from-another-profile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!