Creating and Building a Spring Boot Project with Gradle

In this Spring Boot tutorial, we will walk through the process of creating and building a Spring Boot 3 project using Gradle. We’ll cover each step in detail, from setting up the project structure to running and testing the application.

1. Prerequisites

Before getting started, ensure that you have the following prerequisites installed on your system:

  • Java Development Kit (JDK) version 17 or higher
  • Gradle build tool.

If you haven’t already installed Gradle, you can follow the official installation guide: Installing Gradle.

2. Create Project Directory

Create a new directory for your Spring Boot project. You can name it anything you like. For example, let’s name it spring-boot-project.

mkdir spring-boot-project
cd spring-boot-project

3. Initialize Gradle Project

Inside the project directory, initialize a new Gradle project. Run the following command:

gradle init --type java-application

This command initializes a new Gradle Java project with the default project layout similar to the following:

spring-boot-project/
├── build.gradle
├── gradle/
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src/
    └── main/
        └── java/
            └── <package structure>
        └── resources/
            └── application.properties
  • build.gradle: The Gradle build script for your project.
  • gradle/: The directory containing Gradle wrapper files.
  • gradlew, gradlew.bat: Gradle wrapper scripts for Unix-based and Windows-based systems, respectively.
  • settings.gradle: Gradle settings file.
  • src/: The directory containing your project’s source code and resources.
    • main/: The main source directory.
      • java/: Java source files go here.
      • resources/: Resources (such as properties files, templates, etc.) go here.

4. Configure Build Script (build.gradle)

Open the build.gradle file in a text editor and configure it to include Spring Boot dependencies and plugins.

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.2.4'
	id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '17'
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
	useJUnitPlatform()
}

Let’s break down the configuration settings:

  • java: The Java plugin which adds support for compiling and running Java code.
  • org.springframework.boot: The Spring Boot plugin which provides support for building Spring Boot applications. The version specified is 3.2.4.
  • io.spring.dependency-management: The Spring Dependency Management plugin which manages dependencies for Spring projects. The version specified is 1.1.4.
  • group: Specifies the group or organization to which the project belongs. In this case, it’s set to com.example.
  • version: Specifies the version of the project. The version is 0.0.1-SNAPSHOT.
  • sourceCompatibility: Specifies the version of Java source code compatibility. In this case, it’s set to 17, indicate that the project’s Java source code is compatible with Java 17.
  • repositories: Specifies the repositories where Gradle should look for dependencies. In this case, it’s configured to use Maven Central repository.
  • implementation 'org.springframework.boot:spring-boot-starter': Specifies the Spring Boot starter dependency, which includes essential dependencies for building a Spring Boot application.
  • testImplementation 'org.springframework.boot:spring-boot-starter-test': Specifies the Spring Boot starter test dependency, which includes dependencies for testing Spring Boot applications.
  • useJUnitPlatform(): Configures the test task to use JUnit 5 platform for running tests. This is necessary for using JUnit 5 with Spring Boot.

5. Create a Spring Boot Application Class

Create a new Java class for your Spring Boot application. By convention, this class should be named Application and located in the src/main/java directory.

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Note that the @SpringBootApplication annotation adds @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations. We also defined a main method that starts the Spring Boot application using SpringApplication.run().

6. Run the Application

To run the Spring Boot application, execute the following Gradle command:

./gradlew bootRun

This command starts the embedded Tomcat server and deploys the Spring Boot application.

7. Conclusion

In this tutorial, we learned to create a Spring Boot project with Gradle from scratch. We learned how to initialize a Gradle project, configure the build script, create a Spring Boot application class, and run the application.

Now you can continue to develop and customize your Spring Boot application according to your project requirements.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.