Cannot find profile dependent dependency in a child pom - maven

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.

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 Variable web project not working

I´ve got a modular maven project that contains 3 modules. In one of those (WEB), the profile variable is not being recognized as an environment variable, but it takes que real name of it, as shown below:
<Principal POM>
<Module App-core>
<Module ejb>
<Module web>
At the principal POM, there are 3 profiles thas have the same profile propertie with distinct values:
<profile1>
<environment>value1</environment>
<profile2>
<environment>value2</environment>
<profile3>
<environment>value3</environment>
That variable is used as classifier variable for dependencies, as below:
<dependency>
<groupId>com.test</groupId>
<artifactId>app-core</artifactId>
<version>1.0.0</version>
<classifier>${environment}</classifier>
</dependency>
At maven compile phase, only at Web Project, it doen´t change the variable name with the variable value.
Any help?
The tags profile1 profile2 profile3 don't enable the replacing property. To do this you have change your parent pom in this way
<project>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>profile1</id>
<properties>
<environment>value1</environment>
</properties>
</profile>
<profile>
<id>profile2</id>
<properties>
<environment>value2</environment>
</properties>
</profile>
<profile>
<id>profile3</id>
<properties>
<environment>value2</environment>
</properties>
</profile>
</profiles>
</project>
Now you have to run mvn install -P profile1 yourProjectName
I've resolved the issue.
I've replicated the dependencies into profiles. I had multiples dependencies
This is the code to the profile1
<profile>
<id>profile1</id>
<properties>
<environment>value1</environment>
</properties>
<dependency>
<groupId>com.test</groupId>
<artifactId>app-core</artifactId>
<version>1.0.0</version>
<classifier>${environment}</classifier>
</dependency>
</profile>
This is the code to the profiles2
<profile>
<id>profile2</id>
<properties>
<environment>value2</environment>
</properties>
<dependency>
<groupId>com.test</groupId>
<artifactId>app-core</artifactId>
<version>1.0.0</version>
<classifier>${environment}</classifier>
</dependency>
</profile>
And this the code is to the profile3
<profile>
<id>profile3</id>
<properties>
<environment>value3</environment>
</properties>
<dependency>
<groupId>com.test</groupId>
<artifactId>app-core</artifactId>
<version>1.0.0</version>
<classifier>${environment}</classifier>
</dependency>
</profile>

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.

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

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

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

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.

Resources