创建Task
2017-08-30 本文已影响890人
ProZoom
Task
- 调用Project的task()方法创建Task
在使用Gradle时,创建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
- 通过TaskContainer的create()方法创建Task
在上文中我们讲到,通过task()方法创建的Task都被存放在了TaskContainer中,而Project又维护了一个TaskContainer类型的属性tasks,那么我们完全可以直接向TaskContainer里面添加Task。查查TaskContainer的API文档可以发现,TaskContainer向我们提供了大量重载的create()方法用于添加Task。
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之间的依赖关系
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
一个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