I’ve declared a custom task and defined the inputs and outputs to ensure the uptodate check works properly. So far, so good.
Now, on top of these inputs/outputs conditions, I would like to add another custom condition that flags whether the task is uptodate or not. For example, this custom condition will return true if the current day is Sunday and false on all other week days.
Can I do this with Gradle today? If so, how can it be done? Please note that my custom condition should not be applied exclusively but additive, like the inputs and outputs.
Thanks and regards, Etienne
You can use the upToDateWhen method to programatically determine if a tasks outputs are up to date.
However for your example task.onlyif() is probably a better fit (since the day of the week seems unlikely to actually affect whether a task is up to date)
For more info see 15.8 and 15.9 of the user guide http://www.gradle.org/docs/current/userguide/userguide_single.html#N10EC0
Hm, my challenge is more tricky. I want the inputs and outputs (based on directories) to be included in the uptodate check and I also want an additional condition to be included in the uptodate check. If any of them (inputs/outputs/custom) says that the task is outdated, the task should be run. The problem with task.onlyIf() is that it works exclusively, i.e. if it returns false, it does not matter if the inputs and outputs are ‘dirty’ or not (also, the task will be marked as skipped and not as uptodate).
If there is no API to add a custom condition that does not act exclusively in the decision whether the task is uptodate or not, I wonder if there is an API that allows me to check if the inputs and outputs of a given task are uptodate, something like boolean TaskManager.areInputsUptodate(task) (I’m making that API up for clarity). I guess Gradle must have this kind of API internally anyway to check the uptodate status of a task.
Note that using the weekday in my custom check is not my real use case, but I think it is a simple to understand example. In my real use case, I want to check the state of my database (on top of some config files declared as inputs and some generated source code declared as outputs).
Any help is appreciated!
I got it working using the TaskOutputs#upToDateWhen API. I also created a new feature request to have an API TaskInputs#upToDateWhen.
I consider this thread as ‘answered’.