117

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
  • 64,006
  • 65
  • 247
  • 399

5 Answers5

147

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

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

Snekse
  • 15,005
  • 10
  • 58
  • 76
Rene Groeschke
  • 26,722
  • 9
  • 66
  • 75
94

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
  • 107,539
  • 39
  • 157
  • 158
  • 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. – Stephen Friedrich Oct 23 '18 at 08:39
26

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
  • 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
  • 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
  • 28,341
  • 18
  • 134
  • 157
0

You can run:

./gradlew cleanBuildCache
./gradlew clean

It will force the gradle to rebuild.

acmpo6ou
  • 228
  • 4
  • 13