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.
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=trueThe 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
