Open In App

Multi-Module Project with Maven

Last Updated : 06 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Parent Module Creation

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.

Child Module1 Creation


After creating the child-module1 project, the file structure will look like the below image.

child-module1 Folder Structure

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.

Child Module2 Creation


After creating the child-module2 project, the file structure will look like the below image.

child-module2 Folder Structure

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:

parent-module Folder Structure

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:

mvn clean install

Test Result:

mvn test

Step 12: Run Child Module1

Use the following command to run Child Module1:

java -cp target/classes com.gfg.ChildModel1

Output:

child-module1 Result

Step 13: Run Child Module2

Use the following command to run Child Module2:

java -cp target/classes com.gfg.ChildModel2

Output:

child-module2 Result

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.


Next Article
Article Tags :

Similar Reads