Spring Boot – Setting Up a Spring Boot Project with Gradle
Last Updated :
06 Sep, 2024
Spring Boot is a Java framework designed to simplify the development of stand-alone, production-ready Spring applications with minimal setup and configuration. It simplifies the setup and development of new Spring applications, reducing boilerplate code.
In this article, we will guide you through setting up a Spring Boot project using Gradle, a widely-used build automation tool that supports flexible configurations for managing dependencies, compiling code, and deploying projects.
Build Tools & Gradle
Build Tool
A build tool automates the process of transforming code into a deployable and executable format. Build tools handle tasks like compiling code, running tests, managing dependencies, and deploying the application.
Gradle
Gradle is a popular build automation tool used to compile, test, package applications, and manage dependencies. It’s widely adopted in the Java ecosystem due to its flexibility, performance, and ability to handle large projects. Gradle supports Kotlin and Groovy languages and is often compared to other build tools like Maven and Ant.
Now, let’s set up a Spring Boot project using Gradle.
Project Setup
Before proceeding, ensure that Java is installed on your machine. Run the following command to check:
java --version
In this guide, Java 21 is used, but any version above Java 8 will work. Make sure that the Java version you use is compatible with the version of Spring Boot you want to work with.
Step-by-step Implementation to set up Spring Boot Project with Gradle
Step 1: Generate a Spring Boot Project
Head over to Spring Initializr and generate a new project with the following configuration:
- Project: Gradle - Groovy
- Language: Java
- Spring Boot Version: 3.3.2 (or any preferred version)
- Metadata: Provide group, artifact, and project name
- Packaging: Jar
- Java Version: 21 (or your preferred version)
- Dependencies: Add "Spring Web"
Click "Generate" to download the project, then open it in your preferred IDE.
Step 2: Explore the Gradle Project
Project Structure:
Upon generating the project, you’ll see a folder structure like this:
Let us explore the main files and folders.
src/
Folder
This folder contains:
- Source code under
src/main
- Test cases under
src/test
- Configuration files under
src/main/resources
settings.gradle
This file defines the project name and includes any sub-projects.
rootProject.name = 'springbootgradledemo'
You can add sub-projects like this:
include("my_sub_project")
build.gradle
This file contains information about plugins, dependencies, and build configurations, similar to Maven's pom.xml
.
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.2'
id 'io.spring.dependency-management' version '1.1.6'
}
group = 'com.geeksforgeeks'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
Plugins:
Plugins are basically extensions to Gradle's functionality which enable you to perform common build tasks in an easier way.
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.2'
id 'io.spring.dependency-management' version '1.1.6'
}
Plugins are either Core plugins or Community plugins.
- Core Plugins, such as id 'java' are built in to Gradle.
- Community Plugins, such as id 'org.springframework.boot' version '3.3.2' are from 3rd parties.
Java Language Version:
The below code specifies the Java version to 21.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
Specifying dependency antifactory / dependency repository:
Dependencies in your project are downloaded from a central repository / artifactory.
For example, if you are working on your personal computer, you might be using the Maven repository. If you are working in an organisation, you might be using a repository managed by your organisation.
In either case, you need to specify the dependency management repository using the below syntax.
repositories {
mavenCentral()
}
This asks Gradle to use Maven Central Repository.
Dependencies:
Dependencies are external modules that your project needs to compile, run, and test the code. Instead of coding some functionality from scratch, dependencies allow your application to use functionality provided by other software packages.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
You might note that in Maven's pom.xml, these dependencies are mentioned in XML tags.
Tasks:
Tasks represent a specific action which Gradle executes during the build process, such as compilation of code, running testcases, packaging the app, and so on.
tasks.named('test') {
useJUnitPlatform()
}
This defines a predefined task to run during the testcase execution, and it mentions to use Junit for running the tests.
gradle folder:
This folder contains files essential for managing the Gradle build process.
Adding an API
Now that the project structure is clear, let's add a simple API.
Create a new file HelloController.java
in src/main/java/com/geeksforgeeks/springbootgradledemo/
:
Java
/* springbootgradledemo/src/main/java/com/geeksforgeeks/springbootgradledemo/HelloController.java */
package com.geeksforgeeks.springbootgradledemo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
private String sayHello() {
return "Hello World";
}
}
The above code defines a GET /api/hello API, which returns a string as response.
Running the application
To run the application, type the below command in the terminal / command prompt, from the "springbootgradledemo" directory:
./gradlew bootRun
Output:
You should see a similar output if everything works as expected:
Note that the application runs at port 8080.
If you open http://localhost:8080/api/hello in your browser, you should see the output Hello World, as shown below:
Similar Reads
Spring Boot â Handling Background Tasks with Spring Boot
Efficiently handling background tasks with Spring Boot is important for providing a smooth user experience and optimizing resource utilization. Background tasks refer to operations that are performed asynchronously or in the background, allowing the main application to continue processing other requ
5 min read
Properties with Spring and Spring Boot
Java-based applications using the Spring framework and its evolution into the Spring Boot and the properties play a crucial role in configuring the various aspects of the application. Properties can allow the developers to externalize the configuration settings from the code. Understanding how to wo
4 min read
How to Create and Setup Spring Boot Project in Spring Tool Suite?
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
3 min read
Spring Security Integration with Spring Boot
Spring Security is a powerful and customizable authentication and access control framework for Java applications. It provides comprehensive security services for Java EE-based enterprise software applications. This article will integrate Spring Security with a Spring Boot application, covering confi
5 min read
Spring Boot - Customizing Spring Boot Starter
Spring Boot Starters are specialized project types designed to encapsulate and distribute common functionality, simplifying the setup of Spring Boot applications. Official starters, like spring-boot-starter-web and spring-boot-starter-data-jpa, bundle dependencies, configurations, and pre-built bean
6 min read
Setting the Log Level in Spring Boot When Testing
When developing applications with Spring Boot, controlling log output is essential for diagnosing issues and understanding application behavior, especially during testing phases. Setting the log level allows developers to specify the type of information logged by the application, ranging from verbos
4 min read
Spring Boot MockMVC Testing with Example Project
In a Spring Boot project, we have to test the web layer. For that, we can use MockMVC. In this tutorial, let us see how to do that by having a sample GeekEmployee bean and writing the business logic as well as the test cases for it. Example Project Project Structure: Â This is a maven project. Let's
5 min read
Spring Boot Batch Processing Using Spring Data JPA to CSV File
The Spring Batch is a framework in the Spring Boot ecosystem It can provide a lot of functionalities for Batch processing. The Spring Batch framework simplifies the batch development of applications by providing reliable components and other patterns for common batch processing concerns. Mostly, bat
7 min read
Spring Boot - Spring JDBC vs Spring Data JDBC
Understanding the difference between Spring JDBC and Spring Data JDBC is important for choosing the right approach to interact with relational databases in Spring Boot applications. Both frameworks serve the same purpose but differ significantly in terms of abstraction, ease of use, and developer pr
5 min read
Multi-Module Project With Spring Boot
Multi-Module project with Spring Boot refers to a project structure where multiple modules or subprojects are organized under a single parent project. Each module can represent a distinct component, functionality, or layer of the application, allowing for better organization, maintainability, and co
6 min read