I have a multi-module Maven project with a parent project P
and three sub-modules A
, B
, and C
. Both B
and C
are war projects and both depend on A
.
I can type mvn compile
in P
and have all of the sub-modules properly compiled. The problem comes when I want to do operations for specific modules.
I'd like to be able to package a war for project B
, but when I run the package command from B
's directory, it complains that it can't find the dependencies for A
.
I understand from this question: Maven and dependent modules that perhaps Maven isn't really designed for this type of dependency resolution, but that begs the question of how do I package B
?
Do I have to run mvn package
for the entire project hierarchy when I really just want B
?
Do I have to install snapshots of A into my local repository every time I want to package B
?
This second scenario isn't much fun when A
is still under active development.
Any best practices here?
A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.
Reactor Sortinga project dependency on another module in the build. a plugin declaration where the plugin is another module in the build. a plugin dependency on another module in the build. a build extension declaration on another module in the build.
Any best practices here?
Use the Maven advanced reactor options, more specifically:
-pl, --projects Build specified reactor projects instead of all projects -am, --also-make If project list is specified, also build projects required by the list
So just cd
into the parent P directory and run:
mvn install -pl B -am
And this will build B and the modules required by B.
Note that you need to use a colon if you are referencing an artifactId
which differs from the directory name:
mvn install -pl :B -am
As described here:
Say Parent pom.xml contains 6 modules and you want to run A, B and F.
<modules> <module>A</module> <module>B</module> <module>C</module> <module>D</module> <module>E</module> <module>F</module> </modules>
1- cd into parent project
mvn --projects A,B,F --also-make clean install
OR
mvn -pl A,B,F -am clean install
OR
mvn -pl A,B,F -amd clean install
Note: When you specify a project with the -am option, Maven will build all of the projects that the specified project depends upon (either directly or indirectly). Maven will examine the list of projects and walk down the dependency tree, finding all of the projects that it needs to build.
While the -am command makes all of the projects required by a particular project in a multi-module build, the -amd or --also-make-dependents option configures Maven to build a project and any project that depends on that project. When using --also-make-dependents, Maven will examine all of the projects in our reactor to find projects that depend on a particular project. It will automatically build those projects and nothing else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With