0
0
Spring Bootframework~5 mins

Multi-module project structure in Spring Boot

Choose your learning style9 modes available
Introduction

A multi-module project helps organize code into smaller parts. It makes big projects easier to manage and reuse.

When your project has different parts like web, service, and database layers.
When you want to share common code between different applications.
When multiple teams work on different parts of the same project.
When you want to build and test parts separately.
When you want to keep your project clean and organized.
Syntax
Spring Boot
root-project/
  ├─ pom.xml (parent)
  ├─ module-web/
  │    └─ pom.xml
  ├─ module-service/
  │    └─ pom.xml
  └─ module-data/
       └─ pom.xml

The root pom.xml lists all modules as children.

Each module has its own pom.xml to define dependencies and build rules.

Examples
This is the parent pom.xml listing all modules.
Spring Boot
<project>
  <modules>
    <module>module-web</module>
    <module>module-service</module>
    <module>module-data</module>
  </modules>
</project>
Example of a module pom.xml that refers to the parent project.
Spring Boot
<project>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>root-project</artifactId>
    <version>1.0</version>
  </parent>
  <artifactId>module-web</artifactId>
  <dependencies>
    <!-- module dependencies -->
  </dependencies>
</project>
Sample Program

This shows a simple multi-module Spring Boot project with a parent pom.xml and two modules: module-web and module-service. The web module depends on the service module.

Spring Boot
<?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.example</groupId>
  <artifactId>root-project</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <modules>
    <module>module-web</module>
    <module>module-service</module>
  </modules>
</project>

--- module-web/pom.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>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>root-project</artifactId>
    <version>1.0</version>
  </parent>
  <artifactId>module-web</artifactId>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>module-service</artifactId>
    </dependency>
  </dependencies>
</project>

--- module-service/pom.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>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>root-project</artifactId>
    <version>1.0</version>
  </parent>
  <artifactId>module-service</artifactId>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
  </dependencies>
</project>
OutputSuccess
Important Notes

Always keep the parent pom.xml packaging as pom.

Modules can depend on each other by adding dependencies with the module's artifactId.

Use this structure to build and test modules separately or together.

Summary

Multi-module projects split big code into smaller parts.

The parent pom.xml manages all modules.

Modules can share code and depend on each other easily.