TExcelTAD
Home › Java Builds: Maven & Gradle › The Configuration Cache

The Configuration Cache

The build cache skips execution. The configuration phase still runs on every build: settings script, every build script, every plugin, then the task graph. In a large build that alone takes seconds, even when the answer is "everything is up to date". The configuration cache stores the result of configuration (the task graph with every task's configured inputs) and, on the next build with the same requested tasks and unchanged build logic, loads it instead of running any script.

gradle init already enables it (org.gradle.configuration-cache=true); otherwise use --configuration-cache or that property.

Stored, reused, invalidated

The build script prints a line while it is being configured, which makes it easy to see when configuration actually runs:

bash · try it yourself
mkdir -p cc/src/main/java/demo && cd cc
echo 'rootProject.name = "cc"' > settings.gradle.kts
echo 'org.gradle.configuration-cache=true' > gradle.properties
cat > build.gradle.kts <<'KTS'
plugins { java }
println("configuring the build script")
KTS
echo 'package demo; public class A { }' > src/main/java/demo/A.java
echo "--- 1"; gradle compileJava | grep -v -E '^$|BUILD|actionable|Starting a Gradle'
echo "--- 2"; gradle compileJava | grep -v -E '^$|BUILD|actionable|Starting a Gradle'
echo "--- 3"; gradle compileJava -Pfoo=bar | grep -v -E '^$|BUILD|actionable|Starting a Gradle'
echo "// a comment" >> build.gradle.kts
echo "--- 4"; gradle compileJava | grep -v -E '^$|BUILD|actionable|Starting a Gradle'
--- 1
Calculating task graph as no cached configuration is available for tasks: compileJava
> Configure project :
configuring the build script
> Task :compileJava
Configuration cache entry stored.
--- 2
Reusing configuration cache.
> Task :compileJava UP-TO-DATE
Configuration cache entry reused.
--- 3
Reusing configuration cache.
> Task :compileJava UP-TO-DATE
Configuration cache entry reused.
--- 4
Calculating task graph as configuration cache cannot be reused because file 'build.gradle.kts' has changed.
> Configure project :
configuring the build script
> Task :compileJava UP-TO-DATE
Configuration cache entry stored.
  1. No entry yet: Gradle configures (our println runs) and stores the result.
  2. Same tasks, nothing changed: Reusing configuration cache., and the script didn't run.
  3. An extra -P property was reused too: Gradle records which values configuration actually read, and nothing read foo. Had the script called providers.gradleProperty("foo"), a new value would have invalidated the entry.
  4. Editing a build script, even adding a comment, invalidates the entry, and Gradle names the reason.

The cache key covers the requested tasks, the build logic (scripts, buildSrc, plugins), and every value configuration read: Gradle properties, system properties, environment variables and files read through the providers API.

What configuration-cache compatibility means

To be cached, the task graph must be serialisable, and a task must not reach back into the live build model while executing, because on a cache hit that model doesn't exist. The classic violation is using project inside a task action:

bash · try it yourself
mkdir bad && cd bad
echo 'rootProject.name = "bad"' > settings.gradle.kts
cat > build.gradle.kts <<'KTS'
version = "1.4.0"
tasks.register("printVersion") {
    doLast { println("version " + project.version) }       // reads Project at execution time
}
tasks.register("printVersionFixed") {
    val v = project.version.toString()                       // read during configuration
    doLast { println("version $v") }
}
KTS
gradle --configuration-cache printVersion 2>&1 | grep -E "problem|^- |invocation|BUILD|version"
gradle --configuration-cache printVersionFixed -q
1 problem was found storing the configuration cache.
- Task `:printVersion` of type `org.gradle.api.DefaultTask`: invocation of 'Task.project' at execution time is unsupported with the configuration cache.
[Incubating] Problems report is available at: file:///home/learner/bad/build/reports/problems/problems-report.html
BUILD FAILED in 9s
Configuration cache entry discarded with 1 problem.
version 1.4.0

Gradle refuses to store the entry and reports each problem with its location, plus an HTML problems report under build/reports/problems/. The fix is always the same shape: capture the values you need while configuring (in a val, or better a Provider), and use only those inside the action.

Most popular plugins are compatible today. For a build that isn't yet, org.gradle.configuration-cache.problems=warn turns problems into warnings while you migrate.

Two caches, two phases

Configuration cache Build cache
Skips the configuration phase (scripts, plugins, graph calculation) task execution
Key requested tasks + build logic + values configuration read each task's inputs
Where the project's .gradle/configuration-cache/ ~/.gradle/caches/build-cache-1, optionally remote
Shared between machines? no yes, with a remote cache
Message Reusing configuration cache. FROM-CACHE on a task

They're independent and complementary; a fast incremental build uses both.

Note: In Gradle 9 the configuration cache is the preferred mode of execution, and builds without it print Consider enabling configuration cache to speed up this build (you've seen that line in earlier sessions). It is enabled by default only in projects that ask for it, as gradle init projects do.

Test yourself

What does the configuration cache let Gradle skip on a hit?

  1. Compiling Java
  2. Running settings and build scripts and calculating the task graph
  3. Downloading dependencies
  4. Running tests

Why is project.version inside doLast { } a configuration-cache problem?

  1. version is private
  2. On a cache hit, the Project model doesn't exist at execution time, so tasks must not access it then
  3. doLast is deprecated
  4. Kotlin can't read it

You add a comment to build.gradle.kts. What happens on the next build?

  1. The entry is reused
  2. The configuration cache entry is invalidated because a build script changed
  3. The build cache is cleared
  4. Gradle fails