执行任务时,它不仅仅执行操作 - 如果任务是最新的,它甚至根本不执行任务.
考虑以下脚本,其中我们有两个任务,每个任务包含两个操作以及一个outputs.upToDateWhen闭包.任务a永远不会被认为是最新的,而任务b始终被认为是最新的:
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"
}
})
Run Code Online (Sandbox Code Playgroud)
在第一次调用时gradle a b,您将获得以下输出:
$ 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'
Run Code Online (Sandbox Code Playgroud)
由于它是第一次执行,因此执行两个任务的操作.仍然,您可以看到TaskExecutionListener.beforeExecute在upToDateWhen检查之前调用,而TaskActionListener.beforeActions在检查之后调用.
在第二次执行时,它变得更有趣:
$ 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'
Run Code Online (Sandbox Code Playgroud)
在这里,您可以注意到TaskExecutionListener为两个任务调用了TaskActionListener方法,而不为任务b调用方法,因为任务被认为是最新的并且其操作被跳过.
| 归档时间: |
|
| 查看次数: |
976 次 |
| 最近记录: |