task

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'
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容