14

我有以下内容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

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

4

3 回答 3

15

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

正如 Peter Niederwiser 在这里所写的那样https://stackoverflow.com/a/9204159/2069368 doFirst在执行阶段作为第一条语句(之前doLast)运行,因此您的代码可以正常工作。此示例将向您展示 gradle build 中的执行顺序:

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)

[更新]

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

阅读这篇关于 gradle 生命周期的文章,它将帮助您理解它。

[更新] 如果你想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任务分成两个单独的任务并使用depends,mustRunAfter来控制执行顺序。看这个例子:

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)
于 2013-09-11T08:14:20.317 回答
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)
于 2013-09-11T18:19:09.467 回答
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
于 2020-01-13T06:39:26.487 回答