How to Install Gradle Build Tool on Debian 11

Gradle is a build automation tool that is based on a concept called ‘incremental builds’. It speeds up the development process by, for example, building only those parts of the project that have been modified. Incremental build works by (conceptually) tracking which files were changed and then using this information to determine what needs to be built. This helps Gradle avoid executing tasks that are not necessary.

Gradle can be seen as an analog of Apache Ant or Make, but it also has features typically associated with continuous integration tools like Jenkins: tasks can be executed in parallel, and tasks can depend on other tasks (this makes it possible to specify task execution order).

Gradle consists of various concepts:

  • A Task represents something that has to be done; examples include compiling the java source files, producing the documentation, or creating a jar file. A Task is usually executed by running Gradle using an appropriate command line; examples include “build” (to compile the project) and “docs” (to create HTML documents).
  • A Build Configuration specifies how something has to be done; for example, what compiler should be used or which classes are part of the Java source code. Examples of build configurations are ‘JavaIncremental’ (a default configuration that uses incremental builds) and ‘JavaNoTest’.
  • TaskInputs represent inputs used by tasks; examples of task inputs are AntJavadocTask.createSourceJar() or JarSigningTask.signJarFile([‘src/main/java’, ‘src/test/java’], ‘signed.jar’).
  • TaskOutputs represent outputs produced by tasks; examples of task outputs are JarSigningTask.getFile() or JavaCompile.createJar().

Gradle was created by Hans Dockter. It is released under Apache 2 license. The first public version was 1.0, which was released on July 16, 2012. The last stable release (as of February 2013) is 1.10 which, among others, fixes problems with wrongly running tests when the target directory contains spaces in its name (see ). It also introduces support for Scala language. Previous releases were more rarely updated than newer ones - the gap between 1.0 and 1.1 was about two months, between 1.1 and 1.2 was three months, etc.

In May 2015, the Gradle development team announced the first version of the software built using Gradle - Gradle 2.0. In addition to improvements described in release notes as usually provided for non-tooling parts of the tool as well as features providing better integration with other systems (e.g: Java or Spring framework support), the new version introduced a number of changes that had a strong influence on almost any part of Gradle’s design.

In this article, we’ll show you how to set up Gradle on Debian 11.

Prerequisites

In order to install Gradle, you will need:

  • Root privileges on the system you want to install Gradle on.
  • A Debian 11 system with a working Internet connection.

Updating the System

It’s advisable to update the system before you start installing new packages. Let’s do that now by running the command below.

sudo apt-get update && sudo apt-get upgrade -y

Installing Java

In order to run Gradle, you will need a Java Runtime Environment (JRE) or Java Development Kit (JDK) installed on the system.

  • JRE is a runtime environment that is often enough for most projects. It contains just the parts of Java that are required to run .jar files.
  • JDK is a superset of JRE, i.e., it includes everything from JRE plus tools for developing Java applications.

By default, Debian 11 comes with OpenJDK JDK 11. Let’s install JDK 11 now with the following command.

sudo apt install default-jdk -y

Once the installation finishes, run the command below to ensure that Java is installed correctly.

java -version

The output should be similar to the one below

Installing Gradle on Debian 11

Now that we have our system ready, let’s go ahead and install Gradle.

First, we will download Gradle with the command below. You might want to check the Gradle download page for the most recent version. Replace gradle-7.2 with the name of the file you want to download.

cd /tmp && curl -O https://downloads.gradle-dn.com/distributions/gradle-7.2-bin.zip

The commands above will download Gradle ZIP archive into /tmp directory. Once the download is complete, extract Gradle files from the zip archive using unzip command.

unzip gradle-*.zip

Next, move the extracted files into the /usr/local directory using the following commands.

sudo cp -pr gradle-*/* /opt/gradle

Use the ls command to list files and check that everything is in place.

ls /opt/gradle

You should see the following directories inside the opt directory: bin, init.d, lib, LICENSE, NOTICE, README.

You will now need to configure and update the PATH environment variables for Gradle’s executables to be available from anywhere on the system. To do that, we will create a new file called gradle in /etc/profile.d/ directory. Add Gradle’s executable to the PATH environment variable.

echo "export PATH=/opt/gradle/bin:${PATH}" | sudo tee /etc/profile.d/gradle.sh

Run the command below to enable execution permission for the script we just created.

sudo chmod+x /etc/profile.d/gradle.sh

Your PATH environment variable should now contain Gradle’s path, and you should be able to use all executables in it from any directory on your system.

Run the command below to read the updated PATH variable into the current shell.

source /etc/profile.d/gradle.sh

Finally, test that Gradle is installed correctly by running the gradle command with the -v option.

gradle -v

The output should be similar to the one below. Congratulations! Gradle is now installed and running on your Debian 11 system.

Testing the Installation

Now that Gradle has been successfully installed, let’s create a simple project using Gradle to make sure that everything is working fine.

First, create a new directory called gradle-demo and move to it with the following commands. It’s a good idea to keep your projects organized in separate directories, so you don’t have a huge mess of files and folders inside one directory.

mkdir gradle-demo && cd gradle-demo

Next, run the init command to initialize your project. The gradle init command is used to create Gradle build scripts. It takes a sub-command that tells it what type of project to generate. There are several different types, “basic” being one of them.

gradle init

The init command will prompt you with Select type of project to generate message. Press 2 and Enter to select the 2: application project type.

Once you press enter, Gradle will show Select implementation language question. Press 3 and Enter without typing anything to select Java as an implementation language.

Next, for the Select build script DSL message, press 1 and Enter to choose Groovy as build script DSL.

Next, for the Select test framework question, press Enter to choose the default value.

Next, provide your Project name and hit Enter. You can enter any name.

Gradle will now generate the build file based on these selections. You can see the BUILD SUCCESSFUL message when the build script is successfully generated. Your Gradle project has been successfully created. Everything is working as expected.

Conclusion

Gradle is a build automation tool that can be used to, amongst other things, automate the process of building Android apps.

In this article, we showed you how to install Gradle on Debian 11. We hope the article was helpful. If you have any queries or comments, feel free to leave them as a comment below.

Leave a Comment