Gradle --rerun-tasks forces execution without clearing prior task outputs
0 reputation · 06 Sept 2022, 10:40 UTC
0 reputation · 06 Sept 2022, 10:40 UTC
Assuming Gradle 8.x, a task with declared outputs can be marked UP-TO-DATE when input fingerprints are unchanged, or restored FROM-CACHE from the build cache. Passing --rerun-tasks invalidates the up-to-date check and forces the task action to run, but it is unclear whether Gradle first removes existing files in the task's output directories.
The precise goal is to determine whether a forced rerun overwrites previous outputs cleanly or leaves stale files alongside newly written ones. This matters for tasks that append to fixed output locations or do not delete their output directory before writing, because leftover files can be packaged or published unintentionally.
Does Gradle clear declared output directories before executing a forced task action under --rerun-tasks? If not, which files from the prior execution are treated as stale by the build cache? How should a task be annotated or configured so repeated forced reruns do not duplicate or retain stale output files?
35775 reputation · 06 Sept 2022, 20:49 UTC
No. Passing --rerun-tasks forces Gradle to execute a task action even when its inputs are unchanged, but it does not delete or clean the task's declared output directories beforehand. Any files left from a previous run remain in place unless the task implementation explicitly removes them.
--rerun-tasks is equivalent to setting Task.outputs.upToDateWhen { false } for that invocation. It bypasses the up-to-date check and the build-cache lookup, but it performs no filesystem cleanup.@OutputDirectory or @OutputFiles locations are captured as that task's outputs. Stale files from earlier executions are therefore stored in the build cache and will be restored on subsequent cache hits.Tasks that append to fixed locations (log aggregators, manifest mergers, custom generators) or that write to a directory without first clearing it will accumulate duplicate or obsolete files on repeated --rerun-tasks invocations. Those stale files end up in published artifacts (JARs, ZIPs, Docker layers) and pollute the build cache for every machine that restores that cache entry.
@TaskAction
void generate() {
project.delete(outputDir)
outputDir.mkdirs()
// write fresh files
}
clean before forced reruns when you control the invocation:
./gradlew clean myTask --rerun-tasks
@OutputFiles instead of whole directories when the task produces a known, fixed set of files. Gradle then tracks only those files.@OutputDirectory 'build/out' and writes a timestamped file in @TaskAction without deleting the directory first.--rerun-tasks and list build/out; you will see both timestamped files../gradlew clean, then the task once, then again with --rerun-tasks; only one file appears, confirming clean removes prior outputs.--scan) or inspect ~/.gradle/caches to see the captured output files after a --rerun-tasks run that left stale files.Does your task use incremental task inputs (@InputChanges)? --rerun-tasks bypasses incremental analysis entirely, so the task receives no change information and must handle a full rebuild itself. If the task relies on incremental deltas to decide what to delete, it will not work under --rerun-tasks.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.