Adding dependencies in a Maven sub-module when a profile is activated

半世苍凉 提交于 2019-12-11 05:26:51

问题


I have a project with a parent pom.xml which define profiles, and a debug profile :

<profile>
    <id>debug-true</id>
    <activation>
        <property>
            <name>debug</name>
            <value>true</value>
        </property>
    </activation>
</profile>

I want that one of my sub-modules adds the dependency jboss-seam-debug when the profile debug is activated.

I written this children pom.xml :

<profiles>
    <profile>
        <id>debug-true</id>
        <dependencies>
            <dependency>
                <groupId>org.jboss.seam</groupId>
                <artifactId>jboss-seam-debug</artifactId>
            </dependency>
        </dependencies>
    </profile>
</profiles>

But it doesn't work, that dependency is not part of the dependency tree when I specify -Ddebug=true ... it's like the children pom.xml re-defines my debug profile ...

Do you know how could I add jboss-seam-debug dependency to my sub-module when the property debug has the value true ?


Actually, here is my full need which is a bit more complex.

Here is my parent pom.xml :

<profiles>
    <profile>
        <id>env-dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>env</name>
                <value>dev</value>
            </property>
        </activation>
        <properties>
            <debug>true</debug>
    ... other properties ...
        </properties>
    </profile>
    ...

Generally, I just pass -Denv=dev on the mvn command line and wanted my sub-module to activate jboss-seam-debug only when property debug is defined to true so I wrote that in the sub-module pom.xml :

<profiles>
    <profile>
        <id>debug-true</id>
        <activation>
            <property>
                <name>debug</name>
                <value>true</value>
            </property>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.jboss.seam</groupId>
                <artifactId>jboss-seam-debug</artifactId>
            </dependency>
        </dependencies>
    </profile>
    ...

which didn't work only by passing -Denv=dev because I don't pass the system property -Ddebug=true, it's the maven property which is activated by my parent pom.xml, and that my children doesn't "see" ...


回答1:


it's because profiles are not inhertited in maven. This means debug-true in child POM does not inhertif the activation of profile in parent POM also called debug-true.

You have two possibilities to solve this:

1) call mvn -Pdebug-true which will trigger the coresponding profile in each POM

2) add activation code in each POM

Personally I would prefere first solution.



来源:https://stackoverflow.com/questions/4784750/adding-dependencies-in-a-maven-sub-module-when-a-profile-is-activated

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