On this page
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
- Choose Maven when you want strict, uniform, declarative XML conventions across many teams with minimal build-script complexity.
- Choose Gradle when you have large multi-module repos, custom codegen/polyglot build steps, Android/Kotlin targets, or need aggressive incremental build caching.
Linked from
Related notes
- tuts/third_party/java/junit Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java/maven Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java Linked here; links to this page; shared tags: #java, #prog
- tuts/third_party/java/spring_boot links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java/spring_framework links to this page; shared tags: #java, #prog, #tier
- tuts/java links to this page; shared tags: #java, #prog
- tuts/java/jvm_and_openjdk/openjdk_distributions_and_toolchain Linked here; shared tags: #java, #tier
- tuts/interop/java_and_c_ffm_and_jni shared tags: #java, #prog, #tier
- tuts/java/builtins/AutoCloseable shared tags: #java, #prog, #tier
- tuts/java/builtins/enum shared tags: #java, #prog, #tier