Gradle in depth
The model
Gradle builds a task graph from build scripts (build.gradle.kts Kotlin DSL or build.gradle Groovy DSL) and executes only the tasks needed:
plugins {
java
id("org.springframework.boot") version "3.3.4"
id("io.spring.dependency-management") version "1.1.6"
}
group = "com.acme.orders"
version = "1.4.0"
java { toolchain { languageVersion.set(JavaLanguageVersion.of(21)) } }
repositories {
maven { url = uri("https://nexus.acme.dev/repository/maven-public/") }
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.junit.jupiter:junit-jupiter")
}
tasks.test { useJUnitPlatform() }
Build phases: initialization (which projects, from settings.gradle.kts) → configuration (evaluate scripts, build the task graph) → execution (run selected tasks).
Dependency configurations
| Configuration | Meaning |
|---|---|
implementation |
Needed to compile and run, not exposed to consumers' compile classpath |
api (java-library plugin) |
Exposed to consumers — use sparingly |
compileOnly |
Compile only (like Maven provided) |
runtimeOnly |
Runtime only (like Maven runtime) |
testImplementation |
Tests |
Using implementation instead of the old compile keeps classpaths smaller and lets Gradle avoid recompiling consumers when internals change.
Gradle resolves conflicts by picking the highest version by default (unlike Maven's nearest-wins). Control with platforms/BOMs (implementation(platform("..."))), constraints, resolutionStrategy, and dependency locking (gradle dependencies --write-locks) for reproducibility. Inspect with:
./gradlew dependencies --configuration runtimeClasspath
./gradlew dependencyInsight --dependency jackson-databind
Why Gradle is fast
- Incremental builds — tasks declare inputs and outputs; unchanged tasks are
UP-TO-DATE. - Build cache — task outputs keyed by a hash of inputs, reusable across builds and machines (
FROM-CACHE). A remote build cache (Develocity, formerly Gradle Enterprise, or a self-hosted cache node) shares outputs between CI and developers. - Configuration cache — caches the configured task graph.
- Daemon — a warm JVM between builds (on ephemeral CI runners use
--no-daemonor accept it dies with the runner). - Parallel —
--parallelfor multi-project builds.
gradle.properties:
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.configuration-cache=true
org.gradle.jvmargs=-Xmx2g
The Gradle wrapper
gradlew + gradle/wrapper/gradle-wrapper.properties pin the Gradle version per repository; CI and developers run ./gradlew, never a system Gradle. Commit the wrapper jar and validate it (gradle/actions/wrapper-validation) — a tampered wrapper jar is a supply-chain attack. Maven has the equivalent mvnw.
Publishing
plugins { `maven-publish` }
publishing {
publications { create<MavenPublication>("lib") { from(components["java"]) } }
repositories {
maven {
url = uri(if (version.toString().endsWith("SNAPSHOT"))
"https://nexus.acme.dev/repository/maven-snapshots/"
else "https://nexus.acme.dev/repository/maven-releases/")
credentials {
username = System.getenv("NEXUS_USER")
password = System.getenv("NEXUS_TOKEN")
}
}
}
}
Maven vs Gradle
| Maven | Gradle | |
|---|---|---|
| Config | XML, declarative, convention-heavy | Kotlin/Groovy DSL, programmable |
| Conflict resolution | Nearest wins | Highest version wins |
| Incremental / caching | Limited (extensions exist) | Built in: up-to-date checks, build cache |
| Speed on large builds | Slower | Usually much faster |
| Learning curve | Lower, very predictable | Higher, flexible (can become messy) |
| Typical use | Enterprise Java, Spring | Android, large multi-project builds, Kotlin |