How to Build a Spring Boot Multi‑Module Project with Maven
Learn step‑by‑step how to create a Spring Boot multi‑module Maven project, from designing the parent and child module structure, configuring pom.xml files, writing simple application and controller code, to building and running the modules with Maven commands.
Why Use Multiple Modules?
In modern software development, modular design improves maintainability and team collaboration. For a large enterprise application with features like user management, order processing, and payment, a single project becomes unwieldy. Splitting functionality into independent sub‑modules (module-a, module-b, module-c) clarifies structure and boosts efficiency.
Project Structure
A typical Spring Boot + Maven multi‑module layout looks like:
my-multi-module-project
│
├── pom.xml
├── module-a
│ └── pom.xml
├── module-b
│ └── pom.xml
└── module-c
└── pom.xmlHere my-multi-module-project is the parent project, while module-a, module-b, and module-c are child modules. Each child has its own pom.xml, and the parent’s pom.xml manages dependencies and build configuration.
Create Parent Project
The parent pom.xml defines the modules and common dependencies, for example:
<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.example</groupId>
<artifactId>my-multi-module-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module-a</module>
<module>module-b</module>
<module>module-c</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>The parent pom lists the three sub‑modules and imports Spring Boot’s dependency management.
Create Sub‑Modules
Each sub‑module needs its own pom.xml. For module-a:
<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>
<parent>
<groupId>com.example</groupId>
<artifactId>my-multi-module-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>module-a</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>The <parent> tag links the child to the parent and brings in Spring Boot dependencies.
Write Code
In module-a create a simple Spring Boot application:
package com.example.modulea;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}And a basic controller:
package com.example.modulea.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Module A!";
}
}Run the Project
Build and run the whole project with Maven:
mvn clean install
mvn spring-boot:run -pl module-aAccess http://localhost:8080/hello in a browser to see the response “Hello from Module A!”.
Conclusion
This guide demonstrated how to set up a Spring Boot multi‑module Maven project, covering project layout, parent and child pom configuration, sample code, and execution steps, helping developers apply modular design in real projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
