Appearance
Lesson 02 · Gradle (basics)
Beyond the 1Z0-830 exam
Gradle is the other build tool you'll meet — the default for Android and many modern JVM projects. You don't need to master it; you need to read a build script and know how it differs from Maven.
Objectives
After this lesson you will be able to:
- Read a
build.gradle(.kts): plugins, dependencies, tasks. - Run a Gradle build with the wrapper.
- Compare Gradle and Maven and pick sensibly.
A minimal build script
Gradle scripts are code (Groovy build.gradle, or Kotlin build.gradle.kts — preferred today), not XML. The same project as the Maven POM:
kotlin
plugins {
`java-library`
}
repositories {
mavenCentral() // same artifacts as Maven
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.11.3") // GAV string
}
tasks.test {
useJUnitPlatform() // run JUnit 5
}
java {
toolchain { languageVersion = JavaLanguageVersion.of(21) } // ~ maven.compiler.release
}The dependency string group:artifact:version is the same coordinates Maven uses, pulled from the same repositories.
Configurations ≈ scopes
Gradle's dependency configurations map roughly onto Maven scopes:
| Gradle configuration | Maven scope | Meaning |
|---|---|---|
implementation | compile | used by your code; not exposed to consumers' compile classpath |
api | compile | used by your code and leaked to consumers (library authors) |
testImplementation | test | test code only |
runtimeOnly | runtime | needed at runtime, not compile |
compileOnly | provided | compile only, not packaged |
implementation vs api is Gradle's key idea: hiding a dependency from your consumers' compile classpath speeds up their builds and prevents accidental coupling.
Tasks and the wrapper
Gradle builds a task graph; you invoke tasks, and their dependencies run first.
bash
./gradlew build # compile, test, assemble (the wrapper — no local Gradle needed)
./gradlew test # just tests
./gradlew tasks # list available tasksAlways use the wrapper (./gradlew / gradlew.bat) checked into the repo: it pins the exact Gradle version so every machine and CI runner builds identically.
Gradle vs Maven
| Maven | Gradle | |
|---|---|---|
| Config | XML, declarative | Groovy/Kotlin DSL, programmable |
| Model | Fixed lifecycle/phases | Arbitrary task graph |
| Speed | Solid | Faster: incremental builds + build cache + daemon |
| Predictability | Very high (convention) | High, but scripts can hide logic |
| Best when | Standard projects, simplicity | Custom builds, big/multi-module, Android |
Neither is "better." Maven's rigidity is its feature — every Maven project works the same way. Gradle trades some predictability for flexibility and speed.
Gotcha
Because a Gradle script is real code, build logic can hide in custom tasks and plugins — a Gradle build can do things a quick read of the file won't reveal. Maven's behavior is almost entirely inferable from the POM. Keep Gradle scripts declarative; push imperative logic into named tasks or plugins.
Key Takeaways
- Gradle uses a programmable DSL (prefer Kotlin
.kts) and a task graph, vs Maven's XML and fixed lifecycle. - Dependencies use the same
group:artifact:versioncoordinates and repositories as Maven. - Configurations ≈ scopes;
implementationvsapicontrols whether a dependency leaks to consumers. - Always build via the wrapper (
./gradlew) so the Gradle version is pinned. - Choose Maven for convention/simplicity, Gradle for flexibility/speed/large builds.
Lesson Quiz
How does Gradle express a dependency's coordinates?
What's the point of the Gradle wrapper (./gradlew)?
implementation vs api in Gradle — what's the difference?
A fair characterization of Maven vs Gradle?
Next: Dependencies & Logging.