user4925383
user4925383

Reputation:

Force Gradle to run task even if it is UP-TO-DATE

I came across a situation, when everything is UP-TO-DATE for Gradle, although I'd still like it to run the task for me. Obviously it does not:

gradle test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE

BUILD SUCCESSFUL

Total time: 0.833 secs

I can force it to run test by running clean test, although this may be considered an overhead in some cases. Is there a way to force task execution no matter if Gradle believes it's UP-TO-DATE or not?

Upvotes: 85

Views: 60893

Answers (3)

MartinTeeVarga
MartinTeeVarga

Reputation: 10898

If you want to rerun all tasks, you can use command line parameter --rerun-tasks. However this is basically the same as doing a clean as it reruns all the tasks.

If you want to run a single task every time, then you can specify that it is never up-to-date:

mytask {
    outputs.upToDateWhen { false }
}

If you want to rerun a single task once and leave all the other tasks, you have to implement a bit of logic:

mytask {
    
    outputs.upToDateWhen { 
        if (project.hasProperty('rerun')) {
            println "rerun!"
            return false
        } else {
            return true
        }
    }
}

And then you can force the task to be re-run by using:

gradle mytask -Prerun

Note that this will also re-run all the tasks that depend on mytask.

Upvotes: 101

SKO
SKO

Reputation: 50

If it doesn't work even if you did every of these, you might need to check if they are enabled/disabled by some settings. For my case, it was disabled with checkstyle.enable=false in gradle.properties with a custom setting.

Upvotes: 0

Brad Turek
Brad Turek

Reputation: 2832

In Gradle v7.6, the new --rerun flag can tell an individual task to ignore up-to-date checks:

./gradlew theTask --rerun

Upvotes: 28

Related Questions