avatarRuby Valappil

Summary

This article provides a beginner's guide to building a SpringBoot application using Gradle, including step-by-step instructions, code examples, and personal insights from the author's experience transitioning from Maven to Gradle.

Abstract

The article serves as a comprehensive guide for those new to Gradle in the context of SpringBoot application development. It starts with the author's anecdotal preference for Maven over Gradle, followed by a client's request that prompted a shift to Gradle. The guide covers essential steps such as creating a project, importing code into an IDE, and analyzing Gradle files (settings.gradle and build.gradle). It details the structure and significance of these files, including the declaration of plugins, repositories, and dependencies. The author also demonstrates how to define custom tasks in Gradle and concludes with a practical example of adding a GET API to the application, running it, and accessing the API endpoint. Additionally, the article provides a link to the complete code on GitHub and promotes the author's newsletter and a cost-effective AI service.

Opinions

  • The author has a long-standing preference for Maven but was compelled to learn Gradle due to a client's preference.
  • Gradle is presented as a viable alternative to Maven, with the author suggesting that it might be more suitable for certain projects.
  • The author expresses a personal journey of overcoming resistance to change and embracing a new tool, which could inspire readers to do the same.
  • There is an implied endorsement of Gradle for SpringBoot projects, as the article is structured to guide readers through the benefits and practical uses of Gradle in this context.
  • The author encourages engagement with their content by inviting readers to subscribe to their newsletter and try out a recommended AI service, indicating a belief in the value of these resources.

Beginner's Guide to Build SpringBoot Application with Gradle

A simple example

I have been building SpringBoot applications for the past 7-plus years, and never once have I thought of using Gradle.

Always felt a solid resistance to moving away from Maven. We had a strong bonding, I thought 😜

Recently, for one of the projects, the client preferred Gradle.

While I was doing my research on this, I thought I might as well write it down for first-timers like me.

Below are the steps to create a SpringBoot project using the Gradle build tool.

Create a Project

Generate SpringBoot code

2. Import code to IntelliJ (or any other IDE)

Analyzing Gradle Files

  1. settings.gradle file

This file contains one mandatory line,

rootProject.name = 'gradle-demo'

This statement assigns a name to the build.

The build name need not necessarily be the root folder’s name but could be any name of our choice.

Another line that goes in this file is optional,

include('<subfolder>')

This statement defines that the project contains a subfolder that needs to be included in the build or might contain the main java class.

2. build.gradle file

This file is the equivalent of pom.xml in Maven, where all the dependencies are defined.

Unlike Maven’s build file written in XML, the build file is written as a code.

Gradle build files support both Kotlin and Groovy.

In this example, the file is written in Groovy.

plugins {
   id 'org.springframework.boot' version '2.7.4'
   id 'io.spring.dependency-management' version '1.0.14.RELEASE'
   id 'java'
}

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

repositories {
   mavenCentral()
}
dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-web'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
   useJUnitPlatform()
}
  1. plugins: Used to declare plugin dependencies.
  • A core plugin can be applied using the short name. For example,
id 'java'
  • A community plugin is declared using the fully qualified id
id 'org.springframework.boot' version '2.7.4'

2. repositories: Defines the repository to be used to resolve dependencies.

We can add the URL in the repository block to define a custom Maven repository (for example, an in-house repository hosted by a company).

repositories {
    maven {
        url "http://repo.mycompany.com/maven2"
    }
}

3. dependencies: Defines the dependent jars, an equivalent of

<dependency>   
  <groupId>org.springframework.boot</groupId>   
  <artifactId>spring-boot-starter-web</artifactId>  
</dependency>

in maven.

4. Task: A task is a piece of work that the build performs, like compiling classes, creating a jar, etc.

A task comprises a sequence of Action objects. When the task is executed, each of the actions is executed.

In the above-given example,

  • name of the task is — test
  • action is defined within the { and } brackets

A task could be as simple as,

tasks.register('sample') {
    doLast {
        println 'Sample Task'
    }
}

Run the App as SpringBoot

  1. Add a GET API to the application
@GetMapping(value="greetings", produces = MediaType.APPLICATION_JSON_VALUE )
public String greetings(){
    return "Welcome Girl!";
}

2. Run the application as SpringBoot and hit the GET API from browser

http://localhost:8081/greetings

Output:

output

Code

You can download the complete code from GitHub, and if you do, please leave a star (I’m aiming for 50 😝)

Check out my Weekly Newsletter, The NonConformist Techie.

Subscribe, if it interests you. It’s 🆓

Java
Spring Boot
Software Development
Programming
Coding
Recommended from ReadMedium