Gradle Guide

Gradle is an open source tool used to automate builds. It is versatile enough to build almost any software. Gradle uses Java so having a JDK installed is the only prerequiste to using gradle in your repo.

What is special about Gradle?

There are many other build tools out there like Maven or Ant to name a few so why would you use Gradle. Gradle doesn't really add anything to the software engineer world that one of those tools already does but rather is more of an amalgamation of the two.

What does Gradle do for me?

Gradle can just build and package your code but it does so much more than that. Gradle can also manage dependencies for your code. Gradle is also highly configurable which gives it the power to integrate with almost any code or software you can make. Gradle is also super fast because it support incremental releases. This means that it runs a daemon on your machine which caches built dependencies and keeps track of changes between builds so it only has to recompile affected code. Gradle also supports plugins developed by you or other members of the community so its uses are nearly endless. Gradle build instructions can either be written in Groovy or Kotlin.

Okay I'm sold how do I start?

Gradle can be installed on any machine with a JDK installed. You could build it yourself from source or you can install a package for your OS. In my case (arch linux) I just have to run pacman -S gradle.

Next we will go over some of the Gradle commands and give a description of what they do:

Command Description
init Used to initialize current directory as a gradle project
tasks Used to list the tasks specified in build.gradle

Lets now look at some example projects to get an idea of the directory structure gradle implements and some of the features these commands have.

FirstProject
This is a basic application generated by gradle. This project also goes over some of the basic groovy commands and how to use them.
JavaProject
This project is a simple Java project generated by gradle and I mainly cover the directory structure of a gradle java project.

How does it all work?

Lets work through the a sample build.gradle file to get a better understanding of what we are looking at.

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.0.2/userguide/building_java_projects.html
 */

plugins {
        // Apply the application plugin to add support for building a CLI application in Java.
        id 'application'
}

repositories {
        // Use Maven Central for resolving dependencies.
        mavenCentral()
}

dependencies {
        // Use JUnit test framework.
        testImplementation 'junit:junit:4.13.1'

        // This dependency is used by the application.
        implementation 'com.google.guava:guava:30.0-jre'
}

application {
        // Define the main class for the application.
        mainClass = 'com.maker2413.gradle.App'
}

repositories {}

In this example lets take a look at the repositories block. In this example we are using the mavenCentral repository. This is a list of the repositories for gradle to look for dependencies. We can see in the dependencies block of our example build.gradle that we will require junit for testing so lets do a quick google search for junit and see if we can find it in mavenCentral. It is also worth noting that gradle caches dependencies when we pull them from the web so we don't have to download them every time we run our code, the cache of these dependencies can be found in /.gradle/:

tree -L 2 ~/.gradle/

Outputs:

/home/epost/.gradle/
├── caches
│   ├── 6.6.1
│   ├── 7.0.2
│   ├── jars-8
│   ├── journal-1
│   ├── modules-2
│   ├── transforms-2
│   └── transforms-3
├── daemon
│   ├── 6.6.1
│   └── 7.0.2
├── jdks
├── kotlin-profile
│   └── 2021-05-18-20-26-12-563.profile
├── native
│   ├── 53dbd7edfb1c66fb8903c78663c9afc45aea4f116125fa6feffd364cf09443e8
│   ├── 8bec1ad78214c89d9ad589b4d07fe93259594b29d510b10fafb7a158f7737e5d
│   ├── 9f2fa17c089eee584f3420100432f77ec4c098959e4bf121a4d6054c26537dfb
│   └── jansi
├── notifications
│   └── 7.0.2
├── workers
└── wrapper
        └── dists

23 directories, 1 file

We aren't limited to just mavenCentral mind you we can add other repositories in this block but I won't go over those here.

This page was last updated: not defined. Source