A multi-module project helps organize code into smaller parts. It makes big projects easier to manage and reuse.
Multi-module project structure in Spring Boot
Introduction
Syntax
Spring Boot
root-project/
├─ pom.xml (parent)
├─ module-web/
│ └─ pom.xml
├─ module-service/
│ └─ pom.xml
└─ module-data/
└─ pom.xmlThe root pom.xml lists all modules as children.
Each module has its own pom.xml to define dependencies and build rules.
Examples
pom.xml listing all modules.Spring Boot
<project>
<modules>
<module>module-web</module>
<module>module-service</module>
<module>module-data</module>
</modules>
</project>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>
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.
Practice
1. What is the main purpose of a multi-module project in Spring Boot?
easy
Solution
Step 1: Understand project organization
Multi-module projects break a big codebase into smaller modules for easier management.Step 2: Identify the main goal
This splitting helps teams work on parts independently and share code efficiently.Final Answer:
To split a large project into smaller, manageable parts -> Option CQuick Check:
Multi-module = smaller parts [OK]
Hint: Think of dividing work into smaller folders [OK]
Common Mistakes:
- Confusing multi-module with running multiple apps
- Thinking it removes the need for pom.xml
- Assuming it creates multiple databases
2. Which file typically manages all modules in a Spring Boot multi-module project?
easy
Solution
Step 1: Identify the build management file
In Maven-based Spring Boot projects, the parentpom.xmlcontrols all modules.Step 2: Understand file roles
Other files likeapplication.propertiesconfigure app settings, not modules.Final Answer:
parentpom.xml-> Option DQuick Check:
Parent pom manages modules [OK]
Hint: Parent pom.xml controls modules [OK]
Common Mistakes:
- Confusing application.properties with module management
- Thinking settings.gradle is used in Maven projects
- Assuming module-specific files manage all modules
3. Given a multi-module project with modules
core and web, where web depends on core, what happens if core is not built before web?medium
Solution
Step 1: Understand module dependencies
Ifwebdepends oncore, it needscorebuilt first to access its code.Step 2: Consequence of build order
Buildingwebwithoutcorecauses missing dependency errors and build failure.Final Answer:
Thewebmodule build will fail due to missing dependencies -> Option AQuick Check:
Dependent module needs dependency built first [OK]
Hint: Build dependencies before dependents [OK]
Common Mistakes:
- Assuming modules build independently
- Thinking build order does not matter
- Believing dependent modules build automatically
4. You have a multi-module Spring Boot project but the
web module cannot find classes from the core module. What is the most likely cause?medium
Solution
Step 1: Check module dependencies
Ifwebcannot findcoreclasses, it likely lacks a dependency declaration oncore.Step 2: Verify dependency declaration
Addingcoreas a dependency inweb'spom.xmlfixes class visibility.Final Answer:
Thecoremodule is not listed as a dependency inweb'spom.xml-> Option AQuick Check:
Missing dependency causes class not found [OK]
Hint: Always declare module dependencies in pom.xml [OK]
Common Mistakes:
- Ignoring dependency declarations
- Thinking application.properties affects class visibility
- Assuming Java version mismatch causes missing classes
5. In a multi-module Spring Boot project, you want to share common utility classes across modules without duplicating code. Which approach is best?
hard
Solution
Step 1: Identify code sharing strategy
To avoid duplication, create a dedicatedcommonmodule for shared utilities.Step 2: Use module dependencies
Other modules addcommonas a dependency to access shared code cleanly.Final Answer:
Create a separatecommonmodule and add it as a dependency to other modules -> Option BQuick Check:
Shared code in common module [OK]
Hint: Use a common module for shared code [OK]
Common Mistakes:
- Copying code causes duplication and maintenance issues
- Trying to put code in pom.xml (not for code)
- Using properties files to share classes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
