Maven Build specific childs from Parent - maven

Can we build specific child from a parent Pom in Maven.May be using a file that mentions the name of the modules to be included.

If you in root location of your project you can simply define that on command line of Maven like this:
mvn -pl ModuleYouWouldLikeToBuild package
Excerpt from the command line help:
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path.
If this module what you like to build is used by an other module you can decide to let maven analyze which one and build that depending module also by using:
mvn -pl Module --also-make-dependents
or short version:
mvn -pl Module -amd
If you have modules which are used by the module you triggered to build you can also add:
mvn -pl Module --also-make
or short version:
mvn -pl Module -am

Assume your root project has 3 modules: module-A, module-B and module-C.
Run the commands below from your root project.
If you want to build module-C, run: mvn clean install –pl module-C
In case module-C depends on module-A, run:
mvn clean install –pl module-C –am to build module-A and module-C

You can use maven profile to achieve this.
<project>
...
<profiles>
<profile>
<id>build1</id>
<activation>
<property>build1</property>
</activation>
<modules>
<module>module1</module>
</modules>
</profile>
<profile>
<id>build2</id>
<activation>
<file>
<exists>test2.file</exists>
</file>
</activation>
<modules>
<module>module2</module>
<module>module3</module>
</modules>
</profile>
</profiles>
</project>
You can activate profile by some conditions, eg. present or missing files, existing properties and so on.
More info about maven profile:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html

Related

maven command line for running multiple profile

i have 2 profiles in my pom.xml and need to package 2 WAR files simultaneously
this is pom
<profiles>
<profile>
<id>TEST</id>
-- scripts
</profile>
<profile>
<id>DEV</id>
-- scripts
</profile>
</profiles>
i tried
clean package -P Dev,Test
but alywas the generated WAR is the last one (Test) and Dev profile not run
You can use multiple P arguments:
mvn clean package -P Dev -P Test
If you want to do that, you need to separate Maven runs.
After a lot of investigation i figure out it can't be done

How to activate a profile in a maven submodule using a property?

I am using Maven 3.6.0 and OpenJDK8 on Ubuntu 18.04 (also tested with Alpine Linux).
I have a pom.xml in the root of my project that includes my submodules :
...
<modules>
<module>mysubmodule</module>
</modules>
...
In the mysubmodule folder, the pom.xml has a profile that I want to activate based on a property passed to the mvn executable:
...
<profile>
<id>my-profile</id>
<activation>
<property>
<name>activateMyProfile</name>
</property>
</activation>
...
</profile>
...
I then execute mvn to start the build, but the profile is never activated:
If I run mvn -DactivateMyProfile release:prepare from the root of my project, the profile is never activated and never runs
If I run mvn release:prepare from the root of my project, the profile is never run.
I also tried the inverse:
...
<profile>
<id>my-profile</id>
<activation>
<property>
<name>!doNotActivateMyProfile</name>
</property>
</activation>
...
</profile>
...
If I run mvn -DdoNotActivateMyProfile release:prepare from the root of my project, the profile is still executed
If I run mvn release:prepare from the root of my project, the profile is also executed
It looks like mvn is not able to see the properties being passed through the command line. What is the correct way to activate a profile in a submodule using a property?
As I am using the maven release plugin, parameters must be passed using the -Darguments argument.
For example, instead of using mvn -DactivateMyProfile release:prepare, the correct invocation is: mvn -Darguments=-DactivateMyProfile release:prepare
If there are multiple arguments, use mvn -Darguments="-DactivateMyProfile -DsomeOtherArg -DanotherArg=something" release:prepare

Maven says it cannot find something in the "reactor"

