Gradle serialization 2 - create tasks in multiple ways

1, Closure entrustment

  • Closures have three attributes: thisObject, owner and delegate. When calling methods in closures, they determine which object to use for processing. By default, delegate and owner are equal. At that time, delegate can be modified. This function is very powerful. Many functions of closures in Gradle are realized by modifying delegate.
    task helloDelegate {
      new Delegate().test {
          println "thisObject:${thisObject.getClass()}"
          println "owner:${owner.getClass()}"
          println "delegate:${delegate.getClass()}"
          println "================"
          method1()
          println "================"
          it.method1()
      }
    }
    

def method1() { println "Context this:${this.getClass() } int root" println "method1 in Delegate" }

class Delegate { def method1() { println "Delegate this:${this.getClass() } int Delegate" println "method1 in Delegate" }

def test(Closure<Delegate> closure) {
    closure(this)
}

}

![Insert picture description here](https://img-blog.csdnimg.cn/c7521e5f136846f7b8ee26f80b6ffc49.png)


```groovy
task configClosure {
    person {
        personName = "Zhang San"
        personAge = 20
        dumpPerson()
    }
}

class Person {
    String personName
    int personAge

    def dumpPerson() {
        println "name is ${personName}, age is ${personAge}"
    }
}

def person(Closure<Person> closure) {
    Person p = new Person();
    closure.delegate = p
    // Delegation mode is preferred
    closure.setResolveStrategy(Closure.DELEGATE_FIRST);
    closure(p)
}

2, task,doFirst,doLast

task customTask1 {
    doFirst {
        println 'customTask1:doFirst'
    }
    doLast {
        println 'customTask1:doLast'
    }
}

tasks.create("customTask2") {
    doFirst {
        println 'customTask2:doFirst'
    }
    doLast {
        println 'customTask2:doLast'
    }
}
  • Both methods are acceptable. task is a function of a Project object. The prototype is create (string name, closure, configureclosure)
  • customTask1 is the name of the task, which can be customized; The second parameter is a closure, that is, the code block in curly braces. According to Groovy, when the last parameter is a closure, it can also be placed outside the parentheses, and then the parentheses of the method can be omitted to generate our above writing method, which is very concise. The function of this closure is to configure the task we create. In the example, we use the doFirst and doLast methods of the task to output a text before and after the task is executed. In addition, there are other methods and properties.

    3, Task dependency

    task ex35Hello {
      println 'hello'
    }
    

task ex35Main(dependsOn: ex35Hello) { doLast { println 'main' } }

![1.3](https://img-blog.csdnimg.cn/330b3f658d914c6a888a34388693d95c.png)
- It can be seen from the above that the task ex35Main It depends on the first task, which we write in here dependsOn Keyword to add a pre task. Let's take a look and add multiple tasks
```groovy
task ex35Hello {
    println 'hello'
}

task ex35Main(dependsOn: ex35Hello) {
    doLast {
        println 'main'
    }
}

task ex35World {
    println 'world'
}

task ex35MultiTask {
    dependsOn ex35Hello, ex35World
    doLast {
        println 'multiTask'
    }
}

4, Task keys control and interact through API

task ex36Hello {
    println 'dowLast1'
}

ex36Hello.doFirst {
    println 'dowFirst'
}

ex35Hello.doLast {
    println project.hasProperty('ex36Hello') // It indicates that each task is an attribute of Project
    println 'dowLast2'
}
  • In Project, every task is an attribute, as we can see from the judgment results returned above.

    5, Custom properties

    // Customize the properties of a Project
    ext.age = 18
    

//Customize multiple attributes at the same time through code block ext {phone = 1334512 address = ''}

sourceSets.all { ext.resourcesDir = null }

sourceSets { main { resourcesDir = "main/res" }

test {
    resourcesDir = "test/res"
}

}

Task ex37customproperty {println "age: ${age}" println "phone: ${phone}" println "address: ${address}"

sourceSets.each {
    println "${it.name}of resourcesDir Yes: ${it.resourcesDir}"
}

}

![1.6](https://img-blog.csdnimg.cn/1efce6f7331249ef8244c42aa8f473c7.png)
- The customized attributes are effective. It is generally used in the project to define the version number and version name. The version number and version name are placed separately Gradle Because they change and change frequently every time they release the version, they are placed in a separate file Gradle In the document, it is easy to manage, and it will not be changed because Git Conflict affects the whole Build Documents to facilitate conflict resolution.
## 6, Script code
```groovy
def buildTime() {
    def date = new Date()
    def formattedDate = date.format("yyyyMMdd")
    return formattedDate
}

This article is composed of blog one article multi posting platform OpenWrite release!

Tags: Java

Posted by raymedia on Sat, 16 Apr 2022 01:10:58 +0930