Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gradle

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?

like image 934
automatictester Avatar asked Feb 11 '17 11:02

automatictester


People also ask

Is not up to date because task upToDateWhen is false?

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.

How do I run a default task in Gradle?

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.

How does Gradle check up to date?

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.


1 Answers

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.

like image 153
MartinTeeVarga Avatar answered Sep 28 '22 19:09

MartinTeeVarga