📅  最后修改于: 2023-12-03 14:41:37.542000             🧑  作者: Mango
Gradle is a powerful build automation tool that provides an efficient way to manage dependencies, build and test projects. With Gradle, you can easily build, test and deploy your Java applications. In addition, Spring Boot is a popular framework to create standalone, production-grade Spring based applications. It provides a powerful set of features to simplify the development process and reduce boilerplate code. One of the benefits of using Spring Boot with Gradle is that it provides a simple way to build, run and test your Spring Boot application.
Before you can start building your Spring Boot application with Gradle, you need to have the following installed in your local environment:
Once you have these installed, you can follow these steps to create a new Spring Boot application and run it using Gradle:
mkdir my-spring-boot-app
cd my-spring-boot-app
gradle init
plugins {
id 'org.springframework.boot' version '2.6.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
@GetMapping("/")
public String hello() {
return "Hello World!";
}
@GetMapping("/ping")
public String ping() {
return "Pong!";
}
}
gradle springboot run
http://localhost:8080/
http://localhost:8080/ping
Congratulations! You have successfully built and run your Spring Boot application using Gradle.
Gradle and Spring Boot are powerful tools that can be used together to build robust and scalable Java applications. By following the steps outlined in this tutorial, you should now have a basic understanding of how to build and run a Spring Boot application using Gradle.