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
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.
-
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. Sep 26, 2014 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 ->... }}}
Jan 16, 2015 at 13:49 -
-
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. Oct 23, 2018 at 8: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.
-
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_makerSep 3, 2011 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. Sep 3, 2011 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. Jun 20, 2012 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 }
}
}
}
You can run:
./gradlew cleanBuildCache
./gradlew clean
It will force the gradle to rebuild.