TExcelTAD
Home › Java Builds: Maven & Gradle › The Build Cache: FROM-CACHE

The Build Cache: FROM-CACHE

UP-TO-DATE only helps when a task's outputs are still where it left them. After gradle clean, on a fresh CI checkout, on a colleague's machine, or after switching back to a branch you built yesterday, the outputs are gone, although Gradle has produced exactly those outputs before. The build cache stores task outputs under a cache key computed from all of the task's inputs; a task whose key is already in the cache unpacks the stored outputs instead of running, and is reported FROM-CACHE.

It's off by default. Turn it on per build with --build-cache, or for the project with org.gradle.caching=true in gradle.properties.

Clean build, cached outputs

bash · try it yourself
mkdir -p cached/src/main/java/demo cached/src/test/java/demo && cd cached
echo 'rootProject.name = "cached"' > settings.gradle.kts
echo 'org.gradle.caching=true' > gradle.properties
cat > build.gradle.kts <<'KTS'
plugins { java }
repositories { mavenCentral() }
dependencies {
    testImplementation(platform("org.junit:junit-bom:5.14.4"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
tasks.test { useJUnitPlatform() }
KTS
echo 'package demo; public class Calc { public static int twice(int x) { return 2 * x; } }' > src/main/java/demo/Calc.java
cat > src/test/java/demo/CalcTest.java <<'SRC'
package demo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class CalcTest { @Test void doubles() { assertEquals(8, Calc.twice(4)); } }
SRC
gradle build | grep -E '^> Task|actionable'
gradle clean -q
gradle build | grep -E '^> Task|actionable'
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :assemble
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
> Task :check
> Task :build
4 actionable tasks: 4 executed
> Task :compileJava FROM-CACHE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar
> Task :assemble
> Task :compileTestJava FROM-CACHE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test FROM-CACHE
> Task :check UP-TO-DATE
> Task :build
4 actionable tasks: 1 executed, 3 from cache

After clean, compileJava, compileTestJava and even test came back FROM-CACHE: the test results are outputs too, keyed by the test classes, the runtime classpath and the JVM settings, so tests whose inputs haven't changed don't run again. jar executed: packaging a JAR is cheaper than fetching it from a cache, so the jar task isn't cacheable on purpose.

The cache is shared and relocatable

The local cache lives in ~/.gradle/caches/build-cache-1 and is shared by every build of the user. Cache keys use paths relative to the project, so a second checkout of the same commit in another folder (a second CI workspace, a git worktree) hits the cache too:

bash · try it yourself
mkdir -p one/src/main/java/demo && cd one
echo 'rootProject.name = "demo"' > settings.gradle.kts
echo 'plugins { java }' > build.gradle.kts
echo 'package demo; public class A { public static int f() { return 42; } }' > src/main/java/demo/A.java
gradle --build-cache compileJava -q
cp -r ~/one ~/two && rm -rf ~/two/build ~/two/.gradle && cd ~/two
gradle --build-cache compileJava | grep '^> Task'
gradle --build-cache --info compileJava --rerun-tasks | grep -o "Build cache key for task ':compileJava' is [0-9a-f]*"
> Task :compileJava FROM-CACHE
Build cache key for task ':compileJava' is a08d7cefefdc788fd7f184f771ed01a2

two had never been built, yet compileJava came from the cache filled by one. (The cache key is a hash of the task's inputs; --rerun-tasks forces execution and the line shows the key.)

A remote cache for the team

The same mechanism over HTTP makes a remote build cache: CI builds of main push outputs, and developers' builds pull them, so a fresh checkout compiles almost nothing. Gradle has a built-in HTTP connector; the server can be Develocity, a Bazel-compatible cache, or any HTTP server that accepts PUT:

kotlin
// settings.gradle.kts
buildCache {
    local { isEnabled = true }
    remote<HttpBuildCache> {
        url = uri("https://cache.example.com/cache/")
        isPush = providers.environmentVariable("CI").isPresent   // only CI writes
        credentials {
            username = providers.environmentVariable("CACHE_USER").orNull
            password = providers.environmentVariable("CACHE_PASSWORD").orNull
        }
    }
}

Letting only CI push is the usual rule: developer machines have unreviewed changes and odd environments, and a bad entry in a shared cache affects everyone.

Note: A cache hit is only correct if the cache key captures every input. Tasks from well-written plugins declare them; custom tasks must do the same. A task that reads an undeclared environment variable or absolute path will happily restore wrong outputs. When a cache result looks impossible, --no-build-cache is the first experiment.

Test yourself

After gradle clean, why can compileJava be FROM-CACHE but not UP-TO-DATE?

  1. UP-TO-DATE needs the previous outputs to be present; the build cache restores outputs from stored entries
  2. Clean disables up-to-date checks forever
  3. FROM-CACHE means it was compiled faster
  4. They are the same thing

Why isn't the jar task cacheable?

  1. Jars can't be stored
  2. Re-creating a JAR is cheaper than downloading/unpacking it from a cache
  3. It has no inputs
  4. It's a bug

In a team remote cache, who should normally be allowed to push entries?

  1. Every developer
  2. Only CI builds
  3. Nobody
  4. Only the build engineer's laptop