Gradle之Task操作

各種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

demo地址:
https://github.com/running-libo/GradleStudy

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

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

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