Design and Build REST Multi-Module Spring Boot Project

This article explains how to create multi module Spring Boot project with different approaches. We will consider the usage of Spring Initializr, IntelliJ 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 principals we can achieve clean architecture of the application. One of principals is Dependency Inversion. This principal advices to avoid linking for application units without additional layer. Let us consider with 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 > Module > New.

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.

Lest consider Spring Boot in Java 11 modules structure. In the following subheading you can find information with more detail.

Project File Structure

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

Example of Multi Module Maven Project Spring Boot

In the following file blow you can find a basis structure of main pom. As you can see, 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 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 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 project.

In the file blow you can find 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

Leave a Comment