124

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?

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406

5 Answers5

156

Try to run your build with -C rebuild that rebuilds Gradle's cache.

In newer versions of Gradle, use --rerun-tasks

Snekse
  • 15,474
  • 10
  • 62
  • 77
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
98

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.

cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • 14
    This 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
  • 5
    This 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. – eekboom Oct 23 '18 at 08:39
27

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.

c_maker
  • 19,076
  • 1
  • 24
  • 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
  • 6
    No. 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
  • 1
    normally 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
5

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 }
    }
  }
}
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
-1

You can run:

./gradlew clean

It will force the gradle to rebuild.

acmpo6ou
  • 840
  • 1
  • 12
  • 21