Course topics

By WebNest Studio Engineering Team

Advanced Java Tutorial

Build Tools: Gradle Basics

Gradle is a build automation tool that, like Maven, manages dependencies and orchestrates compiling, testing, and packaging — but it takes a different approach. Instead of a declarative XML file, Gradle build scripts are written in a real programming language: either Groovy (build.gradle) or Kotlin (build.gradle.kts). This makes Gradle scripts more flexible and often faster, at the cost of a steeper learning curve than Maven's fixed structure.

Gradle is the default build tool for Android development and is widely used in large, performance-sensitive Java and Kotlin projects, largely because of its incremental build and caching capabilities.

Why it matters

Gradle is the build tool of Android and of a growing share of server-side Java, including Spring's own projects. Its build files are code, not XML, which makes them shorter and more flexible, and its incremental builds are fast. You are as likely to open a project built with Gradle as with Maven.

Anatomy of a build.gradle File

A Gradle build script typically applies plugins, declares dependency repositories, and lists dependencies — conceptually similar to a Maven pom.xml, but expressed as executable code rather than a fixed XML schema.

  • plugins { } — applies build capabilities, such as the java or application plugin.
  • repositories { } — declares where to fetch dependencies from, commonly mavenCentral().
  • dependencies { } — lists libraries using configurations like implementation, testImplementation, and runtimeOnly.
  • tasks — Gradle work is organized into tasks (compileJava, test, jar, build), and you can define custom tasks directly in the script.

Dependency Configurations

Gradle's dependency configurations are more expressive than Maven's scopes. implementation is the standard choice for a dependency your code needs at compile and runtime, but that consumers of your library don't need to see on their own compile classpath (this speeds up compilation for large multi-module projects). api exposes a dependency to consumers as well. testImplementation is scoped only to test source sets, mirroring Maven's test scope.

Gradle vs. Maven: Flexibility and Performance

Maven's XML is declarative and rigid by design — every project of a given type looks structurally similar, which makes it easy to read but harder to customize. Gradle's script-based approach lets you write conditional logic, custom tasks, and multi-project build logic directly, which is powerful for complex builds but means two Gradle projects can look quite different from each other. Performance-wise, Gradle's incremental builds, build cache, and daemon process (a long-running background JVM that avoids repeated JVM startup cost) generally make repeated builds noticeably faster than Maven, especially on large codebases with many modules.

Example

A minimal build.gradle (Groovy DSL) with dependencies and a custom task

Java
plugins {
    id 'java'
    id 'application'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.google.guava:guava:33.0.0-jre'
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

application {
    mainClass = 'com.webnest.orders.Main'
}

test {
    useJUnitPlatform()
}

tasks.register('printVersion') {
    doLast {
        println "Building version " + project.version
    }
}

// Terminal: gradle build
Output
> Task :compileJava
> Task :test
> Task :jar
> Task :build

BUILD SUCCESSFUL in 2s
5 actionable tasks: 5 executed

Common Mistakes

  • Using implementation when a dependency truly needs to be exposed to consumers of a library module — that requires api instead.
  • Assuming build.gradle syntax is identical between the Groovy DSL and the Kotlin DSL — method calls, string quoting, and typing rules differ between the two.
  • Ignoring Gradle daemon warnings or disabling the daemon in CI without understanding the performance trade-off it introduces.
  • Writing overly clever custom task logic in build.gradle that becomes hard for teammates to reason about — Gradle's flexibility is a double-edged sword.

Exercise

This exercise needs Gradle installed, so it does not run in the playground. Write a build.gradle in the Groovy DSL for a Java project: apply the Java plugin, use Maven Central, add JUnit Jupiter 5.10.0 for the tests, make the test task use the JUnit Platform, and register a custom task named hello that prints a message. Then give the commands that run the custom task and the full build.

Expected result: gradle hello prints the message, and gradle build compiles, tests and creates the JAR in build/libs.

Java · starter code
// TODO: plugins

// TODO: repositories

// TODO: dependencies

// TODO: test configuration

// TODO: the custom task hello

# TODO: the two commands
Show hints
  • Test dependencies use the testImplementation configuration.
  • A task is registered with tasks.register('hello') { doLast { ... } }.
Show solution
Java · solution
plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

test {
    useJUnitPlatform()
}

tasks.register('hello') {
    doLast {
        println 'Hello from Gradle'
    }
}

gradle hello
gradle build

Quick quiz

1. In which languages can a Gradle build file be written?

  • A. XML only
  • B. Groovy or Kotlin
  • C. JSON
  • D. YAML
Show answer

B. build.gradle uses Groovy, and build.gradle.kts uses Kotlin.

2. What is the difference between implementation and testImplementation?

  • A. There is none
  • B. testImplementation is faster
  • C. implementation is for plugins
  • D. implementation is for the main code and is packaged; testImplementation is available only to the tests
Show answer

D. Keeping test libraries out of the main classpath keeps the final artifact small.

3. What is the Gradle wrapper, gradlew?

  • A. A script in the project that downloads and runs the exact Gradle version the project needs
  • B. A plugin for IDEs
  • C. A code formatter
  • D. A test runner
Show answer

A. Everyone, including the build server, then uses the same version without installing Gradle.

Interview questions

What are the main differences between Maven and Gradle?

Show answer

Maven is configured declaratively in XML and follows a fixed lifecycle and strict conventions, which makes builds uniform and easy to understand. Gradle's build file is a script in Groovy or Kotlin, so it is shorter and can express custom logic. Gradle is usually faster, because it runs only the tasks whose inputs have changed, caches outputs and keeps a daemon running. Both use the same repositories and dependency coordinates.

Why is Gradle often faster than Maven?

Show answer

Gradle tracks the inputs and outputs of every task and skips a task whose inputs have not changed, marking it up to date. Its build cache can reuse outputs produced earlier, even on another machine. A long-lived daemon process avoids starting a new JVM for each build, and independent projects can be built in parallel. Maven, by default, re-runs the phases each time.

Key Points to Remember

  • Gradle build scripts are real code (Groovy or Kotlin DSL), unlike Maven's declarative XML.
  • Dependencies are declared under configurations like implementation, api, and testImplementation.
  • Gradle favors flexibility and incremental build performance; Maven favors convention and predictability.
  • Both tools resolve dependencies from repositories such as Maven Central.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

These examples need a JDK project with their libraries on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering