2
我正在查看來自Mainak Mitra的Mastering Gradle(第70頁)的Gradle構建文件中自定義任務的簡單示例。構建腳本是:Gradle自定義任務操作順序
println "Working on custom task in build script"
class SampleTask extends DefaultTask {
String systemName = "DefaultMachineName"
String systemGroup = "DefaultSystemGroup"
@TaskAction
def action1() {
println "System Name is "+systemName+" and group is "+systemGroup
}
@TaskAction
def action2() {
println 'Adding multiple actions for refactoring'
}
}
task hello(type: SampleTask)
hello {
systemName='MyDevelopmentMachine'
systemGroup='Development'
}
hello.doFirst {println "Executing first statement "}
hello.doLast {println "Executing last statement "}
如果我運行gradle這個-q構建腳本:您好,輸出爲:
Executing first statement
System Name is MyDevelopmentMachine and group is Development
Adding multiple actions for refactoring
Executing last statement
正如預期的那樣與doFirst方法首先excecuting,兩個自定義操作執行按照它們的定義順序,然後執行doLast操作。如果我註釋掉加入doFirst和doLast行動線,輸出爲:
Adding multiple actions for refactoring
System Name is MyDevelopmentMachine and group is Development
現在的自定義操作都在定義它們相反的順序執行。我不知道爲什麼。
我不一定需要2個單獨的@TaskAction方法,我只注意到這個例子似乎並不遵守書中描述的規則。 –