Skip to content

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 configurationMaven scopeMeaning
implementationcompileused by your code; not exposed to consumers' compile classpath
apicompileused by your code and leaked to consumers (library authors)
testImplementationtesttest code only
runtimeOnlyruntimeneeded at runtime, not compile
compileOnlyprovidedcompile 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 tasks

Always 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

MavenGradle
ConfigXML, declarativeGroovy/Kotlin DSL, programmable
ModelFixed lifecycle/phasesArbitrary task graph
SpeedSolidFaster: incremental builds + build cache + daemon
PredictabilityVery high (convention)High, but scripts can hide logic
Best whenStandard projects, simplicityCustom 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:version coordinates and repositories as Maven.
  • Configurations ≈ scopes; implementation vs api controls 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

Lesson Quiz · Gradle (basics)0 / 4
  1. How does Gradle express a dependency's coordinates?

    • AAs XML <dependency> elements
    • BAs a 'group:artifact:version' string in a configuration like testImplementation
    • CBy file path only
    • DIt can't — Gradle has no dependencies
  2. What's the point of the Gradle wrapper (./gradlew)?

    • AIt runs tests faster
    • BIt pins the exact Gradle version so every machine/CI builds identically
    • CIt replaces Maven Central
    • DIt compiles Kotlin only
  3. implementation vs api in Gradle — what's the difference?

    • ANo difference
    • Bapi exposes the dependency to consumers' compile classpath; implementation hides it
    • Cimplementation is for tests
    • Dapi is faster
  4. A fair characterization of Maven vs Gradle?

    • AGradle is always better
    • BMaven is rigid/convention-driven; Gradle is a flexible, programmable, often faster task graph
    • CThey use different repositories
    • DMaven can't run JUnit 5

Next: Dependencies & Logging.