How do I use pom.xml profile properties with pom aggregation? - maven

I am using a pom.xml to aggregate various builds. I'd like to setup profiles with settings that apply to multiple pom.xml files. So, I have something like
<project>
....
<modules>
<module>project1</module>
<module>project2</module>
</modules>
<profiles>
<profile>
<id>local</id>
<properties>
<my.setting>setting</my.setting>
</properties>
<profile>
</profiles>
</project>
However, my.setting doesn't seem to get put into the child modules. How do get this to work?

The problem is that you are using only aggegration. But you must use inheritance in the child modules which means to have a parent entry in there. Then the information will be propagated to the childs.

Related

Cannot find profile dependent dependency in a child pom

Could anyone advice on the following:
I have parent maven xml like this:
<project>
...
<modules>
<module>Module_1</module>
</modules>
<profiles>
<profile>
<id>p1</id>
<modules>
<module>Module_2</module>
</modules>
<profile>
</profiles>
...
</project>
And I have child pom dependent on parent one and on the Module_2 because it has beans that I want to use in my child module:
<project>
...
<profiles>
<profile>
<id>p1</id>
<dependencies>
<dependency>
<groupId>some.group</groupId>
<artifactId>Module_2</module>
<version>${project.version}</version>
</dependency>
</dependencies>
<profile>
</profiles>
...
</project>
So, I want Module_2 built and included as dependency into child module only with -PModule_2 key.
But I cannot build the project because it cannot find Module_2 dependency in the child module.
Is there any way to solve this?
Thanks.

Maven run only the parent pom with a profile

In my project, I have a custom profile custom-profile-name.
A simplified structure of my POM looks like this:
<artifactId>parent</artifactId>
<modules>
<module>child</module>
</modules>
When I run
mvn help:active-profiles -P custom-profile-name
I get:
Active Profiles for Project 'org.sample:parent:pom':
The following profiles are active:
custom-profile-name
Active Profiles for Project 'org.sample:child':
The following profiles are active:
custom-profile-name
I've been reading about profile inheritance and If I understand correctly, profiles should not be inherited. Can anyone explain why the custom-profile-name is active in the child module.
My ultimate goal is to execute the parent with one configuration of a custom plugin and all child modules with another configuration of the same plugin.
Not sure why both parent and child modules are getting activated for custom-profile-name. But to get whats needed for you can be done by defining properties inside the profile.
Example:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>parent-app</name>
<url>http://maven.apache.org</url>
<modules>
<module>child</module>
</modules>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>child</module>
</modules>
<properties>
<parentProp>foo</parentProp>
<childProp>foo</childProp>
</properties>
</profile>
<profile>
<id>custom-profile-name</id>
<modules>
<module>child</module>
</modules>
<properties>
<parentProp>xyz</parentProp>
<childProp>abc</childProp>
</properties>
</profile>
</profiles>
The 'parentProp' is a configuration used by the parent pom and 'childProp' is the configuration used by the child pom. From the configuration it can be seen that the default profile and the 'custom-profile-name' profile behaves differently as the values for the properties is different.

One profile properties are overriding with another profile properties in maven?

I have a problem with maven profiles. Coming to the details, I have two profiles like profile1 and profile2. I have declared few properties for both the profiles along with the modules which needs to be updated by each profile individually. Let see the below configuration,
<profiles>
<profile>
<id>profile1</id>
<properties>
<owner>ABC</owner>
</properties>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<profile>
<profile>
<id>profile2</id>
<properties>
<owner>XYZ</owner>
</properties>
<modules>
<module>module3</module>
<module>module4</module>
</modules>
<profile>
</profiles>
Coming to the point, profile1 property ABC has to update in module1 and module2 and profile2 property XYZ has to update in module3 and module4. while building the application I have tried the below all commands.
mvn clean install -Pprofile1,profile2
mvn clean install -P profile1,profile2
when I use the above commands to build the project, XYZ has updating in all the modules. Similarly, when I use the below commands ABC is updating in all 4 modules.
mvn clean install -Pprofile2,profile1
mvn clean install -P profile2,profile1
My requirement is to update ABC only in module1 and module2, XYZ in module3 and module4. Could you please tell me, any solution which will solve this problem.
Note: I have even tried for the below command,
mvn clean install -Pprofile1 -Pprofile2
Build has failed with goal or life cycle issue.
-Thanks
The property in your aggregator is unique. So with your configuration, one profile overrides the other.
The solution in your case is to take the property out of the profile:
Aggregator:
<profiles>
<profile>
<id>profile1</id>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<profile>
<profile>
<id>profile2</id>
<modules>
<module>module3</module>
<module>module4</module>
</modules>
<profile>
</profiles>
Module 1 and 2 (no profile):
<properties>
<owner>ABC</owner>
</properties>
Module 3 and 4 (no profile):
<properties>
<owner>XYZ</owner>
</properties>
Since in your case the properties are always the same for each respective module.
However
As khmarbaise already wrote, your usage of profile seems somewhat odd...

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.

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