Spring Boot Gradle Multi-Module Project

Setting up a multi-module project with Spring Boot and Gradle helps you organize your codebase into smaller, manageable modules. In this tutorial, we'll create a multi-module project for a blogging application consisting of core, common, and web modules. Each module will serve a specific purpose in the application.

Project Structure

The project structure will look like this:

blogger
├── build.gradle
├── settings.gradle
├── blogger-core
│   ├── build.gradle
│   └── src/main/java/com/example/core
├── blogger-common
│   ├── build.gradle
│   └── src/main/java/com/example/common
├── blogger-web
│   ├── build.gradle
│   └── src/main/java/com/example/web
└── gradle
    └── wrapper
        ├── gradle-wrapper.jar
        └── gradle-wrapper.properties

Step 1: Create the Parent Project

  1. Create the parent project directory:
mkdir blogger
cd blogger
  1. Create settings.gradle file:
rootProject.name = 'blogger'
include 'blogger-core', 'blogger-common', 'blogger-web'
  1. Create build.gradle file for the parent project:
plugins {
    id 'org.springframework.boot' version '3.2.0'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}

group = 'com.example'
version = '1.0.0'
sourceCompatibility = '17'

allprojects {
    repositories {
        mavenCentral()
    }
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'

    dependencies {
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testImplementation 'org.mockito:mockito-core:5.1.1'
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
    }

    test {
        useJUnitPlatform()
    }
}

Step 2: Create a Core Module

  1. Create the core module directory:
mkdir -p blogger-core/src/main/java/com/example/core
  1. Create build.gradle file for the core module:
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
}
  1. Add a simple service class:

blogger-core/src/main/java/com/example/core/GreetingService.java

package com.example.core;

import org.springframework.stereotype.Service;

@Service
public class GreetingService {
    public String greet(String name) {
        return "Hello, " + name;
    }
}

Step 3: Create a Common Module

  1. Create the common module directory:
mkdir -p blogger-common/src/main/java/com/example/common
  1. Create build.gradle file for the common module:
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
}
  1. Add a utility class:

blogger-common/src/main/java/com/example/common/StringUtils.java

package com.example.common;

public class StringUtils {
    public static boolean isNullOrEmpty(String str) {
        return str == null || str.isEmpty();
    }
}

Step 4: Create a Web Module

  1. Create the web module directory:
mkdir -p blogger-web/src/main/java/com/example/web
  1. Create build.gradle file for the web module:
dependencies {
    implementation project(':blogger-core')
    implementation project(':blogger-common')
    implementation 'org.springframework.boot:spring-boot-starter-web'
}
  1. Add a controller class:

blogger-web/src/main/java/com/example/web/GreetingController.java

package com.example.web;

import com.example.core.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @Autowired
    private GreetingService greetingService;

    @GetMapping("/greet")
    public String greet(@RequestParam String name) {
        return greetingService.greet(name);
    }
}

Step 5: Running the Application

  1. Add the main application class:

blogger-web/src/main/java/com/example/web/WebApplication.java

package com.example.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.example.core", "com.example.common", "com.example.web"})
public class WebApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
}
  1. Run the application:
./gradlew :blogger-web:bootRun

Conclusion

In this tutorial, we've set up a multi-module Spring Boot project using Gradle. Each module has a specific responsibility, making the codebase more modular and easier to maintain. You can extend this setup with more modules and functionalities as needed. For more information and best practices, refer to the Spring Boot documentation and Gradle documentation.

Comments