Spring Boot Gradle Project Example

Gradle is a powerful build automation tool that is widely used in the Java ecosystem. In this tutorial, we'll create a simple Spring Boot application using Gradle. We will cover the basic setup and structure of a Spring Boot project with Gradle.

Prerequisites

  • JDK 17 or later
  • Gradle
  • Spring Boot (version 3.2+ recommended)
  • IDE (IntelliJ IDEA, Eclipse, etc.)

Step 1: Set Up a Spring Boot Project Using Spring Initializr

Use Spring Initializr to generate a new Spring Boot project with the following configuration:

  • Project: Gradle Project
  • Language: Java
  • Spring Boot: 3.2.x
  • Dependencies: Spring Web

Download the generated project, unzip it, and open it in your IDE.

Project Structure

The basic structure of a Spring Boot project with Gradle looks like this:

my-spring-boot-app/
├── build.gradle
├── settings.gradle
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/demo/
│   │   │       └── DemoApplication.java
│   │   └── resources/
│   │       ├── application.properties
│   └── test/
│       └── java/
│           └── com/example/demo/
│               └── DemoApplicationTests.java
└── gradlew
└── gradlew.bat
└── gradle/
    └── wrapper/
        └── gradle-wrapper.properties

Step 2: Configuring build.gradle

Open the build.gradle file and review its content. This file contains the configuration needed to build your 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 = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

Explanation:

  • plugins: Defines the Gradle plugins required for the project.
    • org.springframework.boot: Spring Boot plugin.
    • io.spring.dependency-management: Plugin for managing dependencies.
    • java: Java plugin.
  • group and version: Define the group ID and version of the project.
  • sourceCompatibility: Sets the Java version compatibility.
  • repositories: Specifies the repositories for dependencies.
    • mavenCentral: Maven Central repository.
  • dependencies: Lists the project dependencies.
    • implementation 'org.springframework.boot:spring-boot-starter-web': Spring Boot Starter for building web applications.
    • testImplementation 'org.springframework.boot:spring-boot-starter-test': Spring Boot Starter for testing.
  • test: Configures the test task to use the JUnit Platform.

Step 3: Create the Application Class

Create a Java class named DemoApplication in the src/main/java/com/example/demo directory.

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Explanation:

  • @SpringBootApplication: Marks this class as the main entry point for the Spring Boot application.
  • main method: Starts the Spring Boot application.

Step 4: Create a REST Controller

Create a Java class named HelloController in the src/main/java/com/example/demo directory.

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}

Explanation:

  • @RestController: Marks this class as a REST controller.
  • @GetMapping("/hello"): Maps HTTP GET requests to the /hello endpoint to the sayHello method.
  • sayHello method: Returns a "Hello, World!" message.

Step 5: Create a Test Class

Create a Java class named DemoApplicationTests in the src/test/java/com/example/demo directory.

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }
}

Explanation:

  • @SpringBootTest: Indicates that this is a Spring Boot test.
  • contextLoads method: Tests if the Spring application context loads successfully.

Step 6: Running the Application

Using IDE

You can run the application directly from your IDE. Look for a "Run" or "Debug" option for the DemoApplication class.

Using Gradle

Alternatively, you can run the application using the Gradle wrapper. Open a terminal in the project root directory and run the following command:

./gradlew bootRun

Explanation:

  • ./gradlew: The Gradle wrapper script.
  • bootRun: A task provided by the Spring Boot Gradle plugin to run the application.

Step 7: Verify the Application

Open a web browser or a tool like Postman and navigate to the following URL to verify the application:

  • Hello Endpoint:
    • URL: http://localhost:8080/hello
    • Method: GET
    • Response: Hello, World!

You should see the "Hello, World!" message returned by the HelloController.

Conclusion

In this tutorial, you have learned how to create a simple Spring Boot application using Gradle. We covered:

  • Setting up a Spring Boot project using Spring Initializr with Gradle.
  • Configuring the build.gradle file.
  • Creating the main application class.
  • Creating a REST controller.
  • Creating a test class.
  • Running the application using an IDE and the Gradle wrapper.
  • Verifying the application by accessing a REST endpoint.

By following these steps, you can easily set up and manage Spring Boot projects using Gradle.

Comments