各種Task操作:
//task依賴
task hello {
println 'Hello world!'
}
task fine(dependsOn: hello) {
println 'I am fine'
}
//打印 Hello world! I am fine
//任務(wù)執(zhí)行開始、結(jié)束的代碼執(zhí)行
task firstLast {
doLast {
println "doLast"
}
doFirst {
println "doFirst"
}
}
//自定義任務(wù)屬性,用ext聲明
task myTask {
ext.myProperty = "myValue"
}
task printTaskProperty {
println(myTask.myProperty)
}
//打印 myValue
//定位 tasks
task location {
//通過屬性獲取task
println(project.hello.name)
//通過tasks 集合 獲取 task
println(tasks['hello'].name)
}
//打印 hello hello
//配置一個任務(wù) - 通過閉包 closure
task myCopy(type: Copy)
myCopy {
println("myCopy task")
}
//打印 myCopy task
// "must run after" 和 "should run after".
task taskX {
println 'taskX'
}
task taskY {
println 'taskY'
}
taskX.mustRunAfter taskY
taskX.shouldRunAfter taskY
//打印 taskX taskY
//給任務(wù)加入描述
task description {
description '這是一個task的description'
println description
}
//打印 這是一個task的description
//拋出一個RuntimeException異常
task stopException {
// throw new RuntimeException()
}
//激活和注銷 tasks
task disableMe {
println 'disableMe'
}
disableMe.enabled = false
//通過 gradle.properties 文件設(shè)置屬性
task printProperties {
println gradlePropertiesProp //gradle屬性
println System.properties['system'] //system屬性
}
//打印 gradlePropertiesValue systemValue
//采用 Project.task(String name) 方法來創(chuàng)建
project.task("myTask3").doLast {
println "doLast in task3"
}
//task類型: 包括 Copy Delete
task copyFile(type: Copy) {
from 'xml'
into 'destination'
}
//自定義Task
//使用自定義task,并通過constructorArgs參數(shù)來指定構(gòu)造函數(shù)的參數(shù)值
task hello1(type: SayHelloTask, constructorArgs:[30]) {
destDir = file("$buildDir/outputFile")
sayHello() //在task中調(diào)用該task的方法
}
task hello2(type: SayHelloTask, constructorArgs:[18]) {
msg = "libo" //在task中設(shè)置該task的屬性
sayHello()
}
//Hello,defualt name, My age is 30
//打印 Hello,libo, My age is 18
//配置TaskInputs、TaskOutputs 增量構(gòu)建
//第2次運行時,test1 task 并沒有運行,而是被標(biāo)記為 up-to-date,而 test2 task 則每次都會運行,這就是典型的增量構(gòu)建。
task test1 {
//設(shè)置inputs
inputs.property("name", "hjy")
inputs.property("age", 20)
//設(shè)置outputs
outputs.file("$buildDir/test.txt")
doLast {
println "exec task task1"
}
}
task test2 {
doLast {
println "exec task task2"
}
}
參考:
https://juejin.cn/post/6844903838290296846#comment
https://doc.yonyoucloud.com/doc/wiki/project/GradleUserGuide-Wiki/index.html