Advanced Gradle Topics

Introduction

Once daily builds work, teams optimize speed, plan migrations from Maven, and debug stubborn failures. This chapter covers the Gradle daemon, build cache, a Maven-to-Gradle cheat sheet, Android/Kotlin pointers, and common troubleshooting commands.

Prerequisites

  • Chapters 0115 or equivalent experience

Build Performance

Gradle Daemon

The daemon keeps a JVM warm between builds:

properties
# gradle.properties — default on
org.gradle.daemon=true

Stop all daemons:

bash
./gradlew --stop

Parallel and Caching

properties
org.gradle.parallel=true
org.gradle.caching=true
bash
./gradlew build --parallel

Configuration cache (Gradle 8+ feature, optional):

bash
./gradlew build --configuration-cache

Fix compatibility issues in scripts before enabling in CI—speeds up large projects when supported.

Incremental Work

Java compile and test tasks skip work when inputs unchanged. Avoid clean on every dev build—use ./gradlew build and trust up-to-date checks.

SymptomIdea
Slow downloadsMirror / corporate Nexus; offline cache seed
Slow tests--tests filter locally; parallel forks in test task
Slow configurationUpgrade Gradle; reduce heavy build.gradle logic at config time

Migrate from Maven

File Mapping

MavenGradle
pom.xmlbuild.gradle + settings.gradle
mvnw (optional)gradlew (expected)
target/build/

Coordinates

MavenGradle
groupIdgroup
artifactIdproject name / archivesBaseName
versionversion

Dependency XML:

xml
<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.11.0</version>
</dependency>

Gradle:

gradle
implementation 'com.google.code.gson:gson:2.11.0'

Scope → Configuration

Maven scopeGradle configuration
compile (default)implementation
providedcompileOnly
runtimeruntimeOnly
testtestImplementation

Command Mapping

MavenGradle
mvn clean package./gradlew clean build
mvn test./gradlew test
mvn dependency:tree./gradlew dependencies
mvn install./gradlew publishToMavenLocal (with publish plugin)

Tools like Spring Initializr generate Gradle builds for new services; legacy Maven repos often stay on Maven until a deliberate migration.

Tip

When to Keep Maven

Stable libraries with mature parent POMs and enterprise standards may stay on Maven for years. Greenfield Spring Boot or Android work often starts on Gradle.

Android and Kotlin (Awareness)

  • Android Gradle Plugin (AGP) applies on top of Gradle for APK/AAB builds—not covered step-by-step here.
  • Kotlin JVM:
gradle
plugins {
    id 'org.jetbrains.kotlin.jvm' version '2.0.21'
}

Sources live in src/main/kotlin. Build scripts may use build.gradle.kts.

Official docs: Kotlin Gradle plugin, Android developers.

Troubleshooting Checklist

Wrong Java Version

bash
./gradlew -version
echo $JAVA_HOME

Align Gradle JVM in IDEA and java.toolchain in scripts.

Dependency Resolution Fails

bash
./gradlew dependencies --configuration compileClasspath
./gradlew build --refresh-dependencies --stacktrace

Verify coordinates on Maven Central, proxy settings, and repository order.

Encoding Issues

gradle
tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8'
}

Wrapper Not Executable (Unix)

bash
chmod +x gradlew

Useful Help Tasks

bash
./gradlew help
./gradlew tasks
./gradlew projects
./gradlew properties

Short Gradle intro in Java: Gradle. This track is the full path; Maven depth lives under /docs/maven.

FAQ

Gradle Enterprise / Develocity?

Commercial build scans and remote cache—optional at scale; not required for learning.

Can Maven and Gradle coexist in one repo?

Usually one build tool per root. Some repos hold separate folders—treat as separate projects.

./gradlew build slow first time?

Cold daemon + dependency download—normal; second build should be faster.

What comes next?

Gradle book recommendations.