gpt4 book ai didi

gradle - Gradle的TaskExecutionListener和TaskActionListener之间有什么区别?

转载 作者:行者123 更新时间:2023-12-03 04:47:48 25 4
gpt4 key购买 nike

最佳答案

执行任务时,它不仅执行 Action -而且,如果任务是最新的,则根本不执行 Action 。

考虑以下脚本,其中我们有两个任务,每个任务有两个 Action 以及一个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"
}
})

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'

由于这是第一次执行,因此将执行两个任务的 Action 。不过,您仍然可以看到 TaskExecutionListener.beforeExecuteupToDateWhen检查之前被调用,而 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'

在这里您可以注意到,两个任务都调用了 TaskExecutionListener方法,而任务b没有调用 TaskActionListener方法,因为该任务被认为是最新的,并且其 Action 被跳过。

关于gradle - Gradle的TaskExecutionListener和TaskActionListener之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22455166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com