Maven child profile activated on parent profile property [duplicate] - maven

This question already has answers here:
Activating a Child Profile from a Parent Profile
(2 answers)
Closed 5 years ago.
I have two pom's an aggregate and a child, the child has a profile I would like to be activated based upon a property set in the parents profile eg.
parent pom.xml
<profiles>
<profile>
<id>emptyModules</id>
<modules>
<module>child</module>
</modules>
<properties>
<emptyModules>true</emptyModules>
</properties>
</profile>
</profiles>
The child has two profiles one with some modules the other without
<profiles>
<profile>
<id>modules</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</profile>
<profile>
<id>emptyModules</id>
<activation>
<property>
<name>emptyModules</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>
When I activate the parent emptyModules profile I want the child emptyModule profile to activate, however the default profile is used instead! Any ideas?

You can activate profiles based on properties:
<profiles>
<profile>
<activation>
<property>
<name>someprop</name>
</property>
</activation>
...
</profile>
</profiles>
This could be used to activate a parent and child profile at once.

Related

Set a property inside a maven profile conditionally, depending on another profile

Is there a way so that, inside a maven profile, I could set a property to one value or another, depending on whether another profile is also active ?
Example: for the sample below,
<profiles>
<profile>
<id>dev</id>
<properties>
<myProp>value-dev</myProp>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<myProp>value-prod</myProp>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<myProp>value-test</myProp>
</properties>
</profile>
</profiles>
if ONLY DEV is active, then I need myProp = value-dev
if DEV and TEST are active, then I need myProp = value-test
How can I achieve this ?
Thanks.

Activating a Child Profile from a Parent Profile

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

how to update the child module parent version

HI My project structure is like this
parentPrj
pom.xml
childModule1
pom.xml
ChildModule2
pom.xml
ChildModule2
pom.xml
I dont want to build all the child builds so I am building each module based on the profile (something like below)
// here is the some sample code
<profiles>
<profile>
<id>child1</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<modules>
<module>childModule1</module>
</modules>
</profile>
<profile>
<id>child2</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<modules>
<module>childModule2</module>
</modules>
</profile>
<profile>
<id>child3</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<modules>
<module>childModule3</module>
</modules>
</profile>
</profiles>
// end of the sample code
My goal is when I run child1 profile I wanted to build childModule1 project and update the parent version of the pom in all three child modules .. Can you help me how to do that? I have tried many options like
release:update-versions -DautoVersionSubmodules=true ,mvn versions:set etc..

how to set a property value according to profile

I'm new to maven and i got this issue when sometimes when i run maven i want to have a property with 1 value and sometimes the other.
I tried to do the following but still something is missing:
<profiles>
<profile>
<id>production</id>
<activation>
<property>
<name>com.sencha.cmd.dir</name>
<value>
${env.SENCHA_PATH}\senchaCmd-${com.sencha.cmd.version}\Sencha\Cmd\${com.sencha.cmd.version}
</value>
</property>
</activation>
</profile>
</profiles>
<properties>
<com.sencha.cmd.dir> ${env.SENCHA_PATH}\senchaCmd- ${com.sencha.cmd.version}\Sencha\Cmd\${com.sencha.cmd.version}</com.sencha.cmd.dir>
my question is how to replace the value of the property com.sencha.cmd.dir according to the availability of the profile?
Hope this is clear enough
It's really simple
<profiles>
<profile>
<id>profile 1</id>
<activation>
...
</activation>
<properties>
<my.property> xxx </my.property>
</properties>
</profile>
<profile>
<id>profile 2</id>
<activation>
...
</activation>
<properties>
<my.property> yyy </my.property>
</properties>
</profile>
</profiles>
and then you can use your my.properties outside, even in an other one properties
<properties>
<my.next.property> abc ${my.property} def </my.next.property>
</properties>

In Maven, can a profile override the modules (to not include any)

In maven, once you define your modules in you pom.xml all profiles aggregate the modules defined in them: (relevant part only)
<project>
<modules>
<module>module1</module>
</modules>
<profiles>
<profile>
<id>pr1</id>
<modules>
<moudule>module2</module>
</modules>
If you perform a mvn clean it will pass the command to module1.
If you issue mvn clean -Ppr1 it will pass along to module1 and module2.
I wonder if in maven 3 it is possible to have a pom.xml with submodules and override this. I mean to execute a profile that instead of add their own modules to the build force those like:
<project>
<!-- omitted -->
<modules>
<!-- modules -->
</modules>
<build>
<!-- build -->
</build>
<profiles>
<profile>
<!-- This profile with no modules -->
</profile>
</profiles>
</project>
The requirement might sound silly, but I just want to know if there is a mechanism like in plugin configuration.
<configuration self.combine="override"
Regards!
ssedano
It's not possible. Profile configuration is always merged with main configuration, so you only can add modules in your case.
What you can do instead, however, is to create a some kind of "default" profile (by <activeByDefault>true</activeByDefault> in <activation> section) that is enabled when no other profiles are invoked and put your default modules' list there. Then, when no profile is specified on Maven build call, this "default" profile is used, but when you call explicitly at least one profile, it's not, so you can this way define modules' list from scratch.
While the question is old, Google still ranks it highly, so it makes sense to add a new answer.
You can use the activation by absence of a property trick to achieve what you want.
<profiles>
<!-- By default, include the modules -->
<profile>
<id>full-build</id>
<activation>
<!-- Activation by absence of a property. Run normally, without -Dskip-modules -->
<property>
<name>!skip-modules</name>
</property>
</activation>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
</profile>
<!-- No-modules build -->
<profile>
<id>no-modules</id>
<activation>
<!-- Activation by a property. Run with -Dskip-modules to activate -->
<property>
<name>skip-modules</name>
</property>
</activation>
<modules>
</modules>
</profile>
</profiles>
You can do things like this:
<profiles>
<profile>
<id>run-xyz</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>www-ab</module>
<module>www-cd</module>
</modules>
</profile>
<profile>
<id>run-its</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<modules>
<module>www-db</module>
<module>www-it</module>
</modules>
</profile>
</profiles>
If you are talking about such things. But i would recommend to be very carefull about such things.
It's only the question how you set activeByDefault. With this it's possible to create more or less any combination.

Resources