Resetting the UP-TO-DATE property of gradle tasks?

GroovyGradle

Groovy Problem Overview


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?

Groovy Solutions


Solution 1 - Groovy

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

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

Solution 2 - Groovy

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.

Solution 3 - Groovy

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.

Solution 4 - Groovy

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 }
    }
  }
}

Solution 5 - Groovy

You can run:

./gradlew cleanBuildCache
./gradlew clean

It will force the gradle to rebuild.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefan KendallView Question on Stackoverflow
Solution 1 - GroovyRene GroeschkeView Answer on Stackoverflow
Solution 2 - GroovycmcgintyView Answer on Stackoverflow
Solution 3 - Groovyc_makerView Answer on Stackoverflow
Solution 4 - GroovyMatthias BraunView Answer on Stackoverflow
Solution 5 - Groovyacmpo6ouView Answer on Stackoverflow