How to create a Multi-Module Project in Maven
- 4.1/5
- 5374
- Jul 20, 2024
A multi-module project in Maven is built from an aggregator POM that manages a group of sub-modules.
In most cases, the aggregator is located in the project's root directory and must have packaging of type "pom".
Let's create a maven project "rabbit-mq-java" having two modules "producer" and "consumer".
mvn archetype:generate -DgroupId=com.codeburps -DartifactId=rabbit-mq-java -DinteractiveMode=false
Once the parent project is generated, we have to open the pom.xml file located in the parent's directory and add the packaging as "pom":
<packaging>pom</packaging>
The parent maven project is of packaging type "pom" and acts as an aggregator - it won't produce other artifacts.
cd rabbit-mq-java mvn archetype:generate -DgroupId=com.codeburps -DartifactId=producer -DinteractiveMode=false mvn archetype:generate -DgroupId=com.codeburps -DartifactId=consumer -DinteractiveMode=false
The information about all the submodules will be added in the parent-project's pom.xml:
<modules> <module>producer</module> <module>consumer</module> </modules>
The information about the parent-project will be added in each of the submodules' pom.xml:
<parent> <groupId>com.codeburps</groupId> <artifactId>rabbit-mq-java</artifactId> <version>1.0-SNAPSHOT</version> </parent>
In order to package, add the following properties in the parent-project's "pom.xml":
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties>
By building the project through the aggregator POM, each project that has a packaging type different from "pom" will result in a built archive file.
% mvn package
Child projects are independent maven projects but inherit properties from parent project.