Incremental Builds: UP-TO-DATE
Before a task runs, Gradle takes a fingerprint of its inputs (file contents, property values, the task's implementation and classpath) and compares it, and the state of its outputs, with what it recorded after the last execution. If nothing changed, the task is UP-TO-DATE and is skipped. This is the "content hashes, not timestamps" idea from Build Fundamentals, applied to every task.
Watching it happen
A two-project build: app uses lib. The session changes things one at a time and prints which tasks did real work:
mkdir -p inc/lib/src/main/java/lib inc/app/src/main/java/app && cd inc
cat > settings.gradle.kts <<'KTS'
rootProject.name = "inc"
include("lib", "app")
KTS
echo 'plugins { `java-library` }' > lib/build.gradle.kts
printf 'plugins { application }\ndependencies { implementation(project(":lib")) }\napplication { mainClass = "app.Main" }\n' > app/build.gradle.kts
cat > lib/src/main/java/lib/Greeter.java <<'SRC'
package lib;
public class Greeter {
public static String greet(String name) { return "Hello, " + name; }
}
SRC
cat > app/src/main/java/app/Main.java <<'SRC'
package app;
public class Main {
public static void main(String[] args) { System.out.println(lib.Greeter.greet("Gradle")); }
}
SRC
work() { gradle "$@" | grep -E '^> Task' | grep -v -E 'UP-TO-DATE|NO-SOURCE' | sed 's/> Task //' | paste -sd' ' -; }
echo "1. first build: $(work :app:jar)"
echo "2. nothing changed: $(work :app:jar)"
sed -i 's/"Hello, "/"Hi, "/' lib/src/main/java/lib/Greeter.java
echo "3. lib method body edited: $(work :app:jar)"
sed -i 's/public static String greet/public static String greet(String n, int times) { return n.repeat(times); }\n public static String greet/' lib/src/main/java/lib/Greeter.java
echo "4. lib API changed: $(work :app:jar)"
touch app/src/main/java/app/Main.java
echo "5. touch Main.java: $(work :app:jar)"
rm app/build/libs/app.jar
echo "6. output deleted: $(work :app:jar)"
gradle :app:run -q1. first build: :lib:compileJava :app:compileJava :app:classes :app:jar
2. nothing changed:
3. lib method body edited: :lib:compileJava
4. lib API changed: :lib:compileJava :app:compileJava :app:classes
5. touch Main.java:
6. output deleted: :app:jar
Hi, Gradle
Line by line:
- Everything runs once.
- Nothing changed, nothing runs.
- A change inside a method body:
librecompiles, but:app:compileJavadoesn't run. (lib's JAR isn't even in the graph: within one build,appcompiles againstlib's classes folder.) Gradle extracts the ABI (the public signatures) oflib's classes, andapp's compile classpath fingerprint only depends on that. This is compile avoidance; in a large build it saves most of the recompilation. - A new public method changes the ABI, so
apprecompiles too, but:app:jarstays up to date: the recompiledMain.classcame out byte-for-byte identical, and the JAR's inputs are the class files. Even when recompiling,javacis given only the classes that may be affected (incremental Java compilation). touchchanges the timestamp, not the content: still up to date. Make-style tools would rebuild here.- A deleted output is detected and recreated.
Why did a task run?
--info makes Gradle explain each decision:
mkdir -p why/src/main/java/demo && cd why
echo 'rootProject.name = "why"' > settings.gradle.kts
echo 'plugins { java }' > build.gradle.kts
echo 'package demo; public class A { }' > src/main/java/demo/A.java
gradle -q compileJava
echo 'package demo; public class A { int x; }' > src/main/java/demo/A.java
gradle --info compileJava | grep -A2 "is not up-to-date because"
gradle --info compileJava | grep "Skipping task ':compileJava'"Task ':compileJava' is not up-to-date because:
Input property 'stableSources' file /home/learner/why/src/main/java/demo/A.java has changed.
Compilation mode: in-process compilation
Skipping task ':compileJava' as it is up-to-date.
A model of the up-to-date check
import hashlib
def fingerprint(files, props):
h = hashlib.sha256()
for name in sorted(files):
h.update(name.encode() + b"\0" + files[name].encode() + b"\0")
for key in sorted(props):
h.update(f"{key}={props[key]}".encode())
return h.hexdigest()[:12]
history = {} # task -> (input fingerprint, output fingerprint)
def run(task, inputs, props, outputs):
before = history.get(task)
now_in = fingerprint(inputs, props)
if before and before[0] == now_in and before[1] == fingerprint(outputs, {}):
return f"{task}: UP-TO-DATE"
outputs["A.class"] = "bytecode of " + inputs["A.java"]
history[task] = (now_in, fingerprint(outputs, {}))
return f"{task}: executed"
outs = {}
src = {"A.java": "class A {}"}
print(run("compileJava", src, {"release": 21}, outs))
print(run("compileJava", src, {"release": 21}, outs)) # same content
print(run("compileJava", src, {"release": 17}, outs)) # input property changed
outs.clear()
print(run("compileJava", src, {"release": 17}, outs)) # outputs deletedcompileJava: executed
compileJava: UP-TO-DATE
compileJava: executed
compileJava: executed
Content, not timestamps; inputs and outputs; properties as well as files. The record lives in the project's .gradle/ folder. Delete it (or run --rerun-tasks) and everything runs again.
Tip: A custom task is only incremental if it declares its inputs and outputs (
@Input,@InputFiles,@OutputFile,@OutputDirectory, as in Lazy Configuration). One undeclared input, such as an environment variable read inside the action, makes it skip when it shouldn't: a correctness bug, not just a performance one.
Test yourself
You touch a source file without changing its content. Does Gradle recompile?
- Yes, the timestamp changed
- No, fingerprints are based on content
- Only with --info
- Only for tests
You change only a method body in lib. Why doesn't app:compileJava run?
- Gradle forgot
- Compile avoidance: app's compile inputs depend only on lib's ABI, which didn't change
- app doesn't depend on lib
- Because jar is up to date
How do you find out why Gradle re-executed a task?
- gradle tasks
- Run with --info and read the 'is not up-to-date because' lines
- gradle --dry-run
- Delete .gradle/