Gradle Command to Run Spring Boot Application

In this guide, we'll cover the necessary steps to run a Spring Boot application using Gradle commands.

Pre-requisites

  • A Spring Boot application set up with Gradle. If you don't have one, you can generate a basic project from the Spring Initializer with Gradle chosen as the build tool. 
  • Gradle installed locally (or use the Gradle Wrapper provided in most Spring Boot projects). 

Running Spring Boot with Gradle 

When you generate a Spring Boot project with Gradle, the spring-boot-gradle-plugin is usually included. This plugin provides the bootRun task, which you can use to run the application. 

To run your Spring Boot application, navigate to the project directory in your terminal and execute:

gradle bootRun

If your project uses the Gradle Wrapper (which is common and recommended for ensuring consistent builds across environments), the command is:

./gradlew bootRun

On Windows:

gradlew.bat bootRun

What's happening behind the scenes? 

When you use the bootRun task

Compilation: Gradle compiles your source code and resources. 

Classpath Construction: All the project's dependencies are gathered and added to the classpath.

Application Start: The Spring Boot application starts, and you'll see the familiar Spring banner in the terminal, followed by the log outputs. 

Stopping the Application 

You can stop the running application by pressing Ctrl + C in your terminal. 

A Note on the Gradle Daemon:

By default, Gradle uses the Gradle Daemon to execute tasks. The daemon is a background process that makes builds faster by caching the results of previous builds. However, if you encounter any unusual behavior while running your application, consider stopping the daemon using the gradle --stop command and then running your application again. 

Conclusion 

Running a Spring Boot application using Gradle is straightforward, thanks to the spring-boot-gradle-plugin. The bootRun task provides a quick and efficient way to start your application, making your development process smoother and more productive. Happy coding!

Comments