创建Task

2017-08-30  本文已影响890人  ProZoom

Task

task helloword << {
   println 'hello Gradle!'
}

这里的“<<”表示追加的意思,即向hello中加入执行过程。我们还可以使用doLast来达到同样的效果:

task helloword2 {
  doLast { println 'hello grade 2 !'}
}

另外,如果需要向Task的最前面加入执行过程,我们可以使用doFirst:

task helloword3 {
  doFirst { println 'hello grade 3 !'}
}

以上我们自定义的3个Task都位于TaskContainer中,Project中的tasks属性即表示该TaskContainer。为此,我们可以新建一个Task来显示这些信息:

task showTasks {
   println tasks.class
   println tasks.size()
}

输出结果:


> Configure project :
The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
        at build_dm49rdwdh0ayv2low2to9ie29.run(/Users/liyang/gradle_demo/build.gradle:33)
class org.gradle.api.internal.tasks.DefaultTaskContainer_Decorated
4


BUILD SUCCESSFUL in 0s

tasks.create(name: 'hello4]word4') << {
   println 'hello grade 4 !'
}

输出结果:


> Configure project :
The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
        at build_dm49rdwdh0ayv2low2to9ie29.run(/Users/liyang/gradle_demo/build.gradle:33)
class org.gradle.api.internal.tasks.DefaultTaskContainer_Decorated
4
hello Gradle 4 !


BUILD SUCCESSFUL in 0s

Task之间是可以存在依赖关系,比如TaskA依赖TaskB,那么在执行TaskA时,Gradle会先执行TaskB,再执行TaskA。我们可以在定义一个Task的同时声明它的依赖关系:

task helloword5(dependsOn: helloword2) << {
    println 'hello Gradle 5 !'
}

输出结果:


> Configure project :
The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
        at build_dm49rdwdh0ayv2low2to9ie29.run(/Users/liyang/gradle_demo/build.gradle:33)
hello Gradle 4 !
class org.gradle.api.internal.tasks.DefaultTaskContainer_Decorated
7

> Task :helloword2
hellow gradle 2 !

> Task :helloword5
hello Gradle 5 !


BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

当然,我们也可以在定义Task之后再声明依赖:

task helloword6 << {
    println 'hello Gradle 6 !'
}
hellword6.dependsOn helloword

输出结果


> Configure project :
The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
        at build_dm49rdwdh0ayv2low2to9ie29.run(/Users/liyang/gradle_demo/build.gradle:33)
hello Gradle 4 !
class org.gradle.api.internal.tasks.DefaultTaskContainer_Decorated
7

> Task :helloword
hello gradle!

> Task :helloword6
hello Gradle 6 !


BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

一个Task除了执行操作之外,还可以包含多个Property,其中有Gradle为每个Task默认定义的Property,比如description,logger等。另外,每一个特定的Task类型还可以含有特定的Property,比如Copy的from和to等。当然,我们还可以动态地向Task中加入额外的Property。在执行一个Task之前,我们通常都需要先设定Property的值,Gradle提供了多种方法设置Task的Property值。

首先,我们可以在定义Task的时候对Property进行配置:

task helloword7 << {
    description = "this is helloword 7"
    println description
}

输出结果:

The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
        at build_dm49rdwdh0ayv2low2to9ie29.run(/Users/liyang/gradle_demo/build.gradle:33)
class org.gradle.api.internal.tasks.DefaultTaskContainer_Decorated
4
hello Gradle 4 !

> Task :helloword7
this is helloword 7


BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

我们还可以通过调用Task的configure()方法完成Property的设置:

task helloword8 << {
println description
}

helloword8.configure {
description = "this is helloword8"
}

输出结果:

> Configure project :
The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
        at build_dm49rdwdh0ayv2low2to9ie29.run(/Users/liyang/gradle_demo/build.gradle:33)
class org.gradle.api.internal.tasks.DefaultTaskContainer_Decorated
4
hello Gradle 4 !

> Task :helloword8
this is helloword8

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
上一篇下一篇

猜你喜欢

热点阅读