A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/advance-java/multi-module-project-with-maven/ below:

Multi-Module Project with Maven - GeeksforGeeks

Multi-Module Project with Maven

Last Updated : 23 Jul, 2025

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: Key Terminologies 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:

Click on the Create button.

Step 2: Create the Child Module1

Create another Maven archetype project using IntelliJ IDEA with the following options:

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="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://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:

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="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://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="https://maven.apache.org/POM/4.0.0"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4