SoFunction
Updated on 2025-03-02

Steps to use Maven for multi-module project management

Using Maven for multi-module project management is a common practice, which can help you organize large projects, making them more structured and easy to maintain and build. Here are the detailed steps to create and manage multi-module projects using Maven:

Step 1: Create the parent project

First, create an empty Maven project as the parent project, which will manage all child module dependencies and plugins.

Create a new project using the Maven prototype:

mvn archetype:generate -DgroupId= -DartifactId=parent-module -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Go to the project directory and edit

In parent project, set<packaging>forpom, and define<modules>Element, lists all submodules.

&lt;project&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;&lt;/groupId&gt;
  &lt;artifactId&gt;parent-module&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;pom&lt;/packaging&gt;
  &lt;modules&gt;
    &lt;module&gt;module-a&lt;/module&gt;
    &lt;module&gt;module-b&lt;/module&gt;
    &lt;!-- Other submodules --&gt;
  &lt;/modules&gt;
  &lt;!-- Dependency management --&gt;
  &lt;dependencyManagement&gt;
    &lt;dependencies&gt;
      &lt;!-- Define dependencies shared by all submodules --&gt;
    &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
&lt;/project&gt;

Step 2: Create a submodule

Create a child module in the parent project directory.

Create submodules using the command line:

mkdir module-a
cd module-a
mvn archetype:generate -DgroupId= -DartifactId=module-a -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0-SNAPSHOT -DinteractiveMode=false

Repeat the above steps to create additional submodules.

Step 3: Configure the submodule

In each submoduleIn, make sure<parent>Element points to the parent project<groupId><artifactId>and<version>

&lt;project&gt;
  &lt;parent&gt;
    &lt;groupId&gt;&lt;/groupId&gt;
    &lt;artifactId&gt;parent-module&lt;/artifactId&gt;
    &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  &lt;/parent&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;artifactId&gt;module-a&lt;/artifactId&gt;
  &lt;!-- Dependencies of submodules --&gt;
  &lt;dependencies&gt;
    &lt;!-- Submodule-specific dependencies --&gt;
  &lt;/dependencies&gt;
&lt;/project&gt;

Step 4: Build a multi-module project

Run the Maven command in the parent project directory to build the entire project.

mvn clean install

This will build each submodule in turn and make sure they all inherit the configuration of the parent project correctly.

Step 5: Manage Dependencies

In parent projectUsed in<dependencyManagement>to manage dependency versions shared by all submodules. Submodules only need to declare dependencies<groupId>and<artifactId>, without specifying a version.

&lt;dependencyManagement&gt;
  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;version&gt;2.5.4&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- Other shared dependencies --&gt;
  &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;

Sample code

Here is a simplified parent projectExample:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId></groupId>
  <artifactId>parent-module</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
    <module>module-a</module>
    <module>module-b</module>
  </modules>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.5.4</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

Each submodule'sIt may look like this:

<project>
  <parent>
    <groupId></groupId>
    <artifactId>parent-module</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>module-a</artifactId>
  <dependencies>
    <dependency>
      <groupId></groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
</project>

In this way, you can effectively manage multi-module Maven projects, ensuring consistency in dependencies and build configurations.

This is the end of this article about how to use Maven for multi-module project management. For more related Maven multi-module project management content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!