Maven Modules
Software Building
Module Development
Specific Module Building
Java Build Tools

Maven: build only one specific module in a multi-module project

Codemia Knowledge Hub ·

To build one module of a multi-module Maven project, run mvn install -pl module-a from the root. Add -am (--also-make) if the module depends on other modules in the same project, so Maven builds those first.

How a multi-module project is laid out

A multi-module project splits one codebase into smaller Maven projects. Each module can have its own dependencies, build instructions and goals, and inherits shared configuration from a parent project.

The parent POM usually contains no code. It uses pom packaging, declares the common configuration and lists each child module:

xml
1<project xmlns="http://maven.apache.org/POM/4.0.0"
2  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4  <modelVersion>4.0.0</modelVersion>
5  <groupId>com.example</groupId>
6  <artifactId>parent-project</artifactId>
7  <version>1.0-SNAPSHOT</version>
8  <packaging>pom</packaging>
9
10  <modules>
11    <module>module-a</module>
12    <module>module-b</module>
13  </modules>
14</project>

Each module's own pom.xml points back to the parent. That link is what applies the inherited configuration to the module:

xml
1<project xmlns="http://maven.apache.org/POM/4.0.0"
2  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4  <modelVersion>4.0.0</modelVersion>
5  <parent>
6    <groupId>com.example</groupId>
7    <artifactId>parent-project</artifactId>
8    <version>1.0-SNAPSHOT</version>
9  </parent>
10  <artifactId>module-a</artifactId>
11</project>

Building a single module with -pl

When you only work on one or two modules, you do not need to build the whole project. The -pl (--projects) option selects the module to build:

bash
mvn install -pl module-a

Here module-a identifies the module to compile or install. Maven still honours the dependencies declared in that module's pom.xml.

Building the modules it depends on with -am

If module-a depends on other modules in the project, those must be built too. -am (--also-make) builds the selected module plus every module it depends on. The related option -amd (--also-make-dependents) works in the other direction: it also builds the modules that depend on the selected one.

bash
mvn install -pl module-a -am

This builds module-a and any other modules it depends on according to the POM files.

OptionWhat it builds
-pl, --projectsOnly the specified module(s).
-am, --also-makeThe specified module(s) plus the modules they depend on.
-amd, --also-make-dependentsThe specified module(s) plus the modules that depend on them.

Things to keep consistent across modules

  • Version management: keep versions consistent when one module depends on another.
  • Dependency scoping: scope dependencies properly to avoid classpath pollution.
  • Build profiles: use Maven build profiles to adapt the build to different environments.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms