什么';Gradle和x27之间的区别是什么;s TaskExecutionListener及其TaskActionListener?

什么';Gradle和x27之间的区别是什么;s TaskExecutionListener及其TaskActionListener?,gradle,Gradle,和之间有什么区别?我想根本的问题是,“执行任务和执行其操作的任务之间有什么区别?”执行任务时,它不仅仅执行操作——如果任务是最新的,它甚至根本不执行操作 考虑下面的脚本,其中我们有两个任务,每个任务有两个操作,还有一个outputs.upToDateWhenclosure。任务a从未被认为是最新的,而任务b始终被认为是最新的: task a { outputs.upToDateWhen { println "a - upToDateWhen"; false } doLast {

和之间有什么区别?我想根本的问题是,“执行任务和执行其操作的任务之间有什么区别?”

执行任务时,它不仅仅执行操作——如果任务是最新的,它甚至根本不执行操作

考虑下面的脚本,其中我们有两个任务,每个任务有两个操作,还有一个
outputs.upToDateWhen
closure。任务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"
    }
})
在第一次调用
gradleab
时,您会得到以下输出:

$ 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'
由于这是第一次执行,因此将执行两个任务的操作。不过,您可以看到,
TaskExecutionListener.beforeExecute
在检查
UpdateWhen
之前调用,而
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
方法,因为任务被认为是最新的,其操作被跳过

在执行任务时,它不仅仅执行操作,而且如果任务是最新的,它甚至根本不执行操作

考虑下面的脚本,其中我们有两个任务,每个任务有两个操作,还有一个
outputs.upToDateWhen
closure。任务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"
    }
})
在第一次调用
gradleab
时,您会得到以下输出:

$ 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'
由于这是第一次执行,因此将执行两个任务的操作。不过,您可以看到,
TaskExecutionListener.beforeExecute
在检查
UpdateWhen
之前调用,而
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
方法,因为任务被认为是最新的,其操作被跳过