【问题标题】:What's the difference between Gradle's TaskExecutionListener and its TaskActionListener?Gradle 的 TaskExecutionListener 和它的 TaskActionListener 有什么区别?
【发布时间】:2014-03-17 13:09:27
【问题描述】:

【问题讨论】:

    标签: gradle


    【解决方案1】:

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

    考虑下面的脚本,我们有两个任务,每个任务有两个动作,还有一个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'
    

    因为是第一次执行,所以两个任务的动作都会被执行。尽管如此,您仍然可以看到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 方法,因为该任务被认为是最新的并且其操作被跳过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 2017-11-13
      • 2015-10-26
      相关资源
      最近更新 更多