Calling profile of a child module from profile defined in parent POM - maven

I have the following profile in parent POM :
<profile>
<id>myprofile</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</profile>
Now in my child POM of module2 I have the following profile :
<profiles>
<profile>
<id>module2profile</id>
<build>
<finalName>mywar</finalName>
<plugins>
....
</plugins>
</build>
</profile>
</profiles>
My requirement is that from the parent POM profile, instead of referring "module2", I want to refer to profile "module2profile".
I need this because in my module2 I am creating 2 different war files (one that is created normally while the other is created by "module2profile").

Related

How to acess a property under specific profile in Maven

I have 2 pom files - ParentPOM and ChildPOM. I want to Access property1 in ParentPOM in ChildPOM. I know this can be done by making ParentPOM as the parent for ChildPOM and use ${property1}. But property1 is defined in 2 profiles - trunk and release and I always want to get the value of property1 defined in release. How do I do that? ie:- In the below example, I want the value should be 0.0.2 and not 0.0.1 when I Access property1 in ChildPOM.
Note:I cannot modify ParentPOM
<project>
<modelVersion>x.x.x</modelVersion>
<artifactId>ParentPOM</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>ParentPOM</name>
<profiles>
<profile>
<id>trunk</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<property1>0.0.1</property1>
</properties>
</profile>
<profile>
<id>release</id>
<properties>
<property1>0.0.2</property1>
</properties>
</profile>
</profiles>
</project>
<project>
<modelVersion>x.x.x</modelVersion>
<parent>
<groupId>com.temp</groupId>
<artifactId>ParentPOM</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>ChildPOM</artifactId>
<version>2.0.0</version>
<packaging>pom</packaging>
<name>ChildPOM</name>
<profiles>
<profile>
<id>trunk</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<property1>x.x.x</property1>
</properties>
</profile>
<profile>
<id>release</id>
<properties>
<property1>y.y.y</property1>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxx</artifactId>
<version>${property1}</version>
</dependency>
</dependencies>
</project>
Introduce a property "releaseProperty1" in your parent pom. Use this property both for the definition of "property1" in the profile "release" and for your use cases in the child pom.
There is one cleanish solution, but it all depends on the why... why do you want this?
The solution is: removing the release-profile from child and execute Maven like this: mvn <goal> -Prelease,!trunk, i.e enable release profile and disable trunk profile.
To confirm this solution, run mvn help:evaluate -Dexpression=property1 -Prelease,!trunk

maven profile based and non-profile based plugin execution

I have pom.xml with the following contents.
<profiles>
<profile>
<id>P1</id>
<build>
... some exec-maven-plugin entries
</build>
</profile>
<profile>
<id>P2</id>
<build>
... some exec-maven-plugin entries
</build>
</profile>
<profiles>
<build>
... some more (but common for all) exec-maven-plugin entries
</build>
Only one of the profile P1 or P2 is activated from command line. But whatever the selected profile, it is expected that plugins in the common section (i.e., the build section outsides the profiles) should also execute. It is also expected that the plugins both in the profile and the common section get executed in the order of configured phase.
Now the build works with maven2 but fails with maven3. I have not been to able to debug this exactly.
With Maven3, would this work as expected?
In what order would the plugins get executed?
Or is it only the selected profile that alone builds?
UPDATE: The common build does get executed. There were conflicting execution IDs in common and in one of the profile. So it failed.
You need something like this:
<profiles>
<profile>
<id>P1</id>
<build>
.. executed for with -PP1
</build>
</profile>
<profile>
<id>P2</id>
<build>
.. executed for with -PP2
</build>
</profile>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
.. executed with no profile
</build>
</profile>
</profiles>
<build>
.. executed for in all cases.
</build>

setting active profile defined in parent pom in project pom

We have a corporate wide Super Pom we use to define many of the defaults we use. For example, the Super Pom defines what version of the JDK to use, and other parameters. This is inherited by our projects as the parent pom.
Most of our projects use JDK 1.7, but one set of projects is still on version JDK 1.6. I've put the following profile definitions in my parent pom:
<properties>
<travelclick.snapshot.repo>artifactory/libs-snapshot-local</travelclick.snapshot.repo>
<old.javac.source>1.5</old.javac.source>
<old.javac.target>1.6</old.javac.target>
</properties>
<profiles>
...
<profile>
<id>jdk1.6</id>
<build>
<plugins>
<plugin>
<groupId>com.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<...>
<source>${old.javac.source}</source>
<target>${old.javac.target}</target>
<...>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<...>
</profiles>
Now, I have a profile called jdk1.6 and I'd like to specify in the project's pom that it should use this one by default. How do I do this?
I've tried adding into the project's pom:
<profiles>
<profile>
<id>jdk1.6</id>
<activations>
<activeByDefault>true</activeByDefault>
</activations>
</profile>
</profile>
But that redefines my jdk1.6 profile.
I've tried putting in this:
<activeProfiles>
<activeProfile>jdk1.6</activeProfile>
</activeProfiles>
But that only works in settings.xml.
How do I specify a profile in the parent pom, and then say that this is the active profile in the child pom?
More Attempts
I've tried using properties. In my parent pom.xml:
<profiles>
<profile>
<id>jdk1.6</id>
<activation>
<property>
<name>use-jdk1.6</name>
</property>
</activation>
<profile>
</profiles>
And the following in my local pom:
<properties>
<use-jdk1.6>true</use-jdk1.6>
</properties>
But, it doesn't pick up the profile. And, this does work:
$ mvn -Puse-jdk1.6 clean package site
So, I know that the parent profiles do work.
Profiles
Could you add yours profile details exectuing goal help:all-profiles
[INFO] Listing Profiles for Project: xxxx
Profile Id: artifactory (Active: true , Source: settings.xml)
Profile Id: jdk1.6 (Active: false , Source: pom)
Profile Id: arse-version (Active: false , Source: pom)
Profile Id: urge (Active: false , Source: pom)
I can activate jdk1.6 from the command line. I just want to activate it as the default in my child poms.
AAAAHGGGGHHHH!
That's me screaming.
I found the issue and why this wasn't working.
In my parent pom, I had the following:
<properties>
<javac.source>1.7</javac.source>
<javac.source>1.7</javac.source>
<old.javac.source>1.7</old.javac.source>
<old.javac.source>1.7</old.javac.source>
...
</properties>
<profiles>
<profile>
<id>jdk1.6</id>
<activation>
<property>
<name>use-jdk1.6</name>
</property>
</activation>
<build>
<plugins>
....
<plugin>
<groupId>maven-compiler-plugin</groupId>
...
<configuration>
<!-- This isn't doing what I think -->
<source>${old.javac.source}</source>
<target>${old.javac.target}</source>
...
</configuration>
</plugin>
</plugins>
<profile>
In my child pom, I had this:
<properties>
<use-jdk1.6>true</use-jdk1.6>
</properties>
And, it appeared that setting the use-jdk1.6 property just wasn't working. However, that wasn't the case. I was setting the profile.
What happens is if I have the system property javac.source and javac.target set, it overrides the configuration of the maven-compiler-pluing (even though I had explicitly set <source> and <target> not to use version 1.7).
So, I spent six hours on this issue before I realized it was due to me setting a property named javac.source rather than something like java-version.

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

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