据我所知,您的代码应该可以正常工作,但事实并非如此。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)