據我所知,你的代碼應該可以正常工作,但它不會。應在0(執行階段)之前運行的配置階段執行
doFirst
。 BTW這個代碼工作正常:
彼得Niederwiser寫到這裏https://stackoverflow.com/a/9204159/2069368doFirst
運行在執行階段的第一條語句(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)
[更新]
這裏http://gradle.1045684.n5.nabble.com/task-run-order-lt-lt-syntax-doLast-doFirst-etc-td3337481.html是很好的例子很好的問題,顯示執行順序。
閱讀this article關於gradle生命週期,它會幫助你理解它。
[更新] 如果你想在publish
執行階段運行任務aoeu
可以調用publish.doFirst
aoeu.execute
。但據我所知,不應該這樣做。
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)
當我看到你想在publish
任務的途中跑aoeu
。我認爲最好的辦法做到這一點將是將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)
既然「'''」運算符已被棄用,我們需要使用doFirst和doLast。 – will