Quick revision
The short answer
Before running a task, Gradle takes a fingerprint (a hash) of every declared input and output and compares it with the fingerprint saved after the task's previous run. If nothing changed and the outputs are still there, the task is marked UP-TO-DATE and skipped. Custom tasks go wrong in two ways: an input that changes on every run, such as a timestamp, makes the task rerun forever, and an input that was never declared, such as a system property the task reads, makes Gradle skip the task when it should have run.
What to remember
In this answer, we saw that Gradle fingerprints a task's declared inputs, its implementation and its outputs, and skips the task as UP-TO-DATE when all of them match the previous execution in the same workspace. A volatile input such as a timestamp makes a task and everything downstream rerun on every build, while an undeclared input such as a system property makes Gradle skip a task that should have run. Overlapping outputs disable the check and caching. The cure is the same for all of them: declare every input with @Input or @InputFiles, every output with @OutputFile or @OutputDirectory, give each task its own output location, and choose a relative path sensitivity for files.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Before running a task, Gradle takes a fingerprint (a hash) of every declared input and output and compares it with the fingerprint saved after the task's previous run. If nothing changed and the outputs are still there, the task is marked UP-TO-DATE and skipped. Custom tasks go wrong in two ways: an input that changes on every run, such as a timestamp, makes the task rerun forever, and an input that was never declared, such as a system property the task reads, makes Gradle skip the task when it should have run.
Why this question is asked
Up-to-date checks are the reason a second Gradle build of an unchanged project takes seconds instead of minutes. They also explain two of the most confusing build bugs: a task that runs every single time for no visible reason, and a task that silently produces stale output. Interviewers ask this at the ten-year level because fixing either bug needs a real picture of what Gradle tracks. In this answer, we will recap what inputs and outputs are, follow how Gradle decides to skip a task, reproduce both kinds of mistake in a custom task, and then fix them with the right annotations and path sensitivity.
1A Quick Recap of Task Inputs and Outputs
A Gradle task is a unit of work, such as compiling Java code or generating a file. Every task has inputs, the things that can change what it produces, and outputs, the files or directories it writes. For compileJava, the inputs are the source files, the compile classpath and the compiler options, and the output is the classes directory.
Gradle does not guess these. Built-in tasks declare them, and custom tasks must declare them too, usually by putting annotations such as @Input or @OutputDirectory on the task's properties. A property is just a getter on the task class, often of a lazy type such as Property<String> or ConfigurableFileCollection that Gradle reads when the task is about to run.
Gradle first configures the tasks by running the build scripts, then executes the ones that were asked for. The up-to-date check happens during execution, just before each task would run.
2How Gradle Decides a Task Is UP-TO-DATE
Just before executing a task, Gradle computes a fingerprint of its inputs. For a simple value like a String or a boolean, the fingerprint is a hash of the value. For files, it hashes each file's content together with its path, normalized according to the path sensitivity we chose. It also hashes the task's own implementation, meaning the class of the task and of any actions added to it, so changing the task's code counts as a change too.
Gradle then compares this with the fingerprint it stored after the last successful execution of the same task in the same project directory. It also checks that the outputs still exist and have not been changed by someone else since then. Only if both inputs and outputs match does it print UP-TO-DATE and skip the work. If anything differs, it runs the task and stores a new fingerprint afterwards.
- Does the task declare outputs?YesFingerprint the inputsNoRun it: never up to date
- Fingerprint inputs and task codevalues, file contents, normalized paths
- Same as the previous execution?YesCheck the outputsNoRun the task
- Outputs present and unchanged?YesSkip: UP-TO-DATENoRun the task
- Store the new fingerprintsafter a successful run
5 steps in order. Play them, or step through them one at a time.
The comparison is always against the last execution in this workspace.
Notice one more rule: a task that declares no outputs at all is never up to date, because Gradle has nothing to compare. So a custom task with only a doLast block always runs. That is correct behaviour, not a bug, but it surprises people who expected Gradle to be clever about it.
3Mistake 1: An Input That Changes on Every Run
Here is a custom task that writes a small properties file into the resources of the jar. It looks harmless, and it reruns on every build.
The builtAt input is a new timestamp each time, so the fingerprint never matches and the task always runs. Worse, its output changes, so processResources, jar and every test task that uses the runtime classpath also see a changed input and rerun. One volatile value can turn a ten-second incremental build into a full rebuild.
The --info flag is the quickest way to find this. It prints the reason a task was not up to date, naming the exact property or file that changed. The fix is to remove the volatile value, or keep it out of anything that feeds the jar: put the timestamp only in release builds, or replace it with something stable such as the commit id, which changes only when the code changes.
4Mistake 2: An Input Gradle Was Never Told About
The opposite mistake is quieter and more dangerous. This task generates a configuration file whose content depends on a system property and on a template file, but declares neither.
Gradle only fingerprints what is declared. Here it sees no inputs, only one output that is still present, so the second build is considered unchanged and skipped. The file keeps the dev value while we believe we built for prod. The same happens when a task reads an environment variable, a file outside the project, or a value from a network call.
The fix is to make every such value a declared property of the task, set from the build script, so Gradle can see it.
5Fixing the Task With Annotations and Path Sensitivity
Here is the corrected task. Every value that affects the output is a property with an annotation, and the file input says how its path should be compared.
Notice what changed. The system property is read through providers.systemProperty, which Gradle also tracks for the configuration cache, and it reaches the task as an @Input. The template is an @InputFiles collection, so editing it reruns the task. Values that must not affect up-to-date checks, such as a logger or a verbose flag, would be marked @Internal instead.
Path sensitivity tells Gradle how much of a file's path matters. The default for file inputs is ABSOLUTE, which means the same file in /home/ana/shop and in /builds/agent7/shop gives different fingerprints. RELATIVE compares the path inside the project, NAME_ONLY only the file name, and NONE only the content. For up-to-date checks in one workspace this rarely matters, but for the build cache, which we will meet in other answers, ABSOLUTE makes every machine miss. The @CacheableTask annotation lets the task's outputs be stored in that cache at all.
6Overlapping Outputs: Two Tasks Writing the Same Place
The last mistake is giving two tasks the same output location, for example two code generators that both write into build/generated/sources. After task A runs, task B writes extra files into the same directory. On the next build Gradle sees files in A's output that A did not produce, so it cannot trust that the directory belongs to A.
When Gradle detects overlapping outputs, it treats the task as not up to date and disables caching for it, and with --info it tells us so. The fix is simple: give every task its own output directory, such as build/generated/sources/openapi and build/generated/sources/grpc, and add each directory to the source set separately.
| Mistake | Symptom | Fix |
|---|---|---|
| Timestamp or random value as input | Task and its consumers rerun every build | Remove it or use a stable value |
| Reads a system property or file directly | Skipped when it should run; stale output | Declare it as @Input or @InputFiles |
| No outputs declared | Always runs | Declare @OutputFile or @OutputDirectory |
| Default ABSOLUTE path sensitivity | Cache misses across machines | @PathSensitive(RELATIVE) or NONE |
| Two tasks share an output directory | Not up to date, caching disabled | One output directory per task |
7Follow-up questions
How do I see why a task was not up to date?
Run the build with --info, or look at the timeline of a build scan. For each task that executed, Gradle prints a short list of reasons, such as an input property whose value changed, an input file that was added or modified, or an output that was removed. That message names the exact property, which usually leads straight to the volatile or missing input.
What is the difference between @Input and @Internal?
An @Input property is part of the fingerprint, so changing it reruns the task. An @Internal property is ignored by up-to-date checks and caching; it is for values that cannot change the output, such as a logging level or a service object. Every property of a task type must carry one of the annotations, and Gradle's plugin validation, run with validatePlugins in a plugin project, reports any property that has none.
8Conclusion
In this answer, we saw that Gradle fingerprints a task's declared inputs, its implementation and its outputs, and skips the task as UP-TO-DATE when all of them match the previous execution in the same workspace. A volatile input such as a timestamp makes a task and everything downstream rerun on every build, while an undeclared input such as a system property makes Gradle skip a task that should have run. Overlapping outputs disable the check and caching. The cure is the same for all of them: declare every input with @Input or @InputFiles, every output with @OutputFile or @OutputDirectory, give each task its own output location, and choose a relative path sensitivity for files.
Open with
Gradle decides a task is UP-TO-DATE by fingerprinting everything the task declares: input values, the content and normalized paths of input files, the task's own implementation, and its outputs.
Then, if they want more
It compares that with what it stored after the last execution in the same workspace, and if nothing changed and the outputs are still intact, it skips the task. So everything depends on declarations. The two classic mistakes are opposites.
If a custom task has an input that changes every run, like a build timestamp, it reruns forever, and because its output changes, everything downstream such as jar and test reruns too.
If a task reads something it never declared, like System.getProperty or a template file read directly, Gradle cannot see the change and skips the task, so we ship stale output. A third problem is two tasks writing to the same directory: Gradle detects overlapping outputs and turns off up-to-date checks and caching for them.
I fix these by making every input a property with @Input, @InputFiles or @InputDirectory, every output an @OutputFile or @OutputDirectory, values that do not matter @Internal, and file inputs @PathSensitive RELATIVE so the build cache works across machines. To diagnose, I run with --info, which prints exactly which property changed.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Gradle user manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does Gradle decide that a task is UP-TO-DATE, and what mistakes in custom tasks...": https://scrivio-interview-library.netlify.app/java/gradle/cc2c3e5d65/