📜  gradle springboot run - Java (1)

📅  最后修改于: 2023-12-03 14:41:37.542000             🧑  作者: Mango

Gradle Spring Boot Run - Java

Introduction

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.

Getting Started

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:

  1. Create a new directory for your Spring Boot application:
mkdir my-spring-boot-app
  1. Navigate to the directory and initialize a new Gradle project:
cd my-spring-boot-app
gradle init
  1. Add the Spring Boot plugin and dependencies to your Gradle build file:
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'
}
  1. Create a new Spring Boot application class and add some simple REST endpoints:
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!";
    }
}
  1. Run your Spring Boot application using Gradle:
gradle springboot run
  1. Access the endpoints in your web browser or using curl:
http://localhost:8080/
http://localhost:8080/ping

Congratulations! You have successfully built and run your Spring Boot application using Gradle.

Conclusion

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.