task創(chuàng)建
//直接通過tadk函數(shù)去創(chuàng)建
task helloTask {
println 'hello task!!!'
}
task helloTask1(group: "jimmy", description: 'task study') {
println 'hello task!!!'
}
task (helloTask2, {
println 'hello task222!!!'
})
//通過TaskCopntainer去創(chuàng)建Task
this.tasks.create(name: "helloTask3") {
setGroup('jimmy')
setDescription('task study333')
println 'hello task333!!!'
}
doFirst
task helloTask1(group: "jimmy", description: 'task study') {
println 'hello task!!!'
doFirst {
println 'doFirst 001, group: ' + group
}
}
//下面的外部doFirst 先于上面的doFirst執(zhí)行
helloTask1.doFirst {
println 'doFirst 002, description: ' + description
}
計算build執(zhí)行時長
def startBuildTime, endBuildTime
this.afterEvaluate { Project project ->
//保證要找的task已經(jīng)配置完畢
def preBuildTask = project.tasks.getByName('build') //preBuild 是錯誤的
preBuildTask.doFirst {
startBuildTime = System.currentTimeMillis()
println 'Bulid 開始時間: ' + startBuildTime
}
def buildTask = project.tasks.getByName('build')
buildTask.doLast {
endBuildTime = System.currentTimeMillis()
println "Bulid 結(jié)束時間: ${endBuildTime - startBuildTime}"
}
}

image.png
task依賴
task taskX {
doLast {println 'taskX'}
}
task taskY {
doLast {println 'taskY'}
}
task taskZ (dependsOn: [taskX, taskY]) {
//dependsOn this.tasks.findAll {task -> task.name.startsWith("lib")}
doLast {println 'taskZ'}
}
println "-----------------"
// << 相當(dāng)于 doLast
task lib1 << {
println 'lib1'
}
task lib2 << {
println 'lib2'
}
task noLib << {
println 'noLib'
}