Home Post System Design

How to create a Multi-Module Project in Maven

Mar 31, 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.

avatar

NK Chauhan

NK Chauhan is a Principal Software Engineer with one of the biggest E Commerce company in the World.

Chauhan has around 12 Yrs of experience with a focus on JVM based technologies and Big Data.

His hobbies include playing Cricket, Video Games and hanging with friends.

Categories
Spring Framework
Microservices
BigData
Core Java
Java Concurrency