Open In App

Spring Boot – Setting Up a Spring Boot Project with Gradle

Last Updated : 06 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Project Metadata

Step 2: Explore the Gradle Project

Project Structure:

Upon generating the project, you’ll see a folder structure like this:

Project Folder Structure

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:

Output

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:

Browser Output



Next Article

Similar Reads