Is there a way I can force a gradle task to run again, or reset all tasks back to the not UP-TO-DATE state?
5 Answers
Try to run your build with -C rebuild that rebuilds Gradle's cache.
In newer versions of Gradle, use --rerun-tasks
- 15,005
- 10
- 58
- 76
- 26,722
- 9
- 66
- 75
If you want just a single task to always run, you can set the outputs property inside of the task.
outputs.upToDateWhen { false }
Please be aware that if your task does not have any defined file inputs, Gradle may skip the task, even when using the above code. For example, in a Zip or Copy task there needs to be at least one file provided in the configuration phase of the task definition.
- 107,539
- 39
- 157
- 158
-
14This does nothing for me. I added it to a task and get "UP-TO-DATE". The funny thing is that it's a ZipTask and I deleted the destination archive. – maaartinus Sep 26 '14 at 21:52
-
5This is incredible for usage like this: `tasks.whenTaskAdded { theTask -> if (theTask.name.startsWith("dex")) { theTask.outputs.upToDateWhen { false } theTask.doLast { task ->... }}}` – Martin L. Jan 16 '15 at 13:49
-
Updated answer for case when task will not run. – cmcginty Jul 26 '16 at 06:58
-
Beware that nowadays when the gradle built cache is enabled this will not cause the task to run again, but its output will just be restored from cache. – Stephen Friedrich Oct 23 '18 at 08:39
You can use cleanTaskname
Let's say you have
:someproject:sometask1 UP-TO-DATE
:someproject:sometask2 UP-TO-DATE
:someproject:sometask3 UP-TO-DATE
And you want to force let's say sometask2 to run again you can
someproject:cleanSometask2
before you run the task that runs it all.
Apparently in gradle, every task that understands UP-TO-DATE also understand how to clean itself.
- 17,978
- 1
- 23
- 29
-
1'gradle clean' will clean everything for the project you are in. It basically deletes your main output folder which is '/build' by default. Is this what you were looking for? – c_maker Sep 03 '11 at 12:55
-
6No. That won't re-set up-to-date if up-to-date was captured as no output files for a given task. That is, if I had some part of the build break but succeed overall, the captured state is wrong, and I need to clear it. – Stefan Kendall Sep 03 '11 at 16:45
-
1normally the 'clean' task that deletes everyting in $buildDir is available in build scripts as it is introduced by the base plugin. – Rene Groeschke Jun 20 '12 at 20:46
I had a tough case where setting outputs.upToDateWhen { false } inside the task or adding the flag --rerun-tasks didn't help since the task's setOnlyIf kept being set to false each time I ran it.
Adding the following to build.gradle forced the execution of myTask:
gradle.taskGraph.whenReady { taskGraph ->
def tasks = taskGraph.getAllTasks()
tasks.each {
def taskName = it.getName()
if(taskName == 'myTask') {
println("Found $taskName")
it.setOnlyIf { true }
it.outputs.upToDateWhen { false }
}
}
}
- 28,341
- 18
- 134
- 157
You can run:
./gradlew cleanBuildCache
./gradlew clean
It will force the gradle to rebuild.
- 228
- 4
- 13