Backend Development 6 min read

Using Maven -pl and -am to Build Specific Modules in Multi‑Module Projects

This article explains how to efficiently package Maven multi‑module projects by leveraging the -pl and -am command‑line options, showing dependency relationships, pom.xml configurations, and practical examples that reduce build time compared to packaging each module separately.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Using Maven -pl and -am to Build Specific Modules in Multi‑Module Projects

When a Maven project consists of several inter‑dependent modules, building each module individually can be time‑consuming; Maven provides a single‑command solution using the mvn clean package -pl and -am options.

The parent project P contains three sub‑modules A , B , and C . Module A depends on B and C , while B depends on C . The parent pom.xml lists the modules as:

<modules>
  <module>A</module>
  <module>B</module>
  <module>C</module>
</modules>

Each sub‑module’s pom.xml declares its dependencies, for example module A includes:

<dependency>
  <groupId>xxx.xxxx</groupId>
  <artifactId>B</artifactId>
  <version>xxxx</version>
</dependency>

To build only module A (and its required dependencies) you can run:

mvn clean package -pl A -am -P test -DskipTests=true

The key parameters are:

Parameter

Description

-pl

Specifies the projects to build (comma‑separated list of artifactIds).

-am

Also builds the modules that the specified projects depend on.

-amd

Builds modules that depend on the specified projects.

-N

Do not recurse into sub‑modules.

Examples:

mvn clean install -pl A -am builds P , A , B , and C because A depends on the latter two.

mvn clean install -pl C -am builds only P and C (since C has no dependencies).

mvn clean install -pl C -amd builds P , C , and any modules that depend on C (e.g., B , A ).

mvn clean install -N builds only the parent project P without its sub‑modules.

Understanding and using these Maven options can dramatically speed up builds for large projects, often cutting build time by more than half.

Finally, the author promotes a PDF book on MyBatis fundamentals and invites readers to request it via a WeChat keyword.

JavamavenCommand LinebuildDependencyMulti-Module
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.