Search code examples
xmlmavendependenciesprofile

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


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


Solution

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