0

What's the difference between http://www.gradle.org/docs/current/javadoc/org/gradle/api/execution/TaskExecutionListener.html and http://www.gradle.org/docs/current/javadoc/org/gradle/api/execution/TaskActionListener.html? I guess the root question is, "What's the difference between a task executing and a task performing its actions?"

1 Answer 1

5

When executing a task, it does more than just perform the actions -- and if the task is up-to-date, it doesn't even perform the actions at all.

Consider the following script, where we have two tasks with two actions each as well as an outputs.upToDateWhen closure. Task a is never considered up-to-date, while task b is always considered up-to-date:

task a {
    outputs.upToDateWhen { println "a - upToDateWhen"; false }
    doLast { println "a.1" }
    doLast { println "a.2" }
}

task b {
    outputs.upToDateWhen { println "b - upToDateWhen"; true }
    doLast { println "b.1" }
    doLast { println "b.2" }
}

gradle.addListener(new TaskExecutionListener() {
    void beforeExecute(Task task) {
        println "beforeExecute of $task"
    }
    void afterExecute(Task task, TaskState state) {
        println "afterExecute of $task"
    }
})

gradle.addListener(new TaskActionListener() {
    void beforeActions(Task task) {
        println "beforeActions of $task"
    }
    void afterActions(Task task) {
        println "afterActions of $task"
    }
})

On the first invokation of gradle a b, you get the following output:

$ gradle a b
:a
beforeExecute of task ':a'
a - upToDateWhen
beforeActions of task ':a'
a.1
a.2
afterActions of task ':a'
afterExecute of task ':a'
:b
beforeExecute of task ':b'
b - upToDateWhen
beforeActions of task ':b'
b.1
b.2
afterActions of task ':b'
afterExecute of task ':b'

Since it is the first execution, actions for both tasks are performed. Still, you can see that TaskExecutionListener.beforeExecute is invoked before the upToDateWhen check, while TaskActionListener.beforeActions is invoked after the check.

On second execution, it gets more interesting:

$ gradle a b
:a
beforeExecute of task ':a'
a - upToDateWhen
beforeActions of task ':a'
a.1
a.2
afterActions of task ':a'
afterExecute of task ':a'
:b
beforeExecute of task ':b'
b - upToDateWhen
:b UP-TO-DATE
afterExecute of task ':b'

Here you can notice that the TaskExecutionListener methods are invoked for both tasks, while the TaskActionListener methods are not invoked for task b since the task is considered up-to-date and its actions are skipped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.