Gradle Introduction

Gradle is an open-source build automation tool focused on flexibility and performance.

Installation

see Installing Gradle for detailed instruction

If you are using sdkman, just run

1
sdk install gradle

It will ask you to set the default gradle, you can just set the latest version.

To get a list of gradle candidate versions

1
sdd list gradle

For Linux user, Just download gradle binary from https://gradle.org/install/ and add its bin folder to System path.

To test if Gradle is installed, get its version:

1
gradle -v

gradle -v command will show the JVM version. JVM version is set from JAVA_HOME Environment Variable.

Eclipse Integration - Gradle has an official Eclipse plugin called BuildShip.

You can learn Gradle from the official document - Learning the Basics section.

Projects, plugins and tasks

Gradle build is made up of one or more projects. Gradle projects are made up of one or more tasks that perform build steps. Most of the time, a project has a build.gradle file.

A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.

Typically, tasks are provided by applying a plugin so that you do not have to define them yourself.

First Build Script

build.gradle file contains the build script. It defines how the project is build. build.gradle file can have tasks, dependencies and plugins. Here we define a task called hello.

build.gradle

1
2
3
4
5
task hello {
doLast{
println "Hello, Gradle"
}
}

You can use Groovy or Kotlin to write Gradle build script. Here we use Groovy.

To run a build, run gradle <task>. Here we pass the name of the task ‘hello’ as argument.

1
2
3
4
5
6
7
$ gradle hello

> Task :hello
Hello Gradle

BUILD SUCCESSFUL in 499ms
1 actionable task: 1 executed

You can add -q option to hide the extra information.

1
2
$ gradle -q hello
Hello Gradle

use gradle tasks --all command to display all tasks

Extra Property

Additional, ad-hoc, properties for Gradle domain objects.

To define a foo property with value “bar”. You can use dollar sign to get the value of a variable. So to access property value foo, use $foo or ${foo}.

1
2
3
4
5
6
7
task hello {
ext.foo = "bar"

doLast {
println "Hello ${foo}"
}
}

You can also define property like this

1
2
3
ext {
foo = "bar"
}

Example - Use of property when defining spring dependencies

1
2
3
4
5
6
7
8
ext {
set('springCloudVersion', "2020.0.2")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

REMEMBER: To refer to a property in a string. The String MUST be enclosed with double quotes. Not single quotes!

see Extra Properties for more information

Default Task

Gradle allows you to define one or more default tasks that are executed if no other tasks are specified.

1
defaultTasks 'clean', 'run'

Project

Each build script you have is associated with an object of type Project and as the build script executes, it configures this Project

Project has many standard properties. You can find all properties from https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#N15101

Example to set group, version and sourceCompatibility property of Project.

1
2
3
4
name = 'projectname'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

You can reference the project property using $, just like extra property. Example to print project name and version:

1
2
3
4
5
task projectinfo {
doLast {
println "name: ${project.name} version: ${project.version}"
}
}

You can define extra properties for the project through ext namespace.

1
2
3
4
5
project.ext.prop1 = "foo"
task doStuff {
ext.prop2 = "bar"
}
subprojects { ext.${prop3} = false }

Plugin

Gradle provides very little features for real world automation. All of the useful features like compile java code are added by plugins.

syntax

1
2
3
4
plugins {
id «plugin id»
id «plugin id» version «plugin version» [apply «false»]
}

Applying plugins to a project.

1
2
3
4
plugins {
id 'org.springframework.boot' version '2.4.4'
id 'java'
}

To learn more about plugins see - Using Gradle Plugins.

Core plugins are provided by Gradle.

Base Plugin

Base Plugin defines serveral standard lifecycle tasks. All the core language plugins, like the Java plugin, apply the Base Plugin and inherit these lifecycle tasks.

Tasks

clean

Deletes the build directory and everything in it

1
gradle clean

check

verification tasks should be attached to check lifecycle task

assemble

Artifact related tasks such as jar should be attached to this lifecycle task

build

Intended to build everything, including running all tests, producing the production artifacts and generating documentation. You will use gradle build command a lot to build project.

1
gradle build

Java Plugin

Java Plugin is the most used core plugin. The Java plugin adds Java compilation along with testing and bundling capabilities to a project. It serves as the basis for many of the other JVM language Gradle plugins.

some useful tasks Java Plugin uses are compileJava, compileTestJava, jar, javadoc, test, clean…

The Java plugin attaches some of its tasks to the lifecycle tasks defined by the Base Plugin and adds a few other lifecycle task

add java plugin

1
2
3
plugins {
id 'java'
}

To build java project, just need to run gradle build

1
gradle build

Repositories

Maven Central is a popular repository hosting open source libraries for consumption by Java projects.

To declare the Maven Central repository for your build

1
2
3
repositories {
mavenCentral()
}

To declare JCenter Maven Repository

1
2
3
repositories {
jcenter()
}

For more on Repositories see Declaring Repositories

Adding Dependencies

Example to add dependencies

1
2
3
4
5
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

The default dependency configurations are

  • Compile
  • Runtime
  • Test Compile
  • Test Runtime

Bootstrapping new Project

Use built-in gradle init task to create a new Gradle build

1
gradle init

You can also specify a project type

1
gradle init --type basic

Clear Cache

Gradle stores all dependencies in caches folder. This folder can get very big after a while. To remove cache, run the following command.

1
rm -r $HOME/.gradle/caches/

Update Gradle Wrapper

To update the Gradle wrapper, run the following command. It will update the gradle wrapper to the latest version.

1
gradle wrapper --gradle-version 8.6

Resources