Java
javaTutorial
Maven multi-module project dependency build order management: use the -am parameter
Maven multi-module project dependency build order management: use the -am parameter

This tutorial details how to ensure that local module dependencies with non-parent-child relationships are correctly built before the main project in a Maven multi-module project. By explaining `mvn clean install -pl
Introduction: Building challenges of Maven multi-module projects
As a powerful project management tool, Maven's multi-module feature greatly simplifies the management and construction of large projects. By splitting a large project into multiple independent modules, each module can have its own responsibilities and life cycle, thereby improving code reusability and reducing coupling. However, in actual development, the dependency relationship between modules is not always a simple parent-child structure. When a main application module depends on other local, non-parent-child modules, it becomes a common challenge to ensure that these dependent modules are compiled and installed correctly before the main module is built.
Understanding the Problem: Pre-Build Requirements for Local Module Dependencies
Consider a typical scenario: you have a main application module named maven-Hell, which needs to depend on two other independent local modules aaa and ddd when compiling. These modules may be in the same Maven aggregation project (Reactor), but there is no direct parent-child inheritance relationship between them. When you do mvn clean install on the maven-Hell module, you want Maven to intelligently recognize and build aaa and ddd first, and then build maven-Hell. If these dependent modules are not pre-built into the local Maven repository, the maven-Hell build will fail because it cannot find the required dependencies.
Core of the solution: Detailed explanation of mvn -am parameters
In order to solve the above problems, Maven provides a set of powerful command line parameters, the most critical of which is the -am (also make) parameter. Combined with the -pl (projects) parameter, we can precisely control the build order of multi-module projects.
Commonly used command formats are as follows:
mvn clean install -pl <target-module-id> -am [-P <profile-id>] [-f <path-to-pom>]</path-to-pom></profile-id></target-module-id>
- clean install : This is Maven's standard lifecycle command to clean and install the project into the local Maven repository.
- -pl
(or --projects : This parameter is used to specify one or more specific modules to build.) is usually the artifactId of the module. This parameter is useful if you only want to build a certain submodule in the project, rather than the entire aggregate project. - -am (or --also-make) : This is the key parameter to solve the core problem. When used with -pl, it tells Maven to build not only the specified module, but also all other modules that this module depends on. Maven will automatically determine the correct build order based on dependencies between modules.
- -P
(or --activate-profiles : Use this parameter if your project defines specific Maven profiles (profiles) and you need to activate them at build time.) - -f
(or --file : If you do not execute the command in the root directory of the project, or need to specify a POM file with a non-default name, you can use this parameter to specify the path to pom.xml. When executing in the root directory of a multi-module project, explicit specification is usually not required.)
Practical example: building maven-Hell and its local dependencies
Suppose we have a Maven multi-module project with the following structure:
my-multi-module-project/
├── pom.xml # Parent POM, aggregates all modules ├── aaa/
│ └── pom.xml # module aaa
├── ddd/
│ └── pom.xml # module ddd
└── maven-Hell/
└── pom.xml # Main application module maven-Hell
In my-multi-module-project/pom.xml, all submodules are aggregated:
<?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>
<groupid>com.example</groupid>
<artifactid>my-multi-module-project</artifactid>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>aaa</module>
<module>ddd</module>
<module>maven-Hell</module>
</modules>
<properties>
<project.build.sourceencoding>UTF-8</project.build.sourceencoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<aaa.version>1.1.1</aaa.version>
<ddd.version>3.3.3</ddd.version>
</properties>
<dependencymanagement>
<dependencies>
<dependency>
<groupid>com.dor.lub</groupid>
<artifactid>aaa</artifactid>
<version>${aaa.version}</version>
</dependency>
<dependency>
<groupid>com.dor.dabu</groupid>
<artifactid>ddd</artifactid>
<version>${ddd.version}</version>
</dependency>
</dependencies>
</dependencymanagement>
</project>
The dependency on aaa and ddd is declared in maven-Hell/pom.xml:
<?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>
<groupid>com.example</groupid>
<artifactid>my-multi-module-project</artifactid>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupid>com.dor.hell</groupid>
<artifactid>maven-Hell</artifactid>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupid>com.dor.lub</groupid>
<artifactid>aaa</artifactid>
<version>${aaa.version}</version>
</dependency>
<dependency>
<groupid>com.dor.dabu</groupid>
<artifactid>ddd</artifactid>
<version>${ddd.version}</version>
</dependency>
<!-- Other dependencies-->
</dependencies>
</project>
Now, in order to automatically build aaa and ddd before building maven-Hell, we can execute the following command in the root directory of my-multi-module-project:
cd my-multi-module-project/ mvn clean install -pl maven-Hell -am
After executing this command, Maven's build process will be:
- Maven recognizes the maven-Hell module.
- Due to the presence of the -am parameter, Maven will parse the dependencies of maven-Hell and find that it depends on aaa and ddd.
- Maven will first perform a clean install operation on the aaa and ddd modules in the correct order (if there are dependencies between aaa and ddd, Maven will handle it) and install them into the local Maven repository.
- Finally, Maven will perform a clean install operation on the maven-Hell module.
In this way, maven-Hell can find the correct versions of aaa and ddd when building, ensuring the smooth compilation and packaging of the entire project.
Things to note and best practices
- Principle of Reactor : The effectiveness of the mvn -am command depends on Maven's reactor mechanism. All related modules must be part of the same aggregate project (i.e. declared in the
tag of the same parent POM), or at least be visible in Maven's build environment. Maven will build a dependency graph of all modules and build them in topological sorting order. - Version management : In multi-module projects, it is strongly recommended to use
in the parent POM to uniformly manage the dependency versions of all sub-modules. This avoids version conflicts and ensures that all modules use consistent dependency versions. - Inter-module dependency declaration : Ensure that each module's pom.xml correctly declares its dependencies on other local modules, including groupId, artifactId and version. This is the basis for Maven to understand dependencies.
- Applicable scenarios : This method is particularly suitable for complex microservice architectures, in which multiple service modules may depend on each other, but there is no strict parent-child module relationship. With the -am parameter, you can easily build one or more specified services and all their internal dependencies.
- Build the entire project : If you want to build all modules in the entire multi-module project, just execute mvn clean install in the root directory without the -pl and -am parameters. Maven will automatically build all modules in dependency order. -pl and -am are mainly used for local builds targeting specific modules and their dependencies.
Summarize
The mvn -pl
The above is the detailed content of Maven multi-module project dependency build order management: use the -am parameter. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20518
7
13631
4
How to configure Spark distributed computing environment in Java_Java big data processing
Mar 09, 2026 pm 08:45 PM
Spark cannot run in local mode, ClassNotFoundException: org.apache.spark.sql.SparkSession. This is the most common first step of getting stuck: even the dependencies are not correct. Only spark-core_2.12 is written in Maven, but spark-sql_2.12 is not added. SparkSession crashes as soon as it is built. The Scala version must strictly match the official Spark compiled version - Spark3.4.x uses Scala2.12 by default. If you use spark-sqljar of 2.13, the class loader cannot directly find the main class. Practical advice: Go to mvnre
The correct way to send emails in batches using JavaMail API in Java
Mar 04, 2026 am 10:33 AM
This article explains in detail how to correctly set multiple recipients (BCC/CC/TO) through javax.mail in Java, solves common misunderstandings - repeatedly calling setRecipients() causes only the first/last address to take effect, and provides a safe and reusable code implementation.
Elementary practice: How to write a simple console blog searcher in Java_String matching
Mar 04, 2026 am 10:39 AM
String.contains() is not suitable for blog search because it only supports strict substring matching and cannot handle case, spaces, punctuation, spelling errors, synonyms and fuzzy queries; preprocessing toLowerCase() indexOf() or escaped wildcard regular matching (such as .*java.*config.*) is a more practical lightweight alternative.
How to safely map user-entered weekday string to integer value and implement date offset operation in Java
Mar 09, 2026 pm 09:43 PM
This article introduces a concise and maintainable way to map the weekday string (such as "Monday") to the corresponding serial number (1-7), and use the modulo operation to realize the forward and backward offset of any number of days (such as Monday plus 4 days to get Friday), avoiding lengthy if chains and hard-coded logic.
How to generate a list of duplicate elements using Java's Collections.nCopies_Initialization tips
Mar 06, 2026 am 06:24 AM
Collections.nCopies returns an immutable view. Calling add/remove will throw UnsupportedOperationException; it needs to be wrapped with newArrayList() to modify it, and it is disabled for mutable objects.
How to use Homebrew to install Java on Mac_A must-have Java tool chain for developers
Mar 09, 2026 pm 09:48 PM
Homebrew installs the latest stable version of openjdk (such as JDK22) by default, not the LTS version; you need to explicitly execute brewinstallopenjdk@17 or brewinstallopenjdk@21 to install the LTS version, and manually configure PATH and JAVA_HOME to be correctly recognized by the system and IDE.
What is exception masking (Suppressed Exceptions) in Java_Multiple resource shutdown exception handling
Mar 10, 2026 pm 06:57 PM
What is SuppressedException: It is not "swallowed", but actively archived by the JVM. SuppressedException is not an exception loss, but the JVM quietly attaches the secondary exception to the main exception under the premise that "only one exception must be thrown" for you to verify afterwards. It is automatically triggered by the JVM in only two scenarios: one is that the resource closure in try-with-resources fails, and the other is that you manually call addSuppressed() in finally. The key difference is: the former is fully automatic and safe; the latter requires you to keep it to yourself, and it can be written as shadowing if you are not careful. try-
How to correctly implement runtime file writing in Java applications (avoiding JAR internal write failures)
Mar 09, 2026 pm 07:57 PM
After a Java application is packaged as a JAR, data cannot be written directly to the resources in the JAR package (such as test.txt) because the JAR is essentially a read-only ZIP archive; the correct approach is to write variable data to an external path (such as a user directory, a temporary directory, or a configuration-specified path).





