Task definition and configuration

Task definition

// Create task helloTask{println; // Create task helloTask{println'this is helloTask'} // In the second method, use TaskContainer to create a task this.tasks.create(name:'helloTask2'){
    println 'this is helloTask2'
}
Copy the code

Task allocation

// Task helloTask(group: group) {// Task helloTask(group: group) {// Task helloTask(group: group) {'imooc',description:'task learn'){
    println 'this is helloTask'
}

this.tasks.create(name:'helloTask2'){// Second configuration: configure it directly in the closuresetGroup('imooc')
    setDescription('task learn')
    println 'this is helloTask2'
}
Copy the code

Task execution and actual combat

The Task to perform

task helloTask(group:'imooc',description:'task learn'){// The println that is written directly into the closure is executed during the configuration phase'this is helloTask'

    doFirst {
        println 'task in do first'// When the task is run, it will be executed first}doLast {
        println 'task in do last'// When a task is executed, it will be executed last}}Copy the code

The Task of actual combat

Def startBuildTime def endBuildTime This.afterevaluate {Project Project -> // Find the first task to execute def preBuildTask = project.tasks.getByName('preBuild')
    preBuildTask.doFirst {
        startBuildTime = System.currentTimeMillis()
        println 'the start time is:'+ startBuildTime} def buildTask = project.tasks.getByName()'build')
    buildTask.doLast {
        endBuildTime = System.currentTimeMillis()
        println 'the build time is:' + (endBuildTime - startBuildTime)
    }
}

Copy the code

Task Execution order

Task dependent on

task taskx {
    doLast {
        println 'taskx'
    }
}
 
task tasky {
    doLast {
        println 'tasky'DependsOn: [taskx, tasky] {dependsOn: [taskx, tasky]}doLast {
        println 'taskz'DependsOn (taskx, tasky) dependsOn(taskx, tasky)Copy the code

Task Dynamic dependency

task taskz{
    doLast {
        println 'taskz2'
    }
}

taskz.dependsOn {
    tasks.findAll { task -> task.name.startsWith('lib') }
}

task lib1 {
    doLast {
        println 'lib1'
    }
}

task lib2 {
    doLast {
        println 'lib2'
    }
}

task nolib {
    doLast {
        println 'nolib'}}Copy the code

Task depends on actual combat

task handleReleaseFile {
    def srcFile = file('releases.xml')
    def destDir = new File(this.buildDir, 'generated/release/')
    doLast {
        println 'Start parsing the corresponding XML file... 'Destdir.mkdir () def releases = new XmlParser().parse(srcFile) releases.release.each {releaseNode -> // Releases the content of each release node def name = releaseNode.versionCode.text() def versionName = releaseNode.versionName.text() def versionInfo = ReleaseNode. VersionInfo. Text () / / create a File and write the node content def destFile = new File (destDir,"release-${name}.text")
            destFile.withWriter { writer ->
                writer.write("${name} -> ${versionName} -> ${versionInfo}")
            }
        }
    }
}
 
task handleReleaseFileTest(dependsOn: handleReleaseFile) {
    def dir = fileTree(this.buildDir.path + 'generated/release/')
    doLast {
        dir.each {
            println 'the file name is ' + it
        }
        println 'Output complete... '}}Copy the code

Task input and output

ext {
    versionName = '1.0.6'
    versionCode = '105'
    versionInfo = 'The second version of the App has introduced some of the most basic core features.'
    destFile = file('releases.xml')
    if(destFile ! = null && ! destFile.exists()) { destFile.createNewFile() } } task writeTask { inputs.property('versionCode', this.versionCode)
    inputs.property('versionName', this.versionName)
    inputs.property('versionInfo', this.versionInfo)
    outputs.file this.destFile
    doLast {// write the input to the output File def data = inputs.getProperties() File File = outputs.getFiles().getSingleFile() def versionMsg = new VersionMsg(data) def sw = new StringWriter() def xmlBuilder = new MarkupBuilder(sw)if(file.text ! = null && file.text.size() <= 0) {xmlBuilder. Releases {versionCode(versionMsg. VersionCode) VersionName (versionMsg. VersionName) versionInfo(versionMsg. VersionInfo)}} writer.append(sw.toString()) } }elseXmlbuilder. release {versionCode(versionMsg. VersionCode) versionName(versionMsg. VersionName) VersionInfo (versionMsg. VersionInfo)} def lines = file.readlines () def lengths = lines.size() -1 file.withWriter { writer -> lines.eachWithIndex { line, index ->if(index ! = lengths) { writer.append(line +'\r\n')}else if (index == lengths) {
                        writer.append('\r\r\n' + sw.toString() + '\r\n')
                        writer.append(lines.get(lengths))
                    }
                }
            }
        }
    }
}

class VersionMsg {
    String versionCode
    String versionName
    String versionInfo
}

task readTask {// Specify that the input file is the output of the previous Task: inputs. File this.destfiledoDef file = inputs.files.singleFile println file.text}} task taskZ {setGroup('imooc')
    dependsOn writeTask, readTask
    doLast {
        println 'End of I/O task'}}Copy the code

Mount a custom Task

task taskX {
    doLast {
        println 'taskx'
    }
}

task taskY {
    mustRunAfter taskX
    doLast {
        println 'taskY'
    }
}

task taskZ {
    mustRunAfter taskY
    doLast {
        println 'taskZ'}} Run C:\Users\zhongyu\Desktop\Test>gradlew taskY taskZ taskX Execution result > Task :app:taskX taskX > Task :app:taskY taskY > Task :app:taskZ taskZCopy the code

Articulated in actual combat

this.project.afterEvaluate { project ->
    def buildTask = project.tasks.getByName('build')
    if(buildTask == null) {
        throw GradleException('the build task is not found')
    }

    buildTask.finalizedBy writeTask
}
Copy the code

Reference:

  • Authoring Tasks