Setting up a multi-project build in Gradle for Spring Boot

Setting up a multi-project build in Gradle for Spring Boot

How to set up a multi-project build in Gradle for Spring Boot

Creating a multi-build project is the best practice for managing shared code like utils, common, exception, security configuration, and more.
In this tutorial, we are going to learn how we can setup multi-project build in a Spring Boot application.

Configure root project

Let's create a new Java project with Gradle. We are using Intellij Idea go to File >> New >> Project . Here we need to create a plain Java project, the screen and the configuration look as follows:
Article Image
Now, delete the src folder from the root project, we don't need the code inside the src folder. We only need the Gradle build configuration.

Project Structure

The overall folder structure for a multi-project build will look like as follows.
Article Image
Here api and utils are the common projects that are utilized inside microservices. And sets of microservices inside the microservices folder.
You can create the microservices projects directly from IntelliJ IDEA or from Spring Initializer.
You can also create the api and utils project as well.

Configure settings.gradle

Now, let's configure the settings.gradle file. Here, we basically include all the sub-projects inside the parent.
java
rootProject.name = 'microservice-app'
include ':api'
include ':utils'
include 'microservices:inventory-service'
include 'microservices:payment-service'
include 'microservices:product-service'
include 'microservices:review-service'
Now reload the project. You can see the floating Gradle icon at the top right corner once the settings.gradle file is changed. Or Intellij Idea provides the Gradle button to refresh, click on it to reload, and download the dependencies of all the projects that are set up inside the parent project.
Article Image

Build the entire project

Open the terminal, navigate to the parent project, and build the entire project
bash
./gradlew build

Run a specific microservice

bash
./gradlew :microservices:product-service:bootRun

Build a specific microservice

bash
./gradlew :microservices:product-service:clean build
This setup is best practice for developing microservices in a small team that doesn't required the separte developement team for development. Also, to run multiple microservices locally.