Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Multi-Module Project With Spring Boot
Next article icon

Multi-Module Project With Spring Boot

Last Updated : 04 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Multi-Module project with Spring Boot refers to a project structure where multiple modules or subprojects are organized under a single parent project. Each module can represent a distinct component, functionality, or layer of the application, allowing for better organization, maintainability, and code reuse. In simple, we can say a Spring Boot project that comprises nested Maven projects is commonly referred to as a multi-module project.

In a Spring Boot Multi-Module Project, the application is divided into multiple modules, where each module plays an important role in the certain functionality of an application. A module can be considered an independent project or sub-project. In this article, we will discuss how to create a multi-module project with Spring Boot.

Note: Multi-Module Project is just a set of multiple projects where each project has its respective function. 

Working of Spring Boot Multi-Module Project

  • Create a Parent Module
    • First, you have to create a Parent module.
    • The parent module acts as a container of sub-modules.
    • Also, you can use the Parent module for bootstrapping of the application ( main() method ).
    • Alternatively, you can also use any of the sub-module by implementing the main() method for bootstrapping the application and deleting the main() method from the parent module.
  • Configure Parent Module
    • Your Parent module should have pom packaging instead of jar and war.
    • When you create sub-modules, all of them get listed in the <modules> tag in pom.xml of the parent module.
  • Inheritance
    • All the dependencies you have enlisted in the pom.xml of the parent module will be automatically inherited by sub-modules directly.
    • Therefore, you don't need to add dependencies to sub-module's pom.xml to use them.
Multi-module-project-in-spring-boot


Steps to Create a Multi-Module Project in Spring Boot

  • Step 1: Create a simple Spring Starter Project (File -> New -> Spring Starter Project -> Next -> ( Select Dependencies ) -> Next -> Finish)
  • Step 2: Change or add <packaging>pom</packaging>
  • Step 3: Add a sub-module (Right click on Parent Module -> New -> other -> Maven -> Maven Module -> Next ( Select both checkboxes ) -> Next -> jar or war -> Finish).
  • Step 4: After creating sub-modules, the parent module will contain the folders of sub-modules, and also independent projects of the already created respective sub-modules will be created.
Project Structure


Step 1: Create the Parent Module

ParentGFG - Parent Module

Now, we will create a simple Parent module using Maven dependencies with pom packaging to the aggregate library and application modules.

pom packaging:

  <groupId>sia</groupId>
<artifactId>ParentGFG</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

pom.xml (Configurations):

XML
<?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                              https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.6.3</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>sia</groupId>     <artifactId>ParentGFG</artifactId>     <version>0.0.1-SNAPSHOT</version>     <packaging>pom</packaging>     <name>ParentGFG</name>     <description>Multi Module Project</description>     <properties>         <java.version>11</java.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-thymeleaf</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>          <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-devtools</artifactId>             <scope>runtime</scope>             <optional>true</optional>         </dependency>         <dependency>             <groupId>org.projectlombok</groupId>             <artifactId>lombok</artifactId>             <optional>true</optional>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>     </dependencies>      <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>                 <configuration>                     <excludes>                         <exclude>                             <groupId>org.projectlombok</groupId>                             <artifactId>lombok</artifactId>                         </exclude>                     </excludes>                 </configuration>             </plugin>         </plugins>     </build>      <modules>         <module>GFG-Module1</module>         <module>GFG-Module2</module>     </modules> </project> 

Note: 

  • After creating Spring Starter Project with a jar or war packaging, make sure that you add or change the 'packaging' tag with 'pom' -> '<packaging>pom</packaging>'.
  • When you will create sub-modules, the 'modules' tag will automatically get generated with added respective modules.
  • You can also remove the 'src' folder from Parent Module after implementing the main() method in any of the sub-module, as the Parent Module can also act just like a container.

Step 2: Create the Sub Modules

GFG-Module1:

Now, we will create one sub module named GFG-Module1.

GFG-Module1 Folder Structure


pom.xml (Configurations):

  • Declares a parent module of which this sub-module will be a part.
  • Information tags of this sub-module are also initialized as well.
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                               https://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <parent>     <groupId>sia</groupId>     <artifactId>ParentGFG</artifactId>     <version>0.0.1-SNAPSHOT</version>   </parent>   <artifactId>GFG-Module1</artifactId>   <name>GFG-Module1</name>   <description>GeeksforGeeks</description> </project> 


Step 3: Create Main Application Class

Main.java (Bootstrapping of the Application)

Java
package controller;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;  @SpringBootApplication public class Main {      public static void main(String[] args)     {         SpringApplication.run(Main.class, args);     } } 


Step 4: Create a REST Controller

RestApi.java (Endpoint of the Application)

  • This class uses 'Starter Web' dependency which is automatically inherited from the parent module's pom.xml.
  • This controller class accepts HTTP requests.
  • Here, the HTTP GET request is accepted by get() method.
  • This method uses an entity class that is created and declared in a sub-module ( GFG-Module2 ).
  • To import classes etc from different modules, you have to hover on the red error underline.
  • Click  -> 'fix project setup' and select the respective sub-module where you have created the class in.
