Design and Build REST Multi-Module Spring Boot Project

This article explains how to create a multi-module Spring Boot project with different approaches. We will consider the usage of Spring Initializr, IntelliJ’s new module creation, and how to create a new module in Eclipse.

What Benefits We Achieve in Multi-Module Project

By following the best practices in programming, especially SOLID principles we can achieve clean architecture of the application. One of the principles is Dependency Inversion. This principal advice is to avoid linking for application units without an additional layer. Let us consider in more detail this principal.

How to Create Multi Module Maven Project in IntelliJ

There is a simple approach to creating a new module in IntelliJ IDEA. In the picture below, you can find the menu by following the path: Root > New > Module.

By selecting this menu, the IDE will create a new module. This module will consist of a source folder and a basic pom.xml file.

Let’s consider Spring Boot in Java 11 modules structure. In the following subheading, you can find information in more detail.

Project File Structure

Each time when you create a new module it will consist of the following folders: src > main > java and pom.xml file.

Example of Multi Module Maven Project Spring Boot

In the following file below you can find a basic structure of the main pom. As you can see, the main app consists of sub-modules.

<?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>

    <groupId>com.restapi</groupId>
    <artifactId>restapi-prototype</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modules>
        <module>event-service-rest</module>
        <module>event-service-api</module>
        <module>event-service-dto</module>
        <module>event-service-impl</module>
    </modules>

</project>

Sample of Main Spring Boot Module

As you can see that the module consists of several significant units. See the list below.

  • Inherited parent dependency restapi-prototype. This unit loads all maven dependencies from the parent starter unit.
  • Java compiler version. You can obviously set the compiler version by properties: maven.compiler.source, maven.compiler.target.
  • Dependencies unit. In this unit, you can include all the used units of your project. It means to include your own modules or general units provided as maven dependency.
  • Spring Boot Maven Plugin. This module is responsible to build an executing jar file of the project.

In the file below you can find the whole pom.xml file as the sample:

<?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>
    <parent>
        <groupId>com.restapi</groupId>
        <artifactId>restapi-prototype</artifactId>
        <version>0.1.0</version>
    </parent>
    <artifactId>event-service-rest</artifactId>
    <version>0.1.0</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.restapi</groupId>
            <artifactId>event-service-impl</artifactId>
            <version>0.1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

#How in Spring Boot to Create Common Library, #Spring Boot multi-module project

#Java modules in Spring Boot, #How to Create Spring Boot Library

#How to Create Multi-Module Spring Boot Project in Eclipse

Related posts

Leave a Comment