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

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.

Related

In a Maven multi-module project, how can I run a full build and run a specific plugin on one child?

I have a multi-project maven project. One child only has a specific plugin and I want it to be optional (so not bound to a specific phase).
How can I run a full clean install on the entire project and additionally run a project's specific plugin?
I've encountered this post but it looks like an overkill, and it is not so easy in my specific case.
Your best option is to use maven build profiles.
In example, use this snippet in child module:
<profiles>
<profile>
<id>only-in-child-module</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
....
</plugins>
</build>
</profile>
</profiles>
This build profile will not be active unless you explicitly ask maven for it like:
mvn clean install -Ponly-in-child-module

Maven inject plugin settings.xml

I'm currently setting up a CI/CD chain for multiple projects. I'd like to inject a maven plugin with every build of our Jenkins server (this one: https://github.com/cedricwalter/git-branch-renamer-maven-plugin). Is there a way to inject the plugin into a build without adding it to every pom.xml in every project?
You can do that with mavenSettings.xml using the profile section: https://maven.apache.org/settings.html
Then set that mavenSettings.xml to the Maven instalation used by Jenkins.
...
<profiles>
<profile>
<id>AllwaysOn</id>
<activation>
<activeByDefault>true</activeByDefault>
...
</activation>
<!-- add plugin config here -->
</profile>
</profiles>
...
Then you can add your specific plugin configuration there.

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?

How do I run selected modules using parent pom in Maven like I have

<module>APP_1</module>
<module>web_1</module>
<module>service_1</module>
<module>schema_1</module>
<module>APP_2</module>
<module>web_2</module>
<module>service_2</module>
<<module>schema_2</module>
sometimes as developer if I want to build first module with all it's depedecies only so how should I achieve this task in parent pom?
maven 3.0.3
jdk-1.6
You can use the -pl or --projects parameter of maven command-line for this.
e.g.
mvn --p APP_1
This will build APP_1 and its dependant modules.
Use profile in parent pom:
</profiles>
<profile>
<!-- Build App 1 -->
<id>app1</id>
<modules>
<module>APP_1</module>
<module>web_1</module>
<module>service_1</module>
<module>schema_1</module>
</modules>
</profile>
<profile>
<!-- Build App 2 -->
<id>app2</id>
<modules>
<module>APP_2</module>
<module>web_2</module>
<module>service_2</module>
<<module>schema_2</module>
</modules>
</profile>
</profiles>
To build App1:
mvn clean install -Papp1
To build App2:
mvn clean install -Papp2

Resources