task创建
//直接通过tadk函数去创建
task helloTask {
println 'hello task!!!'
}
task helloTask1(group: "jimmy", description: 'task study') {
println 'hello task!!!'
}
task (helloTask2, {
println 'hello task222!!!'
})
//通过TaskCopntainer去创建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执行
helloTask1.doFirst {
println 'doFirst 002, description: ' + description
}
计算build执行时长
def startBuildTime, endBuildTime
this.afterEvaluate { Project project ->
//保证要找的task已经配置完毕
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 结束时间: ${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 "-----------------"
// << 相当于 doLast
task lib1 << {
println 'lib1'
}
task lib2 << {
println 'lib2'
}
task noLib << {
println 'noLib'
}
网友评论