INTRODUCTION
The mechanism in Maven 4 that handles multi-module projects is referred to as the reactor. This part of the Maven core does the following:
- Collect all the available modules to build
- Sort the modules into the correct build order
- Select which modules to build
- Build the selected modules in order
Module collection starts from one aggregator project. That project defines the modules of which it consists using the <modules>
element. This is a recursive process, so aggregators can have child modules that are aggregators themselves. A multi-module project is built from an aggregator pom that manages a group of submodules.
The child modules are regular Maven projects, and they can be built separately or through the aggregator pom.
Build only one module out of a multi-module project
This solution shows you how to build a specific Maven module in a multi-module Maven project. It may be useful to reduce the build time during development, and it’s particularly useful when the project you are working on has lots of modules and it takes a long time to finish the build.
Let’s first understand how we can build a module out of a multi-module project with an example and the same example we will use in further sections. Let’s say the project structure would look like this:
ParentProject |- pom.xml |-----------> CommonLibraries | |-> pom.xml |-----------> APIs | |-> pom.xml
where the APIs module is dependent on the CommonLibraries module.
In the ParentProject
pom.xml, add all the submodules inside the modules
section:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- parent pom --> <groupId>com.test</groupId> <artifactId>ParentProject</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>ParentProject</name> <!-- sub modules --> <modules> <module>CommonLibraries</module> <module>APIs</module> </modules> </project>
and in the individual submodule CommonLibraries pom.xml, add the ParentProject in the parent
section:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- parent pom --> <parent> <artifactId>ParentProject</artifactId> <groupId>com.test</groupId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.test</groupId> <artifactId>CommonLibraries</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> </project>
Similarly, for submodule APIs pom.xml. Also, add dependency of CommonLibraries in it.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- parent pom --> <parent> <artifactId>ParentProject</artifactId> <groupId>com.test</groupId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.test</groupId> <artifactId>APIs</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.test</groupId> <artifactId>CommonLibraries</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
To build only the CommonLibraries module, follow the below-mentioned steps.
First, cd
into the ParentProject directory and then run the command:
mvn -pl CommonLibraries clean install
Or, use the below command
mvn -f CommonLibraries/pom.xml clean install
-f,--file Force the use of an alternate POM file. -pl,--projects Comma-delimited list of specified reactor projects to build insteadof all projects. A project can be specified by [groupId]:artifactId or by its relative path.
Building a module with its dependencies in a multi-module Maven project
As mentioned earlier, the APIs module is dependent on the CommonLibraries module.
The trick here is to use Maven advanced reactor options, particularly the following options:
mvn clean install -pl APIs -am
-am, –also-make Builds the specified modules, and any of their dependencies in the reactor.
You can customize the CI/CD tools like Jenkins to build only specific modules and their dependent modules using this command in the pipeline.
-am
option only work with-pl
option. It won’t work with other options like-f
.
This solution makes it possible to decrease the build time, thus leading to decreased development time too, which may be a good way to make the development faster and cheaper.
Very helpful
Nicely explained. we can also user “verify” option instead of “install” option in Maven 4.