Build cache and verification
What you will learn
A Gradle build can skip unchanged work, restore previous task outputs or reuse the configured graph. These are three different mechanisms and need different evidence.
- Interpret UP-TO-DATE versus FROM-CACHE and configuration cache reuse.
- Design a clean-build and cache experiment with separate evidence.
- Attach a verification task to check and distinguish it from framework tests.
Prerequisite: Java packages, classpath and basic terminal compilation. Gradle CLI examples are static and not executed in this batch. The project is local and fictional; use only reviewed source and approved cached dependencies.
Cache experiment
# Same trusted local project and fixed JDK, no external dependencies.gradle --offline clean :app:build --no-build-cache --no-configuration-cachegradle --offline :app:build --no-build-cache --no-configuration-cachegradle --offline clean :app:build --build-cache --no-configuration-cachegradle --offline clean :app:build --build-cache --no-configuration-cache# Separate configuration-cache experiment on an unchanged build.gradle --offline :app:build --configuration-cachegradle --offline :app:build --configuration-cacheExpected qualitative outcomes: unchanged cacheable tasks may be UP-TO-DATE, clean build with populated cache may show FROM-CACHE, repeated configuration-cache compatible invocation may reuse configuration. Capture actual logs; no result is assumed.
Three kinds of reuse
Up-to-date checks compare declared inputs and outputs with the last successful local execution. Build cache can restore outputs of cacheable tasks for matching keys even after outputs are removed. Configuration cache stores configuration results and task graph state, not compiled class outputs. Incremental tasks may process only changed input files when execution is necessary. A clean task removes build outputs but may leave caches intact. Undeclared inputs, environment-sensitive outputs or side effects can invalidate assumptions. Not every task is cacheable; never promise a cache hit.
Verification and reproducibility
The Java plugin provides compileJava, test, jar and lifecycle tasks including check and build; build combines assemble and check. JVM test suites configure test dependencies, source sets and test tasks. The optional suite fragment is not part of this dependency-free fixture. The app attaches a custom JavaExec verification task to check; it exercises four deterministic domain checks but is not a JUnit suite. Record execution outcomes rather than interpreting a successful build with NO-SOURCE test as proof that tests ran. Stable archive order/timestamps, pinned toolchain and dependencies help reproducibility. Compare independent clean builds with caches disabled and matching inputs; then test cache separately. A checksum match in this small fixture is not proof of general reproducibility.
Evidence and operational limits
Run only in a disposable approved workspace with fixed toolchain, no external repositories or secrets, and reviewed task scripts. Gradle 9.2.0 and Java 21 are the instructional lane. The bundle includes no Gradle or wrapper binary. No external test dependencies are installed. Direct javac and jar checks are recorded independently from hypothetical Gradle outcomes; no cache or task graph execution is claimed.
Optional JVM test suite
// Optional fragment for a separate approved JUnit dependency cache.testing { suites { val test by getting(JvmTestSuite::class) { useJUnitJupiter("5.11.4") } }}Separate example using the Java plugin and an approved JUnit 5.11.4 dependency cache; no test artifacts are supplied or resolved. Test framework coverage is next chapter.
Deterministic domain class
package example.books;
public final class Receipt { private Receipt() {} public static int totalCents(int count, int unitCents) { if (count < 0 || unitCents < 0) { throw new IllegalArgumentException("negative purchase"); } return Math.multiplyExact(count, unitCents); }}Complete library class, tested directly on JDK 21 with the application. Positive, zero, negative and overflow cases have fixed behavior.
App and manual checks
package example.bookstore;
import example.books.Receipt;import java.io.InputStream;import java.nio.charset.StandardCharsets;
public class Main { public static void main(String[] args) throws Exception { if (args.length == 1 && args[0].equals("--verify")) { if (Receipt.totalCents(3, 250) != 750 || Receipt.totalCents(0, 250) != 0) { throw new AssertionError("wrong total"); } try { Receipt.totalCents(-1, 250); throw new AssertionError("negative accepted"); } catch (IllegalArgumentException expected) { } try { Receipt.totalCents(Integer.MAX_VALUE, 2); throw new AssertionError("overflow accepted"); } catch (ArithmeticException expected) { } System.out.println("Receipt checks passed"); return; } try (InputStream in = Main.class.getResourceAsStream("/label.txt")) { if (in == null) throw new IllegalStateException("missing label"); String label = new String(in.readAllBytes(), StandardCharsets.UTF_8).strip(); System.out.println(label + ": " + Receipt.totalCents(3, 250)); } }}Complete application. Direct JDK classpath run prints Total cents: 750 and --verify prints Receipt checks passed. Resource label.txt is packaged from src/main/resources. Gradle JavaExec and check linkage remain untested.
Common mistakes
Do not compare a cached output against itself as proof of clean-build reproducibility. Do not interpret NO-SOURCE as passing actual tests. Avoid task actions that read undeclared inputs or mutate unrelated files; do not turn off verification to get a cache hit.
Practice
Design a five-run evidence log: two clean cache-disabled builds, one unchanged rerun, a cache-populating build and a clean cache-enabled repeat. State what hashes and task outcomes would support each claim. Explain why the custom check is not JUnit.
Use the manual practice hints; explain your reasoning before consulting the private review solution.
Official reading: cache · incremental · configcache · suite · security.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build cache and verification — knowledge check
Gradle model and build engineering knowledge check; source-reviewed, not Gradle-executed.
What does FROM-CACHE mean?
What does configuration cache reuse?
Does clean erase all Gradle caches?
What does NO-SOURCE for test establish?
How should clean reproducibility be assessed?