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?
upToDateWhen { false } means “outputs are not up to date”. If any upToDateWhen spec returns false, the task is considered out of date. If they return true, Gradle does the normal behavior of checking input/output files.
Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.
Gradle will determine if a task is up to date by checking the inputs and outputs. For example, your compile task input is the source code. If the source code hasn't changed since the last compile, then it will check the output to make sure you haven't blown away your class files generated by the compiler.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With