Open In App

Spring Boot – Multi-Module Project

There comes a time while the development phase of an application when some things get complex. It becomes critically hard for managing the services, repositories, entities, etc of an application. As we know that the Spring Boot was developed for the reasons like auto-configuration, efficiency, quick development, reduce cumbersome efforts, etc. To overcome this or a similar issue, we can use Spring Boot to make the application as the Multi-Module Project. In a Multi-Module Project, an 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 as an independent project or sub-project.

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



Advantages of a Multi-Module Project

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

Working of the Multi-Module Project

The architecture of the Multi-Module Project

Creating Multi-Module Project (STS – Spring Tool Suite)

Multi-Module Project ( Parent-Module ) – Maven

ParentGFG – Parent Module

pom.xml (Configurations)




<?xml version="1.0" encoding="UTF-8"?>
         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>

Notes: 



  1. 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>’.
  2. When you will create sub-modules, the ‘modules’ tag will automatically get generated with added respective modules.
  3. 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.

GFG-Module1 (Sub-Module)

An independent GFG-Module1 ( Sub-Module ) Project – Maven

pom.xml (Configurations)




         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>

Main.java (Bootstrapping of the Application)




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);
    }
}

RestApi.java (Endpoint of the Application)




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 ( Sub-Module )

An independent GFG-Module2 ( Sub-Module ) Project – Maven

pom.xml (Configurations)




         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>

UserModel.java (User data entity class)




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

  1. GFG-Module2 (sub-module) declares an entity (Data) object.
  2. 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.


Article Tags :