Gradle Tutorial: Core Concepts and Installation

Gradle is a build automation tool, just like Maven or Ant, for writing scripts for building, testing, and deploying software projects and later running them in any environment – on demand. In this tutorial, we will learn about how Gradle is different from other build automation tools, and then we will see one simple example of installing and using Gradle.

1. Gradle Vs. Other Build Tools

1.1. Challenges with Other Build Tools

If you have worked with Maven, you know that you cannot generate more than one artifact (jar or war file etc.) from one build file (pom.xml). If you want to generate two jar files from a single src folder, then you will have to create possibly two projects in Maven. This is one of the difficulties with existing build tools.

Other build tools, for years, had been used for compiling and packaging software. But with changing industry requirements, projects involve large and diverse software stacks, incorporate multiple programming languages, and apply a broad spectrum of testing strategies. With the rise of agile practices, builds must support early code integration and frequent and easy delivery to test and production environments.

1.2. How Gradle Solves the Problems

Other tools fall short in meeting these goals, and it is where Gradle comes into the picture. It draws on lessons learned from established tools and takes their best ideas to the next level.

Gradle’s ability to manage dependencies isn’t limited to external libraries. As your project grows in size and complexity, you’ll want to organize the code into modules with clearly defined responsibilities. Gradle provides powerful support for defining and organizing multi-project builds, as well as modeling dependencies between projects.

Gradle build scripts are declarative, readable, and clearly express their intention. Writing code in Groovy instead of XML, sprinkled with Gradle’s build-by-convention philosophy, significantly cuts down the size of a build script and is far more readable.

2. Core Concepts

Let us go through the core terminology and components of gradle.

2.1. Project

A Gradle project is a collection of files and directories that we write in any application. A project contains the source code, resources, assets and build scripts.

The build script, typically named build.gradle, defines how the project is built, tested, and packaged.

2.2. Tasks

Tasks are the building blocks of a Gradle build process. Tasks are the individual actions that can be executed, such as compiling code, running tests, creating JAR files, and more.

Gradle provides a wide range of built-in tasks, and we can also define custom tasks.

In the following example, when we run ‘gradle hello‘, Gradle executes the hello task, which in turn executes the action provided in the task. In this case, the action is simply a block containing the code to print hello world.

tasks.register('hello') {
    doLast {
        println 'Hello world!'
    }
}

2.3. Gradle Plugins

Plugins in Gradle provide additional functionality and encapsulate common build tasks and configurations. This grouping makes it easier to integrate tools, frameworks, and libraries into the project.

Gradle offers a variety of plugins for different purposes, such as Java, Android, Spring, and more.

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

2.4. Project Dependencies

Dependencies are external libraries or modules that the application requires in the compile time or runtime. Gradle helps manage these dependencies when we declare them in the build script.

Gradle fetches and includes the necessary libraries during the build process.

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

2.5. Gradle Wrapper

The Gradle Wrapper is a script that allows using a specific version of Gradle without having to install it globally on the system. It ensures that everyone working on the project uses the same version of Gradle, enhancing consistency across different environments.

3. Installing Gradle

As a prerequisite, make sure you’ve already installed the JDK 6 or higher. Getting started with Gradle is easy. You can download the distribution directly from the Gradle homepage at https://gradle.org/gradle-download/. The distribution comes with sources, binaries, documents and examples.

  • Extract the downloaded zip file at some place into your development machine. Now set the environment variable GRADLE_HOME to gradle directory.
  • Add GRADLE_HOME/bin folder to PATH environment variable.

Now, we should be able to run ‘gradle -v‘ command from the command line. If it happens and you get the output, you are ready to use gradle into your project.

gradle -v

The output of the above command will look like this:

------------------------------------------------------------
Gradle 2.14.1
------------------------------------------------------------

Build time:   2016-07-18 06:38:37 UTC
Revision:     d9e2113d9fb05a5caabba61798bdb8dfdca83719

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.6.0_33 (Sun Microsystems Inc. 20.8-b03)
OS:           Windows 7 6.1 x86

4. Writing a Simple Gradle Script

Now let’s write a simple gradle script and try to run it. I have created a new folder on the desktop and named it Playground. Then I created build.gradle and wrote down below the hello world script.

task helloWorld {
   doLast {
      println 'Hello world!'
   }
}

The above script is written in Gradle’s DSL (self-descriptive language) e.g. action named doLast is almost self-expressive. It’s the last action that is executed for a task.

5. Executing Gradle Script

To run the script, run the following gradle command and pass the task name. The output will be as shown:

$ gradle -q helloWorld

Hello world!

With option quiet with –q, you tell Gradle only to output the task’s output.

The above usecase is very basic in nature, and has been written with the intention to keep things simple as much as it can – so you are familiar with the most basic stuff first.

In the coming tutorials, we will learn the more advanced features of Gradle.

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.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode