26 August 2014

As discussed in a previous post, Gradle supports multiple ways to perform incremental builds, such as caching based on task inputs, the use of the upToDateWhen closure on a task’s outputs or the use of the task’s onlyIf() method to control execution, to name a few. Sometimes, however, you want a task to only execute if another cachable task has executed. For instance, maybe you want to create an archive of some generated source, but only if the source has been updated/re-generated. One such way to do this is to make use of the getDidWork() method of the task to determine if the task actually executed or was skipped/up-to-date:

class CustomTask extends DefaultTask {

    @TaskAction
    void doSomething() {
        if(project.tasks.getByName('otherTask').getDidWork()) {
            // Do this task's work here!
        }
    }
}

By using the getDidWork method on the other task to determine if it executed, we can avoid having to rely upon the input/output of the task to determine if the downstream task should execute, thus giving us better control over what triggers the tasks in our project.

comments powered by Disqus