The right gradle setup for microservices with Spring Boot

How to set up a gradle multi-project for a microservice project with Spring Boot

In that first article, I want to show you how to setup a multi-project project for a Spring Boot Application with Gradle. For this we are using a very simple business case of a stock-service that is supposed to give us the stock information.

Project setup


How to Install a JDK?

For the installation of the JDK, I would strongly recommend to use sdkman . The tool will allow you to install multiple version of the JDK on your machine and to switch the version depending on the project your are working on. After the installation we will execute the following command to install the latest JDK:

$ sdk install java 16.0.1-open

To list all available version, use the following command:

$ sdk list java

How to Install Gradle?

Gradle is a build automation tool for multi-language software development. We can install it the same way we install the SDK with sdkman. As we installed a JDK 16, we have to choose at least a gradle version 7.

$ sdk install gradle 7.1

How to Generate a Spring Project?

To setup a spring boot project, the easiest way is to use the Spring Initializr

Create a Gradle multi-project build


multimodule-gradle-spring
|-- common
  |-- common-api
    |-- build.gradle
  |-- common-impl
    |-- build.gradle
  |-- build.gradle
|-- stock-service
  |-- stock-service-api
    |-- build.gradle
  |-- stock-service-app
    |-- build.gradle
  |-- build.gradle
|-- gradle
  |-- wrapper
    |-- gradle-wrapper.jar
    |-- gradle-wrapper.properties
|-- build.gradle
|-- gradle.properties
|-- settings.gradle
|-- gradlew
|-- gradlew.bat

Gradle root configuration

There are many gradle specific files needed in the root directory of the project.

build.gradle

allprojects {
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/milestone" }
    }

    apply plugin: 'base'
    apply plugin: 'idea'
}

In that file in the root of the project, we will configure the properties specific to the whole project like the repository addresses, and the plugin common to all sub-projects.

settings.gradle

include ':common:common-api'
include ':common:common-impl'

include ':stock-service:stock-service-api'
include ':stock-service:stock-service-app'

In that file, we will list all the Gradle projects of the projects. Gradle will look into thoses projects and build them one by one.

gradle.properties

javaVersion = 16

springBootVersion = 2.5.2
springCloudVersion = 2020.0.3
springDependencyManagementVersion = 1.0.11.RELEASE

In the gradle.properties file, we will define some constants common to the whole build process. In our case, we are telling gradle to build the project on a JDK 16 and to use the Spring Boot version 2.5.2. This makes very easy to upgrade the project of a new version as everything is one place.

gradle wrapper

The Gradle wrapper is a script that invokes a declared version of Gradle, downloading it beforehand if necessary. As a result, developers can get up and running with a Gradle project quickly without having to follow manual installation processes saving your company time and money.

To update the project to a newer gradle version, execute the following command:

./gradlew wrapper --gradle-version=${gradle_version}

Stock-service project

The stock-service project contains our microservice application based on Spring Boot. The project is divided into 2 subprojects to provide better flexibility and to be able to use the domain objects in other microservices:

  • stock-service-api: contains domain objects, exceptions,… and a reference to the project common-api.
  • stock-service-app: contains the microservice implementation of the stock-service uses the common-impl-project.

A build.gradle file will be placed at the root of the project and contains the common gradle configuration for the 2 subprojects.

Common-project

The idea of the common project is to use the DRY principle. The code common to all microservices will be shared. The common-project will be divided into 2 subprojects:

  • common-api: contains the common domain objects, exceptions, …
  • common-impl: contains the common implementation classes for all our microservices.

Summary


In this article, we created a gradle multi-project baseline for a Microservice project with a microservice example, and a common project to reuse the common code across all our future microservices. The project is using a gradle wrapper to save gradle installation and centralizes all versions of the libraries to ease version upgrade of the project. The source code of the project can be found in github.