6 Use Gradle custom task task

The task is one of two basic concepts of Gradle build, but the task of defining and using a variety of forms, the following gave you the task of defining and using a few.

The basic syntax defined task

// 使用task 后带任务名称 加上执行闭包{}
task t1 {
  println 't1'
}
// 任务名称后加上圆括号
task t2() {
  println 't2'
}
// IDEA 刷新 Task 点击 build 或 左下 Terminal 输入 gradle build 即可看到结果
t1
t2

These are the basic definition of the task, the result will be executed when configuring the project phase, in addition to task dependencies, any reference to the need to configure the project object operation will be performed. If you need to perform an action only when the code needs to be defined in doFirst task calls or doLast method.

Common tasks defined way

task t1{
    // 任务调用前执行动作
   doFirst{
       println 't1'
   }
}
// 任务调用后执行动作
task t2(){
    doLast{
       println 't2'
    }
}
// << 是 doLast 的简写,任务调用后执行动作
task t3 << {
    println 't3'
}
// 可给任务名带 ' '

Test: gradle t1 t2 t3

> Task :t1
t1

> Task :t2
t2

> Task :t3
t3

The above is the most common way to define the tasks, and does not call doFirst doLast method of configuring project. It will only be executed when called directly dependent on the task or other tasks. That in the end how dependent here that depend on it? The following configuration is dependent look at the task.

Dependent tasks of configuration dependsOn

Dependent tasks require the use of a keyword dependsOn do depend on the configuration, the configuration takes many forms, demonstrate the following come to you under common usage:

task t1 {
    doFirst {
        println 't1'
    }
}
// 定义任务时指明依赖
task t2(dependsOn : t1) {
    doLast {
        println 't2'
    }
}
task t3 << {
    println 't3'
}

task t4 {
    dependsOn t3          // 任务内写依赖
    doLast{
        println 't4'
    }
}
// 外部添加依赖 t1 依赖 t3
t1.dependsOn t4

// 外部增加任务动作达到依赖
t3.doFirst{
    println '给 t3 增加增加动作1'
}
t3 << {
    println '给 t3 增加增加动作2'
}

Mission T2 gradle t2, the following results:

> Task :t3
给 t3 增加增加动作1
t3
给 t3 增加增加动作2
> Task :t4
t4
> Task :t1
t1
> Task :t2
t2

These are task dependent on several common ways of writing, there is no task that what syntax features high point of it?
In fact, some, with Groovy's powerful simple tasks can be defined not only can do more things. For example, you can dynamically define the task.

Dynamic Task

4.times {
    val ->
    task "tk${val}" << {
        println "The task is task${val}"
    }
}

Above to dynamically define the four tasks tk0, tk1, tk2, tk3, that in addition to high-level dynamic task What does it fast hardware? In fact, you can also customize the property to the task.

Custom attributes to the task

Here the use of ext to add additional attributes to the task, with ext. Attribute name = value from the definition.

// 给任务添加属性
task tk5{
    ext.myProperty = "The property value"
}
// 获取自定义属性
task printTaskProperty << {
    println tk5.myProperty
}
// ============== 结果 ===================
> Task : printTaskProperty
The property value

There are wood?
In fact, the task here only describes some common operations only, want to learn more in-depth knowledge, you can refer to the official documentation.

Next, use an example to consolidate the next task.

Examples
demand: custom task to automatically generate project directory.

// 定义创建目录的普通方法
def createDir(fileName){
    File f = new File(fileName);
    f.mkdirs()
}

// 定义生成Java 项目目录的任务
task createJavaProjectDir << {
    def dirList = ['src/main/java','src/main/resources','src/test/java','src/test/resources']
    dirList.each {fileName  ->  createDir(fileName)}
}

// 定义生成 Web 项目目录的任务
task createWebProjectDir (dependsOn: createJavaProjectDir) << {
    def fileName = 'src/main/webapp'
    createDir(fileName)
}

In fact, Java components have been previously defined, we only need to directly call on the line.

task createDirs << {
    sourceSets*.java.srcDirs*.each{
        it.mkdirs()
    }
    sourceSets*.resources.srcDirs*.each{
        it.mkdirs()
    }
}

These are the practices introduced in mission and tasks of Gradle.

Reprinted link: https://www.jianshu.com/p/e0a0c40f3e14

Guess you like

Origin blog.csdn.net/weixin_33719619/article/details/91014756