gradle

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?


Solution

  • 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.