Maven Tutorial
Maven in IDE
Maven aggregation is a way to manage multiple related Maven projects within a single parent project. This is particularly useful when you have a complex project that consists of several modules or subprojects that depend on each other. Using Maven aggregation, you can build, test, and deploy all modules in the correct order with a single command.
In this tutorial, you'll learn how to set up a multi-module Maven project using Maven aggregation.
1. Create a parent project
First, create a new directory for the parent project and navigate to it. Initialize the parent project by creating a pom.xml
file with the following content:
<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>parent-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <!-- List of modules will be added here --> </modules> </project>
2. Create module projects
Next, create a new directory for each module within the parent project. Initialize each module with its own pom.xml
file. Ensure that each module has a unique artifactId
and set the parent's groupId
, artifactId
, and version
in the <parent>
tag:
<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>parent-project</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>module-a</artifactId> </project>
3. Add module references to the parent project
In the parent project's pom.xml
file, add references to each module within the <modules>
tag. Use the directory names of the module projects:
<modules> <module>module-a</module> <module>module-b</module> <!-- Add more modules as needed --> </modules>
4. Define dependencies between modules (optional)
If your modules depend on one another, you can specify the dependencies in the pom.xml
file of the dependent module:
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>module-a</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
5. Build the multi-module project
To build the entire multi-module project, navigate to the parent project's directory and run the following command:
mvn clean install
Maven will build each module in the order specified in the parent pom.xml
file, taking into account any inter-module dependencies.
In conclusion, this tutorial demonstrated how to set up a multi-module Maven project using Maven aggregation. With this knowledge, you can efficiently manage and build
How to use Maven aggregation for multi-module projects:
<!-- Example: Parent POM for aggregation --> <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <modules> <module>module1</module> <module>module2</module> </modules> </project>
Configuring parent and child POMs for Maven aggregation:
<!-- Example: Child POM in a multi-module project --> <project> <parent> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>1.0.0</version> </parent> <artifactId>module1</artifactId> <!-- Other configurations --> </project>
Building and packaging aggregated projects with Maven:
# Example: Build all modules mvn clean install # Example: Build a specific module cd module1 mvn clean install
Dependencies management in Maven aggregation:
<!-- Example: Dependency management in the parent POM --> <dependencyManagement> <dependencies> <!-- Define shared dependencies --> </dependencies> </dependencyManagement>
Maven reactor and build order in aggregated projects:
# Example: Build a specific module and its dependencies cd module1 mvn clean install