Maven profile file activation fails - maven

I have a parent pom which defines profiles which shall be activated, when the defined folder exists:
<groupId>common</groupId>
<artifactId>common-build</artifactId>
<profiles>
<profile>
<id>dependency-profile-ejb</id>
<activation>
<file>
<exists>${basedir}/src/profile/dependency-ejb/</exists>
</file>
</activation>
<dependencies>[...]</dependencies>
</profile>
<profile>
<id>dependency-profile-jsf</id>
<activation>
<file>
<exists>${basedir}/src/profile/dependency-jsf/</exists>
</file>
</activation>
<dependencies>[...]</dependencies>
</profile>
</profiles>
I have a maven project with several submodules:
<parent>
<groupId>common</groupId>
<artifactId>common-build</artifactId>
</parent>
<groupId>project/groupId>
<artifactId>project-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>project-child-one<module>
</modules>
In the child project I just define the parent dependency:
<parent>
<groupId>project</groupId>
<artifactId>project-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-child-one</artifactId>
and in this child project I have defined the folder src/profile/dependency-ejb.
If I run the build of the projects in eclipse everything works as expected. I have then installed the project in jenkins, but the build fails. If I checkout the project-parent into a clean directory and try to run a maven build, the build fails, too.
Why does the build run in eclipse but not in command line? Why is the profile definition in the parent pom of common-build not respected?

After some further tests I found the solution. The build failed, because the folders for the profile activation were missing. I have added the project to git and forgot that git does not commit/push empty folders.
The solution is to add an empty .gitkeep in the folders to force git to commit/push them.

Related

How to deploy only part of a multi module maven project?

I have a multi-module maven project, and would like to deploy only 2 sub modules:
pom.xml // parent pom
module1
--pom.xml
module2
--pom.xml
module3
--pom.xml
only module1.jar and module2.jar should be deployed.
I have found the solution in the following question:
How to Deploy only the sub-modules using maven deploy?
The following property should be added to the pom.xml set to false for the modules that should not be deployed:
<properties>
<maven.deploy.skip>true<maven.deploy.skip>
<properties>
From my personal experience, we had a multi maven project. Where many modules was features that some clients had it and some other clients dint had them. For example when we build for 'clientA' we wanted Jenkins to package 'module1', 'module2' and 'module3' but when we build for 'clientB' we wanted only to package 'module1' and 'module2' to just give him the base functionality.
So, its not absolutely wrong to deploy/release part of a multi module project. In the above example there are a case where you need to build some of the modules and a case where you want to build all the modules. In those cases you can do that with profiles.
Example: in parent pom.xml
<profiles>
<profile>
<id>base-functionality</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>/module1</module>
<module>/module2</module>
</modules>
</profile>
<profile>
<id>new-feature</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<modules>
<module>/module3</module>
</modules>
</profile>
</profiles>
and then on Jenkins job for "clientA":
clean package -Pbase-functionality,new-feature
where on Jenkins job for 'clientB':
clean package -Pbase-functionality
Note: as khmarbaise stated on comment below this approach has same pitfalls and should be avoided if possible.

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

Module in Maven project that is not included in a certain profile is still being built

I haven't had any issues using profiles in maven until now. We recently added a new module newModule to our maven project, and it's building when we run a profile it's not included in.
We have 2 profiles, default and coverage. Our mainModule has dependencies on backendModule and newModule, neither are included in our coverage profile, but one of them is building when we run that profile. Our maven profiles look something like this:
<profiles>
<profile>
<id>coverage</id>
<modules>
<module>mainModule</module>
</modules>
</profile>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>mainModule</module>
<module>backendModule</module>
<module>mainModuleTools</module>
<module>newModule</module>
</modules>
</profile>
</profiles>
When I use mvn clean install all 4 modules in our default profile are built. When I use mvn clean install -Pcoverage, both mainModule and newModule are being being built.
Is there a time where a module not listed in a profile is included in the build?

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.

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