Gradle doFirst()执行顺序 [英] Gradle doFirst() Execution Order

查看:631
本文介绍了Gradle doFirst()执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Gradle构建脚本中确定doFirst方法的顺序如何?我有以下示例脚本包含两个doFirst方法。我知道它们是可加性的,因为它们都执行,但是这种情况发生的顺序看起来倒退了:

 任务初始化
任务depTask(dependsOn:初始化)

初始化{
doFirst {
println'在初始化(配置)中处理doFirst'
}

println'处理初始化(配置)'
}

depTask {
println'处理depTask(配置)'
}

depTask << {
println'执行depTask(执行)'
}

初始化<< {
println'执行初始化(执行)'
}

initialize.doFirst {
println'执行doFirst初始化(执行)'
}

此脚本的输出为:

<$ p
处理初始化(配置)
处理depTask(配置)
执行初始化(执行)的doFirst
处理初始化(配置)中的doFirst
执行初始化(执行)
执行depTask(执行)

定义第一个doFirst函数在初始化任务中。第二个是在配置块之外定义的。为什么第一个实例在第二个实例之前没有执行?执行顺序看起来落后。我希望第一个在配置定义中首先执行。任何帮助理解这一点将不胜感激。

解决方案

initialize {doFirst {...}} code>和 initialize.doFirst {...} 完全一样。两个语句都在任务的动作列表的前面插入一个动作。因此,稍后插入的动作(在这种情况下进一步放在脚本中)将首先执行。


How is the order of the doFirst method determined in a gradle build script? I have the following sample script that contains two doFirst methods. I understand that they are additive, as they both execute, but the order that this occurs looks backward:

task initialize
task depTask(dependsOn: initialize)

initialize {
    doFirst {
        println 'processing doFirst in initialization (configuration)'
    }

    println 'processing initialize (configuration)'
}

depTask {
    println 'processing depTask (configuration)'
}

depTask << {
    println 'executing depTask (execution)'
}

initialize << {
    println 'executing initialize (execution)'
}

initialize.doFirst {
    println 'executing doFirst on initialize (execution)'
}

The output from this script is:

processing initialize (configuration)
processing depTask (configuration)
executing doFirst on initialize (execution)
processing doFirst in initialization (configuration)
executing initialize (execution)
executing depTask (execution)

The first "doFirst" function is defined in the initialize task. The second is defined outside of the configuration block. Why doesn't the first instance execute before the second one? The order of execution looks backward. I would have expected the first one, inside the configuration definition, to execute first. Any help understanding this would be appreciated.

解决方案

initialize { doFirst { ... } } and initialize.doFirst { ... } are the exact same thing. Both statements are inserting an action at the front of the task's action list. Hence the action that gets inserted later (in this case further down in the script) will get executed first.

这篇关于Gradle doFirst()执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