Backend Development 12 min read

Spring Boot Multi‑Module Maven Project Tutorial

This article demonstrates how to create a Spring Boot multi‑module Maven project, covering the setup of an aggregate parent pom, creation of sub‑modules, implementation of controller, service, repository, and entity layers, configuration files, packaging, and deployment steps.

Java Captain
Java Captain
Java Captain
Spring Boot Multi‑Module Maven Project Tutorial

Compared with a traditional monolithic project, using Maven multi‑module configuration helps split the project into reusable modules, prevents the POM from becoming overly large, and allows building individual modules without rebuilding the whole project.

The tutorial walks through constructing a Spring Boot application in a Maven environment using a multi‑module approach.

1. Create the Aggregating Parent Project

Generate a Maven project with Spring Initializr, keep only the pom.xml , and declare the child modules in the parent POM.

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!-- Basic information -->
    <description>SpringBoot 多模块构建示例</description>
    <modelVersion>4.0.0</modelVersion>
    <name>springboot-integration</name>
    <packaging>pom</packaging>
    <!-- Parent Spring Boot pom -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath/>
    </parent>
    <!-- Declare modules -->
    <modules>
        <module>mm-web</module>
        <module>mm-service</module>
        <module>mm-repo</module>
        <module>mm-entity</module>
    </modules>
    <!-- Dependency management -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.hehe</groupId>
                <artifactId>mm-web</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            ... (other module dependencies) ...
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.42</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2. Create Sub‑Modules

Using IDEA (or Eclipse), create modules mm-web , mm-service , mm-repo , and mm-entity , each inheriting the parent POM.

2.1 mm‑web (Web Layer)

Example pom.xml for the web module:

<?xml version="1.0" encoding="UTF-8"?>
<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.hehe</groupId>
    <artifactId>mm-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>mm-web</name>
    <parent>
        <groupId>com.hehe</groupId>
        <artifactId>springboot-integration</artifactId>
        <version>1.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.hehe</groupId>
            <artifactId>mm-service</artifactId>
        </dependency>
        <dependency>
            <groupId>com.hehe</groupId>
            <artifactId>mm-entity</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Key classes:

@SpringBootApplication
public class MmWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(MmWebApplication.class, args);
    }
}

@RestController
@RequestMapping("/user/*")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("list")
    public R list() {
        try {
            return R.isOk().data(userService.list());
        } catch (Exception e) {
            return R.isFail(e);
        }
    }
}

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/socks?useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

2.2 mm‑service (Business Layer)

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userRepository;

    @Override
    public List
list() {
        return userRepository.findAll();
    }
}

2.3 mm‑repo (Data Access Layer)

public interface UserRepository extends JpaRepository
{ }

2.4 mm‑entity (Entity Layer)

package com.hehe.integration.common;

import java.io.Serializable;

public class R
implements Serializable {
    private static final long serialVersionUID = -4577255781088498763L;
    private static final int OK = 0;
    private static final int FAIL = 1;
    private static final int UNAUTHORIZED = 2;
    private T data;
    private int status = OK;
    private String msg = "";
    // static factories, getters, setters omitted for brevity
}

@Entity
@Table(name = "T_USER")
public class User {
    @Id
    @Column(name = "USERID")
    private String userId;
    @Column(name = "USERNAME")
    private String username;
    @Column(name = "PASSWORD")
    private String password;
    // getters & setters
}

3. Run the Project

Download the sample repository, open the springboot-integration project, configure a MySQL database named socks with a t_user table, then run the MmWebApplication class. Access http://localhost:8080 to see the user list.

4. Operations & Deployment (Multi‑Module Packaging)

4.1 Add Packaging Plugin

Only the startup module ( mm-web ) needs the spring-boot-maven-plugin to produce an executable JAR; other modules remain plain JARs.

<build>
    <plugins>
        <plugin>
            <!-- Build executable JAR -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

4.2 Package the Project

Run mvn clean package on the parent project. Maven builds each module and places the artifacts in their respective target directories.

4.3 Start the Application

cd mm-web/target
java -jar mm-web-0.0.1-SNAPSHOT.jar

The application starts, and you can verify the REST endpoint by visiting http://localhost:8080/user/list .

backendJavamavenSpringBootTutorialMulti-Module
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.