The introduction

In the last stage, we know that project is the entrance to Gradle. We also have a detailed understanding of the project series, such as: Project API family overview/project API/ extended properties, etc. For more details, please refer to the following chapters:

Gradle Project Details (1)

Gradle Project details (2)

Gradle Project Details (3)

Next, we will start gradle’s next important module, Task;

define

(1) The direct task function creation method, which defines the task mode in the project, will be added to the tasks in the project. Tasks is a TaskContainer class, which is mainly used to manage all tasks in the project.

Code:

task daviTask1 {
    println '= = = = = = = = = = = = start defining = = = = = = = = = = = = = = = ='
    println 'I am daviTask1'' I am daviTask1'
    println '= = = = = = = = = = = = end defined = = = = = = = = = = = = = = = ='

}
Copy the code

Execution Result:

(2) Create a project using TaskContainer

this.tasks.create(name: 'daviTask2') {
    println '= = = = = = = = = = = = start defining the = = = = = = = = = = = = = = = = = = = = = = ='
    println '[task definition] [TaskContainer] I am daviTask2'
    println '= = = = = = = = = = = = end defined = = = = = = = = = = = = = = = = = = = = = = = = ='
}
Copy the code

configuration

(1) Configure when defining

Code:

task daviTask3(group: 'davi'.description: 'Davi configuration Description') {
    println '= = = = = = = = = = = = start defining = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ='
    println '[task configuration] [Key-valus mode] I am daviTask3'
    println '= = = = = = = = = = = = end defined = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ='

}
Copy the code

Execution Result:

(2) Set method configuration

Code:

this.tasks.create(name: 'daviTask4') {
    println '= = = = = = = = = = = = start defining the = = = = = = = = = = = = = = = = = = = = = = ='

    setGroup('davi2')
    setDescription('Davi2 Configuration Description')

    println '[task] [set] I am daviTask4'
    println '= = = = = = = = = = = = end defined = = = = = = = = = = = = = = = = = = = = = = = = ='
}
Copy the code

Execution Result:

In addition to the group and description attributes in the example, you can also configure the following attributes (depending on your own situation) :

perform

Code:

task daviTask5(group: 'davi'.description: 'Davi configuration Description') {
    doFirst {
        println '= = = = = = = = = = = = start task in execution phase = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ='
        println '[daviTask5] [Task in execution phase] [closure...] i am daviTask5'
        println '= = = = = = = = = = = = end task in execution phase = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ='
    }
}
daviTask5.doFirst {
    println '= = = = = = = = = = = = start task in execution phase = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ='
    println '[daviTask5] [Task in execution phase] I am daviTask5, prior to execution within closure '
    println '= = = = = = = = = = = = end task in execution phase = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ='
}
Copy the code

Execution Result:

As you can see, externally specified doFirst is executed before internally specified doFirst

Case study

Collect statistics about the packaging duration in the Execution phase by task


// After the timing configuration, ensure that the task topology is complete
this.afterEvaluate { Project project ->
    println '= = = = = = = = = = = = start statistical package execution phase 】 【 time = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ='

    def startTime, endTime

    // The first task in the execution phase is 'preBuild'
    def firstTask = project.tasks.getByName('preBuild')
    firstTask.doFirst {
        // Record the time before the first task is executed
        startTime = System.currentTimeMillis()
    }

    // The last task in the execution phase is 'build'
    def lastTask = project.tasks.getByName('build')
    lastTask.doLast {
        // Record the time since the last task was executed
        endTime = System.currentTimeMillis()
        / / the result
        println ${endtime-startTime} ${endtime-startTime}

        println '= = = = = = = = = = = = start statistical package execution phase 】 【 time = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ='}}Copy the code

dependsOn

Code:

task daviTask111(group: 'davi') {
    doLast {
        println "DaviTask111 doLast executed."}}task daviTask222(group: 'davi') {
    doLast {
        println "DaviTask222 doLast executed."}}/ / dependsOn dependency
task daviTask333(group: 'davi', dependsOn: [daviTask111, daviTask222]) {
    doLast {
        println "DaviTask333 doLast executed."}}Copy the code

Execution Result:

Input and output of task

Implementation using the input and output of task, the implementation of the string into a file;

def taskApiFile
task taskWriteApiFile(group: 'davi') { Task t ->
    / / define
    def vName = '1.0.0111'
    taskApiFile = file('taskApiFile.xml')
    if(! taskApiFile.exists()) {println "[input/Output method] [taskWriteApiFile] file does not exist, create.."
        taskApiFile.createNewFile()
    } else {
        println "[input/Output mode] [taskWriteApiFile] File exists.."
    }

    // Specify input
    t.getInputs().property('vName', vName)
    // Specify the output
    t.getOutputs().file(taskApiFile)

    // Output output execution use
    t.doLast {
        println "[input/Output method] [taskWriteApiFile] Save the input to the output file.."

        // Get input/output
        Map<String, Object> data = t.getInputs().getProperties()
        File f = t.getOutputs().getFiles().singleFile

        // The input is saved to the output file
        StringWriter sw = new StringWriter()
        sw.write(data.get('vName'))
        f.withWriter { BufferedWriter writer ->
            writer.append(sw.toString())
        }
    }
}
Copy the code

MustRunAfter mode

Code:

task task666(group: 'davi') { Task t ->
    t.doLast {
        println "task666"}}task task777(group: 'davi') { Task t ->
    t.mustRunAfter(task666)

    t.doLast {
        println "task777"}}task task888(group: 'davi') { Task t ->
    t.mustRunAfter(task777)

    t.doLast {
        println "task888"}}task taskTest667788(group: 'davi') { Task t ->
    t.dependsOn(task777, task888, task666)
}
Copy the code

Execution Result:

Custom tasks that are attached to the Gradle build process

Code:

task taskBeforePreBuild(group: 'davi') { Task t ->
    println "Configuration phase: [taskAfterPreBuild]"
    doLast {
        println "Execution stage: [taskAfterPreBuild]"}}this.project.afterEvaluate { Project p ->
    println "After the configuration phase is complete..."

    def preBuild = p.tasks.getByName('preBuild')
    preBuild.dependsOn(taskBeforePreBuild)
}
Copy the code

Execution Result:

At the end

Haha, that’s all for this article (systematic learning and growing together)