Introduction
Spring boot is a popular framework for creating java applications and gradle is a popular framework for building, managing and running java based applications.
In this article, we will look at managing a spring boot application, build and run it using gradle directly.
Before continuing further, make sure that you have java and gradle installed on your system.
To start, create a new Spring Boot application. You can generate a new project in 2 different ways automatically using Spring Initializr and Spring Tool Suite(STS) or create the project structure manually.
2. Configure gradle
Navigate into the project created in step 1 and find build.gradle in the root folder.
Open it with a text editor. It will look somewhat as below
plugins { id 'org.springframework.boot' version '2.7.8' id 'io.spring.dependency-management' version '1.0.15.RELEASE' } group = 'com.codippa' 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' }
Go through this file, it contains the following details
A. plugins
This block specifies the plugins used in the project.
In this case, we are using the org.springframework.boot
and io.spring.dependency-management
plugins.
org.springframework.boot
plugin provides tasks for building and running Spring Boot applications, while the io.spring.dependency-management
plugin manages the versions of dependencies used in the project.
B. group
Specifies the group of the project. It is the unique id of the organization to which this project belongs.
Normal convention is to name it in reverse domain such as com.codippa.
C. version
It represents the version of project.
group
and version
are used to identify our project when it is built and deployed.
D. sourceCompatibility
Specifies the version of java that the application is compatible with.
In this case, we are using java 17.
E. repositories
This block specifies the repository that we want to use for downloading dependencies.
In this case, we are using the Maven Central repository. You can set it to your own if required.
F. dependencies
Specifies the dependencies that we want to use for our project.
In this case, we are using the org.springframework.boot:spring-boot-starter-web
dependency, which includes all the necessary dependencies to build a web application with spring boot.
We are also including the org.springframework.boot:spring-boot-starter-test
dependency for testing.
You can add further dependencies according to the project requirements.
Open command prompt or terminal and navigate to the project root directory.
Run the following command to compile the source code of the application
gradle build
This will execute build task of gradle and it will compile the application and execute unit test cases.
If this gives you gradle not found error, then set the path variable of your system to gradle installation directory or use gradle wrapper on Windows
gradlew build
or Linux/Mac
gradlew build
4. Running the application
To run a spring boot application from gradle, execute bootRun
task as shown below
gradle bootRun
This task is provided by org.springframework.boot
plugin, that we added in build.gradle.
Note that bootRun
also compiles the application code. So, if you do not want to execute unit test cases, then you can skip Step 3 and execute bootRun
directly to run a spring boot application from gradle.
Once you run this task, you will see spring boot logo followed by some logs and application startup message.
You have successfully started a spring boot application directly from gradle.
Hope the article was useful.