How to Build Spring Boot Project Using Gradle

In this article, we will cover how to build a Spring Boot project using Gradle, a popular build automation tool for Java projects. Gradle simplifies the build process and dependency management, allowing developers to focus on coding rather than configuration. This guide will walk you through the steps to set up and build a Spring Boot project using Gradle.

Prerequisites

Before we begin, ensure you have the following installed on your Ubuntu system:

  • AWS Account with Ubuntu 24.04 LTS EC2 Instance.
  • OpenJDK 17: The Java Development Kit (JDK) for running Java applications.
  • Gradle: A build automation tool that manages dependencies and build tasks.

Step #1:Set Up Ubuntu EC2 Instance

If you don’t have JDK or Gradle installed on your system, you can install them using the following commands.

First, update the package list.

sudo apt update
How to Build Spring Boot Project Using Gradle 1

Then install OpenJDK 17, which is required to run your Spring Boot application.

sudo apt install openjdk-17-jdk -y
How to Build Spring Boot Project Using Gradle 2

Verify the Java installation.

java -version
How to Build Spring Boot Project Using Gradle 3

You should see the version details of your Java installation.

Now let’s install Gradle. Gradle can be installed using the following command,

sudo apt install gradle -y
How to Build Spring Boot Project Using Gradle 4

Step #2:Create Spring Boot Project Using Gradle

To create a new Spring Boot project, you can use the command line. Run the following command to generate a new project.

First create a directory for the project.

mkdir spring-boot-project
How to Build Spring Boot Project Using Gradle 5

Navigate to that directory.

cd spring-boot-project
How to Build Spring Boot Project Using Gradle 6

Next, initialize the project using Gradle.

gradle init --type java-application
How to Build Spring Boot Project Using Gradle 7

This command sets up a basic Gradle project structure.

Step #3:Create Spring Boot Application

Open the build.gradle file.

nano build.gradle
How to Build Spring Boot Project Using Gradle 8

Replace its content with the following.

plugins {
        id 'java'
        id 'org.springframework.boot' version '3.3.5'
}

apply plugin: 'io.spring.dependency-management'

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

repositories {
        mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}
How to Build Spring Boot Project Using Gradle 9

Explanation of the code:

This is a Gradle build script for a simple Java Spring Boot project, using the Spring Boot plugin and dependency management for easier setup and version control.

  1. Plugins Block:
    • id 'java': Adds support for Java compilation and packaging.
    • id 'org.springframework.boot' version '3.3.5': Adds Spring Boot’s Gradle plugin (version 3.3.5) for Spring Boot application support, which includes tasks for running and packaging the app.
  2. apply plugin:
    • This applies the Spring Dependency Management plugin, which manages transitive dependencies for Spring projects, making it easier to handle compatible versions of libraries.
  3. Project Metadata:
    • group: Defines the package namespace (e.g., com.example) for identifying the project.
    • version: Sets the current version of the project (0.0.1-SNAPSHOT), where “SNAPSHOT” denotes a work-in-progress.
    • sourceCompatibility: Specifies Java 17 as the target version.
  4. Repositories:
    • Defines mavenCentral as the source for downloading dependencies, the main repository for Java libraries.
  5. Dependencies Block:
    • implementation 'org.springframework.boot:spring-boot-starter-web': Adds the Spring Boot Web Starter dependency, which includes everything necessary to create a web application, including a Tomcat server and essential Spring components.

Next navigate to the Main Java source directory.

cd src/main/java
How to Build Spring Boot Project Using Gradle 10

Remove the default App.java file that was generated.

sudo rm App.java
How to Build Spring Boot Project Using Gradle 11

Create the package structure and navigate into it.

mkdir -p com/example
cd com/example
How to Build Spring Boot Project Using Gradle 12

Create a new Java file named MyApplication.java.

nano MyApplication.java
How to Build Spring Boot Project Using Gradle 13

Add the following code to create a simple Spring Boot application into MyApplication.java.

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// Mark this class as a Spring Boot application
@SpringBootApplication
@RestController
public class MyApplication {

    // Define a simple REST endpoint
    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

    // The main method to run the application
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
How to Build Spring Boot Project Using Gradle 14

Explanation of the code:

This Java code defines a simple Spring Boot application with a REST API endpoint.

  1. Package Declaration:
    • Declares the package as com.example, helping to organize the code within this namespace.
  2. Imports:
    • SpringApplication and SpringBootApplication: These provide the main structure for a Spring Boot application.
    • RequestMapping and RestController: These are used to define a REST controller and map HTTP requests to methods.
  3. Class Annotation and Declaration:
    • @SpringBootApplication: Marks this class as the main entry point for Spring Boot, enabling auto-configuration and component scanning.
    • @RestController: Marks the class as a REST controller, allowing it to handle HTTP requests and send responses directly.
  4. Define a Simple REST Endpoint:
    • @RequestMapping("/"): Maps the root URL (/) to this method, making it the default endpoint.
    • home(): When a user accesses the root URL, this method returns "Hello World!" as a plain text response.
  5. Main Method:
    • main: The starting point of the application, running it by calling SpringApplication.run(MyApplication.class, args), which boots up the embedded web server (usually Tomcat by default) and deploys the application.

This creates a simple web application with a single endpoint (/) that returns “Hello World!” when accessed.

Step #4:Build and Run the Application

Navigate back to the root directory of the project.

cd ../../../../..
How to Build Spring Boot Project Using Gradle 15

Now note that, here system install older version of gradle which is not compatible with our project setup.

gradle -version
How to Build Spring Boot Project Using Gradle 16

so lets upgrade Gradle Wrapper.

Open the gradle-wrapper.properties file to configure the Gradle wrapper. This file is located in the gradle/wrapper directory. You can adjust the Gradle version here.

nano gradle/wrapper/gradle-wrapper.properties
How to Build Spring Boot Project Using Gradle 17

specify the Gradle version you want to use in this file. Here we are using 8.1.1.

How to Build Spring Boot Project Using Gradle 18

Once done, check the Gradle wrapper version.

./gradlew -version
How to Build Spring Boot Project Using Gradle 19

Now you can build and run the application using the following command.

./gradlew bootRun
How to Build Spring Boot Project Using Gradle 20

You should see logs indicating that your application has started successfully. Now lets access the Hello World Endpoint.

Open your web browser and run the following URL http://<EC2-Instance-IP>:8080.

How to Build Spring Boot Project Using Gradle 21

You should see the message “Hello World!”.

Conclusion:

In conclusion, Gradle is a powerful tool that simplifies the process of building and managing Spring Boot projects. By following the steps outlined in this article, you can set up a new Spring Boot project, manage dependencies, and build your application effortlessly. This basic setup can be extended with various Spring Boot features, such as REST endpoints, database connections, and more. With practice, you’ll find that Gradle becomes an indispensable part of your Java development workflow.

Related Articles:

Metrics and Tracing for .NET App using OpenTelemetry, Jaeger and Prometheus

Reference:

Official Opentelemetry Page

Prasad Hole

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap