Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Modules + Building a Single Specific Module

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?

  1. Do I have to run mvn package for the entire project hierarchy when I really just want B?

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

like image 618
Brian Ferris Avatar asked Jul 11 '09 16:07

Brian Ferris


People also ask

What is multi-module Maven project?

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.

What is Reactor build order in Maven?

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.


2 Answers

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:

  • Define modules list which shall be build in Maven multiproject build
like image 157
Pascal Thivent Avatar answered Sep 21 '22 17:09

Pascal Thivent


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.

like image 43
Waqas Ahmed Avatar answered Sep 23 '22 17:09

Waqas Ahmed