On this page
  1. Gradle
  2. Overview
  3. Minimal build.gradle.kts (Java 21 + JUnit 5)
  4. Key Dependency Configurations
  5. Version Catalogs (gradle/libs.versions.toml)
  6. Common Wrapper Commands
  7. Maven vs. Gradle at a Glance

Gradle

Prev: Apache Maven | Up: third_party/java | Next: JUnit 5
Rel: Apache Maven, OpenJDK Distributions & Toolchain, JUnit 5
Ref: Gradle User Manual | Maven Central
Tags: #public #prog #java #third-party #build-tool #tier/core


Overview

Gradle is a fast, programmable build automation tool that uses a directed acyclic graph (DAG) of tasks, incremental compilation, a persistent background daemon, and local/remote build caching. Modern Java projects prefer the Kotlin DSL (build.gradle.kts) over Groovy (build.gradle) for IDE autocompletion and compile-time checking.

Minimal build.gradle.kts (Java 21 + JUnit 5)

plugins {
    java
    application
}

group = "com.example"
version = "1.0.0"

java {
    // Automatically compiles and tests with a Java 21 toolchain
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.fasterxml.jackson.core:jackson-databind:2.18.1")
    testImplementation(platform("org.junit:junit-bom:5.11.3"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.test {
    useJUnitPlatform()
}

application {
    mainClass = "com.example.Main"
}

Key Dependency Configurations

Configuration Compile Classpath Runtime Classpath Exposed to Downstream Consumers
implementation Yes Yes No (reduces recompilation cascade)
api (java-library plugin) Yes Yes Yes (part of your public method signatures)
runtimeOnly No Yes No (JDBC drivers, logging bindings)
compileOnly Yes No No (compile-time annotations)
testImplementation Test only Test only No

Version Catalogs (gradle/libs.versions.toml)

Multi-module projects centralize dependency coordinates in gradle/libs.versions.toml:

[versions]
jackson = "2.18.1"
junit = "5.11.3"

[libraries]
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
junit-bom = { module = "org.junit:junit-bom", version.ref = "junit" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter" }

Common Wrapper Commands

Always commit ./gradlew and gradle/wrapper/ so every developer and CI runner uses the exact same Gradle version:

./gradlew build                 # Compile, test, check, and package
./gradlew test --rerun-tasks    # Force re-run tests even if up-to-date
./gradlew dependencies          # Inspect resolved dependency graph
./gradlew run                   # Execute application mainClass

Maven vs. Gradle at a Glance

Linked from

Related notes