Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Gradle Quick Start

Gradle is a build automation tool used mainly for Java, Android, and JVM-based projects, but it can build almost any type of software.

  • Purpose: Automates compiling code, running tests, packaging apps (e.g., JAR/APK), publishing artifacts, and running custom tasks.
  • Language: Build scripts are written in Groovy or Kotlin (files like build.gradle or build.gradle.kts).
  • Dependency management: Automatically downloads and manages libraries from repositories like Maven Central.
  • Performance: Uses incremental builds and build caching to avoid redoing work, making builds faster.
  • Extensible: Has a rich plugin system (e.g., Java plugin, Android plugin) and allows you to write custom tasks.

Installation

Prerequisites

Note

Gradle supports Kotlin and Groovy as the main build languages. Gradle ships with its own Kotlin and Groovy libraries, therefore they do not need to be installed. Existing installations are ignored by Gradle.

Linux Installation (manual)

Step 1 - Download the latest Gradle distribution

The distribution ZIP file comes in two flavors: Binary-only and Complete. I’ll be installing the Binary-only version.

Step 2 - Unpack the distribution

Unzip the distribution zip file in the directory of your choosing, e.g.:

sudo mkdir /opt/gradle

sudo unzip -d /opt/gradle gradle-9.3.1-bin.zip

Step 3 - Configure your system environment

export PATH=$PATH:/opt/gradle/gradle-9.3.1/bin

Verify the installation

gradle -v

Output will be similar to this:

------------------------------------------------------------
Gradle 9.3.0
------------------------------------------------------------

Build time:    2026-01-16 11:14:22 UTC
Revision:      701205ed2f78811508466c8e1952304c2ea869f5

Kotlin:        2.2.21
Groovy:        4.0.29
Ant:           Apache Ant(TM) version 1.10.15 compiled on August 25 2024
Launcher JVM:  21.0.9 (Ubuntu 21.0.9+10-Ubuntu-124.04)
Daemon JVM:    /usr/lib/jvm/java-21-openjdk-amd64 (no Daemon JVM specified, using current Java home)
OS:            Linux 6.14.0-37-generic amd64

IDE Setup

I’ll be using Visual Studio Code as my IDE. If you’re using something different (e.g., Intellij), it shouldn’t be difficult to adapt.

Install these VS Code extensions:

Initialize a Project

mkdir project-name

cd project-name

Then:

gradle init --type java-application  --dsl kotlin

(Accept the defaults)

Valid --type values:

TypeDescription
pomConverts an existing Apache Maven build to Gradle
basicA basic, empty, Gradle build
java-applicationA command-line application implemented in Java
java-gradle-pluginA Gradle plugin implemented in Java
java-libraryA Java library
kotlin-applicationA command-line application implemented in Kotlin/JVM
kotlin-gradle-pluginA Gradle plugin implemented in Kotlin/JVM
kotlin-libraryA Kotlin/JVM library
groovy-applicationA command-line application implemented in Groovy
groovy-gradle-pluginA Gradle plugin implemented in Groovy
groovy-libraryA Groovy library
scala-applicationA Scala application
scala-libraryA Scala library
cpp-applicationA command-line application implemented in C++
cpp-libraryA C++ library

Valid --dsl values: kotlin and groovy.

Project Details

The Gradle configuration details can be found in app/build.gradle.kts:

/*
 * 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 on building Java & JVM projects, please refer to https://docs.gradle.org/9.3.0/userguide/building_java_projects.html in the Gradle documentation.
 */

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

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

dependencies {
    // Use JUnit Jupiter for testing.
    testImplementation(libs.junit.jupiter)

    testRuntimeOnly("org.junit.platform:junit-platform-launcher")

    // This dependency is used by the application.
    implementation(libs.guava)
}

// Apply a specific Java toolchain to ease working on different environments.
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

application {
    // Define the main class for the application.
    mainClass = "org.example.App"
}

tasks.named<Test>("test") {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}

A main class is generated in app/src/main/java/org/example/App.java:

/*
 * This source file was generated by the Gradle 'init' task
 */
package org.example;

public class App {
    public String getGreeting() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}

A unit test is generated in app/src/test/java/org/example/AppTest.java:

/*
 * This source file was generated by the Gradle 'init' task
 */
package org.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class AppTest {
    @Test void appHasAGreeting() {
        App classUnderTest = new App();
        assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
    }
}

Gradle Wrapper

The generated project includes Gradle wrapper shell (gradlew) and batch (gradlew.bat) scripts. These allow anyone to build the project without having to install Gradle manually.

You can see all of the tasks that the Gradle wrapper is configured to run within a specific project by invoking it like this:

./gradlew tasks

Example output (abbreviated):

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project 'gradle-test'
------------------------------------------------------------

Application tasks
-----------------
run - Runs this project as a JVM application

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the classes of the 'main' feature.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
updateDaemonJvm - Generates or updates the Gradle Daemon JVM criteria.
wrapper - Generates Gradle wrapper files.

Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the 'main' feature.

Build the Project

Using the Gradle wrapper (in the project directory):

./gradlew build

Using the Gradle runner in VS Code:

VS Code Gradle Runner

Run the Project

Using the Gradle wrapper:

./gradlew run

Using the Gradle runner in VS Code:

VS Code Gradle Runner

Output:

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 1s
2 actionable tasks: 1 executed, 1 up-to-date
Configuration cache entry reused.

Run Unit Tests

Using the Gradle wrapper:

./gradlew test

Using the Gradle runner in VS Code:

VS Code Gradle Runner

Output:

BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 up-to-date
Configuration cache entry stored.

Gradle Home Page

Other

DescriptionURL
Oracle Java: Oracle’s Java distribution, with Oracle-specific licenses and subscriptions.https://www.oracle.com/java/
OpenJDK: Open-source reference implementation of Java. Free to use and redistribute.https://openjdk.org/
Kotlin: A general-purpose programming language derived from Java.https://kotlinlang.org/
Apache Groovy: A Java-syntax-compatible object-oriented programming language for the Java platform.https://groovy-lang.org/
Maven Central: Public repository for Java and JVM artifacts used by Apache Maven, Gradle, and other build tools.https://central.sonatype.com