Activating a Child Profile from a Parent Profile
Asked Answered
I

2

13

I have the following parent pom.xml file:

<profile>
    <id>build_full</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <modules>
        <module>mymodule_interface</module>
        <module>mymodule_switch</module>
        <module>mymodule_switch_simulator</module>
        <module>mymodule_switch_controller</module>
        <module>mymodule_server</module>
    </modules>
</profile>

and in my child pom for mymodule_server, I have the following:

<profile>
    <id>subprofile</id>
    <modules>
        <module>...various modules...</module>
    </modules>
</profile>  
<profile>
    <id>default</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <modules>
        <module>...various modules...</module>
    </modules>
</profile>  

How, when I invoke maven: mvn -P build_full, can I force the child module (mymodule_server) to use profile subprofile rather than default?

Indecent answered 10/2, 2016 at 0:23 Comment(0)
W
10

No, you can't activate a child profile from a parent profile. More generally, you can't activate or deactivate any profile from any other profile (there is a JIRA feature-request MNG-3309). What you can do is activate two profiles based on the same property.

First of all, using a profile that is activated by default is generally not a good idea. What you want is to activate a profile based on some condition (OS version, system property...). To solve your problem, you can activate build_full profile when a certain system property is present, and make sure that subprofile is also activated when that same property is present.

A sample configuration would be the following, where both profiles are activated when the fullBuild system property is set to true. Invoking Maven with mvn -DfullBuild=true ... will thus activate both profiles.

<profile>
    <id>build_full</id>
    <activation>
        <property>
            <name>fullBuild</name>
            <value>true</value>
        </property>
    </activation>
    <modules>
        <module>mymodule_interface</module>
        <module>mymodule_switch</module>
        <module>mymodule_switch_simulator</module>
        <module>mymodule_switch_controller</module>
        <module>mymodule_server</module>
    </modules>
</profile>
<profile>
    <id>subprofile</id>
    <activation>
        <property>
            <name>fullBuild</name>
            <value>true</value>
        </property>
    </activation>
    <modules>
        <module>...various modules...</module>
    </modules>
</profile>
Wildermuth answered 10/2, 2016 at 8:35 Comment(3)
I just tested locally before posting my answer, I probably made a mistake, gonna re-test it thenSakmar
@A.DiMatteo This should work when running from the parent POM. Your solution should work also but I'm not quite found of all the -P parameters (I think this is too fragile)Wildermuth
I confirm both solutions work well. Indeed your proposal is less verbose from command line and centralizes the activation in one common place, better.Sakmar
S
2

In your case, from the top parent/aggregator folder, you could just run:

mvn clean install -Pbuild_full,!default,subprofile

It will disable any profile having name default (and hence disable the profile in the concerned sub-module) and enable any profile having name subprofile (and hence enable the profile you wanted).

Alternatively, you could configure subprofile as such:

<profiles>
    <profile>
        <id>subprofile</id>
        <activation>
            <property>
                <name>subprofile</name>
                <value>true</value>
            </property>
        </activation>
        ....

and then run as following:

mvn clean install -Dsubprofile=true -Pbuild_full

It will have the same effect. You can even avoid the value element and simply specify -Dsubprofile, its existence would be enough to activate the profile (in that case a more meaningful name is suggested, like -DactivateSubprofile). Since you active a different profile, automatically Maven will deactivate the default one.

Sakmar answered 10/2, 2016 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.