Java
package controller;  import entity.UserModel; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  @RestController @RequestMapping("/get") public class RestApi {      @GetMapping public UserModel get()     {          UserModel entity = new UserModel();         entity.setId("1");         entity.setName("Darshan.G.Pawar");         entity.setEmail("geek@geek");         entity.setPincode("422 009");          return entity;     } } 

 GFG-Module2:

GFG-Module2 Folder Structure


pom.xml (Configurations)

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                               https://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <parent>     <groupId>sia</groupId>     <artifactId>ParentGFG</artifactId>     <version>0.0.1-SNAPSHOT</version>   </parent>   <artifactId>GFG-Module2</artifactId>   <name>GFG-Module2</name>   <description>GeeksforGeeks</description> </project> 


Step 5: Create Entity Class in Another Module

UserModel.java (User data entity class)

  • This class represents a user entity class, which has been used by the get() method of the RestApi.java class.
  • This class requires an important set of Getter and Setter methods of all instance variables fields.
  • This class uses 'Lombok' dependency which is automatically inherited from the parent module's pom.xml.
  • Therefore, the '@Data' annotation of the Lombok library is used to automatically generate Getter and Setter methods at runtime.
  • The annotation '@RequiredArgsConstructor' is used to automatically generate a constructor for required ( where the constrain is '@NonNull' ) or final fields of a class.
  • If a class doesn't contain fields, then the '@RequiredArgsConstructor' annotation acts as a '@NoArgsConstructor' annotation which creates zero parameter constructor.
Java
package entity;  import lombok.Data; import lombok.RequiredArgsConstructor;  @Data @RequiredArgsConstructor public class UserModel {      String id;     String name;     String email;     String pincode; } 


Output:

RestApi.java of GFG-Module1

  • GFG-Module2 (sub-module) declares an entity (Data) object.
  • This data is used in the body of the response which is returned by the RESTful API of GFG-Module1 ( sub-module ).
Default JSON body output ( UserModel object ) from @RestController

Note: You will have to run the GFG-Module1 as the main() method and the controller class is in this sub-module.

Advantages of a Multi-Module Project

  • It provides a great ability to build all sub-modules only with a single command.
  • We can run the build command from the parent module.
  • While building the application, the build system takes care of the build order.
  • Deployment of the applications gets very convenient and flexible.
  • Also, the code from the various modules across different projects can be re-used.
  • Last but not the least, with the help of Multi-Module Project architecture, we can gain benefits from the microservices architecture.

Next Article
Multi-Module Project With Spring Boot

P

pawardarshan461
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Java-Spring-Boot
Practice Tags :
  • Java

Similar Reads

    Spring Boot - EhCaching
    EhCache is an open-source and Java-based cache. It is used to boost performance. Its current version is 3. EhCache provides the implementation of the JSR-107 cache manager. Features of EhCache are given below: It is fast, lightweight, Flexible, and Scalable.It allows us to perform Serializable and O
    5 min read
    Difference Between Spring Boot Starter Web and Spring Boot Starter Tomcat
    Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
    3 min read
    Spring Boot - Starter Test
    Spring Boot is built on top of the spring and contains all the features of spring. It is becoming a favorite of developers these days because of its rapid production-ready environment, which enables the developers to directly focus on the logic instead of struggling with the configuration and setup.
    5 min read
    Exception Handling in Spring Boot
    Exception handling in Spring Boot helps deal with errors and exceptions present in APIs, delivering a robust enterprise application. This article covers various ways in which exceptions can be handled and how to return meaningful error responses to the client in a Spring Boot Project. Key Approaches
    8 min read
    Spring Boot - Project Deployment Using Tomcat
    Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because it’s a rapid production-ready envir
    5 min read
    Spring Boot - Difference Between AOP and OOP
    AOP(Aspect-Oriented Programming) complements OOP by enabling modularity of cross-cutting concerns. The Key unit of Modularity(breaking of code into different modules) in Aspect-Oriented Programming is Aspect. one of the major advantages of AOP is that it allows developers to concentrate on business
    3 min read
    Spring Boot - Difference Between AOP and AspectJ
    Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
    3 min read
    Spring Boot - Logging
    Logging in Spring Boot plays a vital role in Spring Boot applications for recording information, actions, and events within the app. It is also used for monitoring the performance of an application, understanding the behavior of the application, and recognizing the issues within the application. Spr
    8 min read
    Advantages of Spring Boot JDBC
    Spring JDBC is used to build the Data Access Layer of enterprise applications, it enables developers to connect to the database and write SQL queries and update data to the relational database. The code related to the database has to be placed in DAO classes. SpringJDBC provides a class called JdbcT
    3 min read
    Spring Boot - Packaging
    The Spring Framework was created to provide an open-source application framework to feature better infrastructure support for developing Java applications. It is one of the most popular Java Enterprise Edition (Java EE) frameworks, Spring framework helps developers in creating high-performing applic
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences