且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

激活配置文件时,在Maven子模块中添加依赖关系

更新时间:2022-12-06 09:00:32

这是因为个人资料不会在maven中继承。这意味着孩子POM中的 debug-true 不会继承在父POM中也称为 debug-true 的配置文件的激活。



您有两种解决办法:



1)调用 mvn -Pdebug -true 将触发每个POM中的相应配置文件



2)在每个 POM

个人我会推荐第一个解决方案。


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

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.