Table of contents
1.
Introduction
2.
What is Multi-Module Project?
3.
Why do we need a multi-module project?
4.
Advantages of multi-module projects in Spring Boot
5.
Spring Boot Parent Module
6.
Child Modules
7.
Maven child projects/ modules
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024

Spring Boot Multi-Module Project

Author Juhi Sinha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Spring Boot is a Java-based open-source framework for developing microservices.

While creating projects, it is beneficial and simple to divide the project into multiple modules. This can be done easily with the multi-module project in Spring Boot.

In this article, we will learn about the Spring Boot Multi-Module Project. So, without any further ado, let's get started!

What is Multi-Module Project?

The multi-module project is a Spring Boot project that contains nested Maven projects. The parent project in a multi-module project serves as a container for fundamental maven configurations.

A multi-module project is made up of a parent pom that looks at the collection of submodules. The packaging type pom that makes the project an aggregator must be present in the parent maven project. The parent project's pom.xml file contains a list of all modules, common dependencies, and properties that are sent down to the child projects. The project's root directory contains the parent pom. The child modules are full-fledged Spring Boot projects that inherit the parent project's maven properties.

All of the modules are deployed together in an embedded Tomcat Server when we execute the multi-module project.

 

Why do we need a multi-module project?

If a module needs to be distributed individually, splitting the project into distinct modules is good.

A multi-module build is when we have modules that belong together, such as an ear project, which typically includes numerous others such as a client, server, ejb, war, and so on. If our modules share the same dependencies, we may provide them in the main pom, and all other modules will benefit. We won't have to specify the same requirements for each module.

Suppose we need to work on numerous projects at the same time. We make a package from all projects and ship it to a customer. Therefore we use the main pom to build them and another maven plugin to package them as zip files.

Even if the code could technically all be in the same module, it is easier to maintain track of in large projects.

Advantages of multi-module projects in Spring Boot

 

Here are some of the advantages of using Spring Boot for a multi-module project:

  • The build system handles the build order.
  • Make the application deployment simple and flexible.
  • The code from the modules can be reused in several projects.
  • Allow for the construction of all modules with a single command. From the parent module, run the build command.

Spring Boot Parent Module

The first step in creating a spring boot multi-module project is to build a simple parent project. This parent module has a pom.xml file, which will provide the following information:

  • All of the modules are listed here.
  • A list of all modules' shared dependencies.
  • All modules have the same setup.

 

We'll also include the spring-boot-starter-parent dependency in the Spring Boot application. It is the parent POM for Spring Boot-based applications, and it manages dependencies and plugins. This step is optional, but it is highly recommended for a Spring Boot application. We have two possibilities for creating the parent project:

  • Manually create the pom.xml file.
  • Use the Maven archetype for a quick start.

 

To create the parent project, let's discuss the second option:

mvn archetype:generate -DgroupId=com.javadevjournal
       -DartifactId=spring-boot-multi-module-project
       -DarchetypeArtifactId=maven-archetype-quickstart
       -DinteractiveMode=false

 

Once we run the above command, maven will construct a structure along with the pom.xml file. For this parent module, change the packaging type to pom. The completed pom.xml file looks like this:

 

<?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>org.ninjas</groupId>
 <artifactId>multi-module-project</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>pom</packaging>
 <name>spring-boot-multi-module-project</name>
 <url>https://www.ninja.com</url>
 <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.2.4.RELEASE</version>
 </parent>
 <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>
     <java-version>1.8</java-version>
 </properties>
 <modules>
     <module>module1</module>
     <module>module2</module>
 </modules>
 <dependencies>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter</artifactId>
     </dependency>
 </dependencies>
</project>

 

Spring-boot-starter is defined as a global dependent in our pom.xml file. Within our project, we'll make two directories. These two directories represent the sub-modules defined in the parent pom.xml file:

 

<modules>
     <module>jdj-core</module>
     <module>jdj-web</module>
</modules>

Child Modules

Let's start by making child modules. We can define any child module. The module structure depends on specific project requirements and has no boundaries or restrictions. The following structure is being defined:

  • The jar packaging is included in this module.
  • Our core module is reliant on a web module.

Maven child projects/ modules

The child modules are separate Maven projects that share the parent project's attributes.

Because it is contained within a parent project, all child projects may be built with a single command. The relationship between the projects is easier to define.

Must Read Spring Tool Suite

FAQs

  1. What is Spring Boot?
    Spring Boot is a Java-based open-source framework for developing microservices. Pivotal Team created it, and it is used to create stand-alone and production-ready spring applications.
     
  2. In Spring Boot, what is a multi-module project?
    The multi-module project is a Spring Boot project that contains nested Maven projects. The parent project in a multi-module project serves as a container for fundamental maven configurations. A multi-module project is made up of a parent pom that oversees a collection of submodules.
     
  3. What are the advantages of a multi-module project in Spring Boot?
    The advantages of using Spring Boot for a multi-module project are as follows:
    The build system handles the build order.
    Make the application deployment flexible and straightforward.
    The code from the modules can be reused in several projects.
    Allow for the construction of all modules with a single command. From the parent module, run the build command.

Key Takeaways

We’ve learned about the Spring Boot multi-module project in this article. 

You can also check our previous blogs on STS Download, Introduction to Spring BootSpring Boot Auto-configuration, Spring Boot Annotations, and Spring Boot CLI. You can also consider our Spring Boot Course to give your career an edge over others.

 

Thank you for reading!

 

Live masterclass