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

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.

Related

Is it possible for a maven profile to reference the content of another profile or inherit from another profile?

background
We have to build our app for different environments, the only information about the target environment passed to building pipeline (in a standalone building machine we can't reach) is a property with value, thus
mvn package -Dtarget=env1
to adapt to different environments, we maintains several profiles activated by different property values in our main pom:
<project>
<groupId>whatever</groupId>
<artifactId>whatever</artifactId>
<version>whatever</version>
<profiles>
<profile>
<id>app-for-env1</id>
<activation>
<property>
<name>target</name>
<value>env1</value>
</property>
</activation>
<properties>
<!-- properties for env1 -->
</properties>
<modules>
<!-- modules for env1 -->
</modules>
<dependencyManagement>
<!-- dependencyManagement for env1 -->
</dependencyManagement>
</profile>
<profile>
<id>app-for-env2</id>
<activation>
<property>
<name>target</name>
<value>env2</value>
</property>
</activation>
<properties>
<!-- properties for env2 -->
</properties>
<modules>
<!-- modules for env2 -->
</modules>
<dependencyManagement>
<!-- dependencyManagement for env2 -->
</dependencyManagement>
</profile>
</profiles>
</project>
for now it works for two different runtime environments env1 and env2.
however, we need to support env3 which is very similar to env2, so we have to duplicate the whole app-for-env2 profile to meet the requirement.
so is it possible for a maven profile to reference the content of another profile or inherit from another profile?

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

Maven sub module is not skipped during build

Using maven 3.1.1
In my parent pom I have:
<groupId>com.samples</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Parent</name>
<modules>
<module>a</module>
<module>b</module>
<module>c</module>
</modules>
<profiles>
<profile>
<id>skip-c</id>
<modules>
<module>a</module>
<module>b</module>
</modules>
</profile>
...
But module c is still build with I build with:
mvn clean package -Pskip-c
How do I skip a submodule when building my parent project?
Your problem is that your complete module list is always active. You could use a default profile instead.
<groupId>com.samples</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Parent</name>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>a</module>
<module>b</module>
<module>c</module>
</modules>
</profile>
<profile>
<id>skip-c</id>
<modules>
<module>a</module>
<module>b</module>
</modules>
</profile>
</profiles>
In addition to julschis comment some details:
Profiles do not replace the normal non profile content of the pom. Therefore, when using your profile, you get a, b and c from the normal pom, and a and b from the profile, therefore your profile is useless.
You need to remove your conditional modules from the main part and put it only into profiles (usually an activeByDefault Profile).
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>a</module>
<module>b</module>
</modules>
</profile>
<profile>
<id>add-c</id>
<modules>
<module>a</module>
<module>b</module>
<module>c</module>
</modules>
</profile>
So it is the other way around, profiles cannot remove anything, but only add or override content.
This technique is called reactor pruning and described here: http://www.blackbuild.com/how-to-really-use-maven-profiles-without-endangering-your-karma/
Your could also use the new functionality from https://jira.codehaus.org/browse/MNG-5230, and simply call (this requires Maven 3.2.1+)
mvn clean package -pl !c
Deactivation of profiles can be done using Maven 2.0.10 + .
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
Deactivating a profile
Starting with Maven 2.0.10, one or more profiles can be deactivated using the command line by prefixing their identifier with either the character '!' or '-' as shown below:
mvn groupId:artifactId:goal -P !profile-1,!profile-2
This can be used to deactivate profiles marked as activeByDefault or profiles that would otherwise be activated through their activation config.

Using profiles to control which Maven modules are built

I have the following XML in my maven POM.xml:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>default</name>
<value>!disabled</value>
</property>
</activation>
<modules>
<module>m1</module>
<module>m2</module>
<module>m3</module>
</modules>
</profile>
<profile>
<id>x</id>
<modules>
<module>m1</module>
</modules>
</profile>
</profiles>
What I'm trying to achieve is this:
When I run mvn install, I want it to build m1, m2 and m3 projects.
When I run mvn install -Px, I want it to only build m1.
My current problem is that with the code above, option 2 builds all m1, m2 and m3.
Found the solution guys, define 'x' profile first and the 'default' and it works fine (insane Maven!!). Here's the final result:
<profiles>
<!-- DO NOT CHANGE THE *ORDER* IN WHICH THESE PROFILES ARE DEFINED! -->
<profile>
<id>x</id>
<modules>
<module>m1</module>
</modules>
</profile>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>m1</module>
<module>m2</module>
<module>m3</module>
</modules>
</profile>
</profiles>
You can disable maven profiles that have runByDefault set to true from the command line like so:
mvn install -P !default
Note, this requires Maven version 2.0.10.
Just add a space after -P the sintax of the command is
mvn install -P x
And not like you are using
mvn install -Px
Take a look at Maven - Introduction to profiles

Resources