bootRun
task provided by the Spring Boot Gradle plugin. This guide will show you how to set up and run a Spring Boot application using Gradle.Prerequisites
- JDK 17 or later
- Gradle installed on your machine (or use the Gradle Wrapper)
- 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 configure it as follows:
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()
}
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);
}
}
Step 4: Running the Application
Using the Gradle Wrapper
The Gradle Wrapper is a script that allows you to run Gradle tasks without having Gradle installed on your system. It's included in your project when you generate a Spring Boot project from Spring Initializr.
Steps to Run the Application
-
Open a terminal: Navigate to the root directory of your Spring Boot project.
-
Run the Application: Use the
bootRun
task to start the application.
For Unix/Linux/macOS:
./gradlew bootRun
For Windows:
gradlew.bat bootRun
Explanation of the Command:
./gradlew
orgradlew.bat
: This is the Gradle Wrapper script. It ensures that the version of Gradle specified in your project is used, even if Gradle is not installed globally on your system.bootRun
: This is the task provided by the Spring Boot Gradle plugin to run your application.
Step 5: Creating a Simple REST Controller
To verify the application works as expected, let's create a simple 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!";
}
}
Running the Application Again
After adding the REST controller, run the application again using the same bootRun
command.
For Unix/Linux/macOS:
./gradlew bootRun
For Windows:
gradlew.bat bootRun
Verifying 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!
- URL:
You should see the "Hello, World!" message returned by the HelloController
.
Conclusion
In this tutorial, you have learned how to run a 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.
- Running the application using the Gradle Wrapper and the
bootRun
task. - Creating a simple REST controller to verify the application works as expected.
By following these steps, you can easily set up and run Spring Boot applications using Gradle, leveraging its powerful build and dependency management capabilities.
Comments
Post a Comment
Leave Comment