Gradle Task的创建和使用
文章目录
创建栈
1tasks.register("Hello") {
2 doLast {
3 println("Run the method do last")
4 }
5}
使用 gradle -q Hello会得到输出
任务的生命周期
1
2tasks.register("World") {
3 println("Config the world task")
4 dependsOn("Hello")
5 doFirst {
6 println("World first")
7 }
8 doLast {
9 println("World do last")
10 }
11}
12
13tasks.register("Hello") {
14 println("Config the hello task")
15 doLast {
16 println("Hello do last")
17 }
18 doFirst {
19 println("Hello do first")
20 }
21}
22
23tasks.register("Other") {
24 println("Config the other task")
25}
register来注册task,然后通过doLast和doFirst来确定顺序,通过dependsOn来指定是否要依赖其他的Task。有一个叫Other的任务因为没有被依赖,所以干脆就没有被调用,而本身的闭包内的内容是直接调用的,并没有遵循 Task 的依赖顺序,所以闭包内不能做其他任务做过的事。如果需要依赖其他的内容,还是需要在 doLast/doFirst里来做。
除了任务本身的Task外,项目也可以做全局的task创建的监听,具体的参考
官网文档
。