I have a maven project and I'm using this --projects command from the root of the project to run several pom files.
mvn clean install --projects proj1, then-proj2
The proj1 and then proj2 are the artifact ids of the projects I want to run. That is, I go to the pom.xml for each project and use the "artifact id" there.
Both proj1 & then-proj2 are themeslves sub modules of projects in the main pom file.
Say proj1 is in A, and then-proj2 is in B, and both A & B are in the main pom file.
When I run this, maven tells me: "could not find the selected project in the reactor: proj1".
This is really frustrating - why not tell me what all the projects in the reactor are? Anyway, what am I doing wrong?
If you only use the artifactId's of the given project you have to define that correctly on command line:
help output of Maven (mvn --help)
Comma-delimited list of specified reactor projects to build of all
projects. A project can be specified by [groupId]:artifactId or by its
relative path
This means in your case you have to define:
mvn clean install --projects :proj1,:then-proj2
Try this:
mvn clean install -pl A/proj1, B/then-proj2
check this out: https://stackoverflow.com/a/23076358/1680793
Another thing is to make sure that you have listed your child projects inside the
<modules>
<module>
sections of the corresponding parent multimodule projects.
For example in the below project structure:
main
A
proj1
proj1A
B
then-proj2
Let's say you are trying to build proj1A. When you try
mvn package -pl A/proj1/proj1A
from the main's pom directory you will still have this same reactor error if you don't have:
"A" as a module in "main", or
"proj1" as a module in "A", or
"proj1A" as a module in "proj1"
If your modules are distinguished based on profiles then make sure to consider profiles too. Eg:
mvn -P profile1 -pl relative/path/to/project1 clean install
For this kind of setting, the pom would be:
<profiles>
<profile>
<id>profile1</id>
<modules>
<module>project1</module>
</modules>
</profile>
<profile>
<id>profile2</id>
<modules>
<module>project2</module>
</modules>
</profile>
</profiles>
Not mentioning profile would also give the Not Found in reactor.
A solution that worked for me, run the below command from the root directory.
mvn clean install --projects :projA, :projB -am
assuming that projB is dependent on projA
-am
If project list is specified, also
build projects required by the
list
The command used by you :
mvn clean install --projects 'submodule1','submodule2`
works on the sub-modules submodule1 and submodule2 specified in the pom.xml of the module where you're executing this command.
The guide to Working with Multiple modules shall help you understand the reactor and its sorting order. A general structure of the module for such use case would look like:
<groupId>stackoverflow</groupId>
<artifactId>mainmodule</artifactId>
<packaging>pom</packaging>
<version>1.2.3</version>
<modules>
<module>submodule1</module>
<module>submodule2</module>
... others
</modules>
... other tags

How to exclude a single module from Javadoc generation in a Maven multi module project?

I have a Maven multi module project like this:
foo-parent
foo-module
foo-module-new
foo-other-module1
foo-other-module2
pom.xml
And when calling mvn javadoc:aggregate in foo-parent, I want to exclude the module foo-module from Javadoc generation.
Exclusion by package name with the parameter excludePackageNames doesn't work in my case because foo-module and foo-module-new have the same package name.
Launch the following to generate javadocs only for foo-other-module1 and foo-other-module2 modules (note: always include the aggregator module, in this case foo-parent):
mvn javadoc:aggregate -pl :foo-parent,:foo-other-module1,:foo-other-module2
Since Maven 3.2.1, you can also exclude modules from your reactor, so the above simplifies to
mvn javadoc:aggregate -pl '!foo-module'
Use maven profile.
add in your pom :
<profiles>
<profile>
<id>javadoc</id>
<modules>
<module>module_include_javadoc1</module>
<module>module_include_javadoc2</module>
</modules>
</profile>
</profiles>
Run this:
mvn javadoc:aggregate -Pjavadoc

How can I exclude a project from a mvn clean install? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I exclude certain modules from a maven build using the commandline
I am running a maven clean install in a pom file which includes several modules (and sub-modules). I was wondering if it is possible to run a maven build but specifying on command line to skip a module from the build ( at the moment I exclude them manually from the build, but Id prefer to do it via command line).
I know that with -pl you can selectively choose projects, but what I would like is to selectively exclude (in a blacklist fashion) some.
You could have a separate <modules> section in a profile, and activate the profile you need in the command line.
Example:
<profiles>
<profile>
<id>profile-1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>...</modules> <!-- module set 1 -->
</profile>
<profile>
<id>profile-2</id>
<modules>...</modules> <!-- module set 2 -->
</profile>
</profiles>
Now, dependent on your current need, execute
mvn install
mvn install -P profile-2
Note that you'd have to think it over carefully, there must be no cross-profile dependencies on the excluded module.

Resources