Stop 11 · Git, Build Tools & TestingTopics · 5/10

Gradle

Part of the Java Roadmap

In short

Every Android app and many modern backend projects build with Gradle. It does the same job as Maven, but with a short script instead of XML and smart caching that makes repeat builds fast.

Gradle is a build tool that uses Groovy or Kotlin scripts instead of XML. It is the standard for Android and is also widely used for backend Java projects.

The jaadu ticks items off a long checklist clipped to a board, happily skipping the ones already ticked.

Think of it like this

The smart to-do list

Imagine a to-do list that remembers what you already finished. If you ironed your shirt yesterday and it hasn't been worn, it skips ironing today. Gradle works like that: tasks depend on each other, and any task whose inputs haven't changed is marked UP-TO-DATE and skipped.

Watch it happen

Maven vs Gradle

Maven

  • pom.xml in XML
  • Fixed lifecycle: compile, test, package
  • Very predictable, easy to read
  • Common in enterprise and Spring projects

Gradle

  • build.gradle.kts (Kotlin) or build.gradle (Groovy)
  • Graph of tasks with incremental builds
  • Build cache and daemon make repeat builds fast
  • Standard for Android, popular for backends

Both use the same Maven Central libraries and the same folder layout. Learn to read both; use whichever your team already uses.

The big ideas

What you need to know

Plugins add tasks

The java plugin adds compileJava, test and jar. application adds run. Spring Boot adds bootRun and bootJar.

Configurations

implementation for app code, testImplementation for tests, runtimeOnly for things only needed when running.

Use the wrapper

./gradlew build, ./gradlew test, ./gradlew run. The wrapper downloads the exact Gradle version the project expects.

Toolchains

java { toolchain { ... } } pins the JDK version, so builds use Java 21 even if your default JDK is different.

See it in code

Walk through the code

kotlin
1plugins {2    java3    application4}5 6group = "com.example"7version = "1.0.0"8 9java {10    toolchain {11        languageVersion = JavaLanguageVersion.of(21)12    }13}14 15repositories {16    mavenCentral()17}18 19dependencies {20    implementation("com.google.code.gson:gson:2.11.0")21    testImplementation(platform("org.junit:junit-bom:5.11.3"))22    testImplementation("org.junit.jupiter:junit-jupiter")23    testRuntimeOnly("org.junit.platform:junit-platform-launcher")24}25 26application {27    mainClass = "com.example.App"28}29 30tasks.test {31    useJUnitPlatform()32}

1/4java gives compile, test and jar tasks. application adds a run task.

A build.gradle.kts for a Java 21 command-line app with Gson and JUnit 5. Run ./gradlew test or ./gradlew run.

Save yourself the pain

Common mistakes

Oops

Forgetting useJUnitPlatform() in the test task.

Do this instead

Without it, Gradle looks for JUnit 4 tests and silently runs none of your JUnit 5 tests.

Oops

Running a globally installed gradle command.

Do this instead

Run ./gradlew (gradlew.bat on Windows) so every machine uses the same Gradle version.

Oops

Using the old compile configuration from old tutorials.

Do this instead

compile was removed in Gradle 7. Use implementation and testImplementation.

Your mission

Same app, second build tool

~30 min

Quick check

Which Gradle configuration should JUnit go in?

Go deeper

  1. 1Create a Gradle project with gradle init and run ./gradlew build.
  2. 2Add the same dependency you used in Maven to build.gradle.
  3. 3Learn why projects commit the Gradle wrapper (gradlew).

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge