如何在执行Gradle任务的依赖项之前执行某些操作?

时间:2013-09-10 23:43:28

标签: gradle

我在build.gradle中有以下内容:

task aoeu << {
    println "**************************** during"
}

tasks.publish.dependsOn(aoeu)

tasks.publish.doFirst {
    println "************************* before"
}

tasks.publish.doLast {
    println "************************* after"
}

它的输出是:

:aoeu
**************************** during
:publish
************************* before
************************* after

但我真正需要的是''''在“之前”和“之后”之间发生。如何才能做到这一点? “发布”应该仅仅取决于“之前”吗?如果是这样,我如何保证它在其他依赖项之前发生?

3 个答案:

答案 0 :(得分:14)

据我所知,你的代码应该可以正常运行,但事实并非如此。 doFirst应该在doLast(执行阶段)之前运行的配置阶段执行。顺便说一句,这段代码工作正常:

正如Peter Niederwiser在此写的那样https://stackoverflow.com/a/9204159/2069368 doFirst在执行阶段作为第一个语句(在doLast之前)运行,因此您的代码运行正常。此示例将显示gradle构建中的执行顺序:

task publish {
    println "(1) before (run in configuration phase)" // this will run before gradle dependencies
}

task aoeu << {
    println "(2) during  (run in execution phase as last statement)"
}

tasks.publish.dependsOn(aoeu)
tasks.publish.doLast {
    println "(4) after  (run in execution phase as last statement)"
}

tasks.publish.doFirst {
    println "(3) after  (run in execution phase as FORST statement - before doLast/<<)"
}

它会返回

C:\>gradle publish
(1) before (run in configuration phase)
:aoeu
(2) during  (run in execution phase as last statement)
:publish
(3) after  (run in execution phase as FORST statement - before doLast/<<)
(4) after  (run in execution phase as last statement)

<强> [UPDATE]

这里http://gradle.1045684.n5.nabble.com/task-run-order-lt-lt-syntax-doLast-doFirst-etc-td3337481.html是一个很好的问题,有一个显示执行顺序的很好的例子。

阅读关于gradle生命周期的this article,它将帮助您理解它。

<强> [UPDATE] 如果您想在aoeu执行阶段运行任务publish,可以在aoeu.execute中致电publish.doFirst。但据我所知,不应该这样做。

task publish {
}

task aoeu << {
    println "(2) during  (run in execution phase as last statement)"
}

tasks.publish.doLast {
    println "(3) after  (run in execution phase as last statement)"
}

tasks.publish.doFirst {
    println "(1) after  (run in execution phase as FORST statement - before doLast/<<)"
    aoeu.execute()
}

将返回

C:\>gradle publish
:publish
(1) after  (run in execution phase as FORST statement - before doLast/<<)
(2) during  (run in execution phase as last statement)
(3) after  (run in execution phase as last statement)

我认为您希望在aoeu任务中运行publish。我认为最佳方式可以将publish任务划分为两个单独的任务,并使用dependsmustRunAfter来控制执行顺序。看看这个例子:

task publishInit << {
    println '(1)'
}

task aoeu << {
    println '(2)'
}

task publish << {
    println '(3)'
}

publish.dependsOn publishInit
publish.dependsOn aoeu
aoeu.mustRunAfter publishInit

它会返回

C:\>gradle publish
:publishInit
(1)
:aoeu
(2)
:publish
(3)

答案 1 :(得分:4)

task snth << {
    println "************************* before"
}

task aoeu << {
    println "**************************** during aoeu"
}
publish.dependsOn(aoeu)

task ueoa << {
    println "**************************** during ueoa"
}
publish.dependsOn(ueoa)

publish.doLast {
    println "************************* after"
}

publish.dependsOn.each { dependency ->
    if (dependency instanceof Task) {
        dependency.dependsOn(snth)
    }
}

将输出:

:snth
************************* before
:aoeu
**************************** during aoeu
:ueoa
**************************** during ueoa
:publish
************************* after

如果您希望以递归方式添加依赖项:

def recursivelyAddDependsOn(Task parent, Task dependsOn) {
    if (!parent.equals(dependsOn)) {
        parent.dependsOn(dependsOn)

        def tasks = parent.dependsOn.findAll { dependency ->
            dependency instanceof Task
        }
        tasks.each { task ->
            recursivelyAddDependsOn(task, dependsOn)
        }
    }
}

recursivelyAddDependsOn(publish, snth)

更实用的解决方案是:

def recursivelyApplyToTaskDependencies(Task parent, Closure closure) {
    closure(parent)

    parent.dependsOn.findAll { dependency ->
        dependency instanceof Task
    }.each { task ->
        recursivelyApplyToTaskDependencies(task, closure)
    }
}

def recursivelyAddTaskDependency(Task parent, Task dependency) {
    def addTaskDependency = { p ->
        if (!p.name.equals(dependency.name)) {
            p.dependsOn(dependency)
        }
    }

    recursivelyApplyToTaskDependencies(parent, addTaskDependency)
}

recursivelyAddTaskDependency(publish, snth)

答案 2 :(得分:0)

订购任务令人头疼,<<是不推荐使用的语法。

我最终编织了如下任务。

task publish {
    println "here we go"
}

task task1 {
    println "first"
}

task task2 {
    println "second"
}

task task3 {
    println "third"
}

// task execution order. this executes bottom to top order.
publish.dependsOn task3
task3.dependsOn task2
task2.dependsOn task1

publish任务结果如下;

here we go
first
second
third