Multi-Module Project with Maven
Last Updated :
06 Aug, 2024
A multi-module project in Maven allows you to manage a collection of related projects in a single build. This approach is particularly useful for large applications where different modules have distinct functionalities but need to be managed, built, and deployed together. By using the parent POM (Project Object Model), you can streamline dependency management, version control, and build processes.
In a multi-module Maven project, you have a parent POM that manages the configurations and dependencies for the entire project. Each module within the project has its own POM file and can be built independently or as part of the larger project. The parent POM typically resides in the root directory, while the modules are in subdirectories.
Key Benefits:
- Centralized Dependency Management: Manage dependencies in one place.
- Modularization: Break down the project into smaller, manageable parts.
- Reusability: Share common code across different modules.
- Consistent Build Process: Use a single command to build all modules.
Key Terminologies
- Parent POM (Parent Project Object Model): The POM file that acts as the parent for one or more child modules, centralizing configurations and dependencies inherited by the child modules.
- Child Module: A sub-project of the parent POM, each with its own POM file and directory structure.
- Module: A sub-project within a multi-module Maven project. Each module has its own POM file and is treated as a separate Maven project.
- Packaging: The type of artifact that Maven will produce, like JAR, WAR, and POM.
- Dependency Management: The section in the parent POM that specifies the versions and configurations used in the child modules.
- Plugin Management: The section in the parent POM that defines the plugin configurations, versions, and settings used by the build process.
- Plugin: A tool or extension that performs specific tasks during the build process.
- Artifact: The package output of the Maven build, such as a JAR, WAR, or ZIP file
Implementation of Multi-Module Project with Maven
Step 1: Create the Parent Module
Create a Maven archetype project using IntelliJ IDEA with the following options:
- Name: parent-module
- JDK: 17
- Archetype: maven-archetype-quickstart
Click on the Create button.
Step 2: Create the Child Module1
Create another Maven archetype project using IntelliJ IDEA with the following options:
- Name: child-module1
- JDK: 17
- Archetype: maven-archetype-quickstart
Click on the Create button.
After creating the child-module1 project, the file structure will look like the below image.
Step 3: Create the ChildModule1 Class
Java
package com.gfg;
/**
* Hello world!
*
*/
public class ChildModule1
{
public static void main( String[] args )
{
System.out.println( "Hello Child Module 1!" );
}
}
pom.xml
for child-module1
:
XML
<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.gfg</groupId>
<artifactId>child-module1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>child-module1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Step 4: Create the Child Module2
Create another Maven archetype project using IntelliJ IDEA with the following options:
- Name: child-module2
- JDK: 17
- Archetype: maven-archetype-quickstart
Click on the Create button.
After creating the child-module2 project, the file structure will look like the below image.
Step 5: Create the ChildModule2 Class
Java
package com.gfg;
/**
* Hello world!
*
*/
public class ChildModule2
{
public static void main( String[] args )
{
System.out.println( "Hello Child Module2!" );
}
}
pom.xml
for child-module2
:
XML
<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.gfg</groupId>
<artifactId>child-module2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>child-module2</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Step 6: Parent File Structure
After setting up the child modules, the parent file structure should look like this:
Step 7: Add Spring Starter to parent pom.xml
Add the Spring starter dependency to the parent pom.xml
:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>
Step 8: Add Maven Plugin
Add the Maven compiler plugin to the parent pom.xml
:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Step 9: Add the Child Modules
Add the child modules to the parent pom.xml
:
<modules>
<module>child-module1</module>
<module>child-module2</module>
</modules>
Parent pom.xml:
XML
<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.gfg</groupId>
<artifactId>parent-module</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent-module</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>child-module1</module>
<module>child-module2</module>
</modules>
<dependencies>
<!-- Dependencies defined here are inherited by child modules -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Step 10: Create the ParentModule Class
Java
package com.gfg;
/**
* Hello world!
*
*/
public class ParentModule
{
public static void main( String[] args )
{
System.out.println( "Hello Parent Module!" );
}
}
Step 11: Install the Maven
Use the following command to install the Maven of the parent module:
mvn clean install
Output:
Test Result:
Step 12: Run Child Module1
Use the following command to run Child Module1:
java -cp target/classes com.gfg.ChildModel1
Output:
Step 13: Run Child Module2
Use the following command to run Child Module2:
java -cp target/classes com.gfg.ChildModel2
Output:
Conclusion
Setting up a multi-module project with Maven allows for better management of complex projects by dividing them into smaller, more manageable modules. This approach enhances modularity, reusability, and simplifies the build process, making it easier to maintain and develop large applications.
Similar Reads
Multi-Module Project With Spring Boot
Multi-Module project with Spring Boot refers to a project structure where multiple modules or subprojects are organized under a single parent project. Each module can represent a distinct component, functionality, or layer of the application, allowing for better organization, maintainability, and co
6 min read
How to Use Dagger in a Multi-Module Project in Android?
We know about the Dagger dependency injection framework and how to use it in a single module project. Dagger is basically used for reducing or completely getting rid of the unnecessary boilerplate code. We have already seen step-by-step usage of the dagger library in android, check it here. But in t
6 min read
Adding JGit to the project with Gradle
JGit is the lightweight and pure Java library implementing the Git version control system. It can be widely used for integrating Git functionalities into Java applications without relying on the native Git command line tools. In this article, we will explore the process of adding JGit to the Gradle
3 min read
Building a Java Project with Maven
Maven is one of the most popular build automation tools. It is developed using the Java programming language and is primarily used for Java-based projects. However, it has been updated to support programming languages like C#, Ruby, and more. This article will teach us how to build a Java project wi
2 min read
Running a Single Test or Method With Maven
Running a single test or method using Maven is straightforward and can be accomplished using Maven commands. By default, Maven runs all tests in your project, but you can specify a single test class or even a single test method to execute. In this article, we will learn how to run a single test or m
3 min read
What is Project Management Maturity Model (PMMM)?
The Project Management Maturity Model (PMMM) is a framework that assesses the maturity and capability of an organization's project management processes. By providing a structured path for improvement, PMMM helps organizations achieve higher levels of efficiency, effectiveness, and consistency in man
11 min read
How to use External Modules and NPM in a project ?
Need for External Modules: For a large JavaScript application, it becomes difficult and messy to write the whole code in just one JavaScript file. This is where CommonJS comes into the picture and this CommonJS format defines a module format that can be used up for breaking your JS application into
3 min read
Maven - Build & Test Project
Maven is a build automation tool used for Java projects. It simplifies the process of project building, dependency management, and project documentation. This guide will walk you through the steps of building and testing a Maven project. Key Concepts:Project Object Model: It is the heart of the Mave
3 min read
Skipping Tests with Maven
Skipping tests during the Maven build process is a common requirement, especially in scenarios where tests are time-consuming or not applicable to the current build phase. Skipping tests in Maven can be achieved in several ways depending on the requirements. In this article, we will explain about sk
3 min read
Adding JGit to the Project with Maven
Integrating the JGit with Maven not only enhances the project version control capabilities but also streamlines workflow processes by leveraging the Maven build and dependency management system. This article will guide you through the steps of adding JGit to the Maven project and enabling you to har
3 min read