gradle学习(1)-helloworld
最后更新于:2022-04-01 06:51:20
## 1\. 创建build.gradle文件
~~~
qianhuis-Mac-mini:1225 qianhui$ touch build.gradle
~~~
## 2.打开build.gradle
~~~
vi build.gradle
~~~
## 3.add a task
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466433296.jpg)
定义了一个任务hello,在该任务执行完后打印Hello world!
## 4\. run task
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466448aee.jpg)
## 5.如果你想看到执行的具体信息,可以将-q省略掉
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e46645a898.jpg)
## 6.另一种定义任务的方式
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e46646cec6.jpg)
## 7.使用groovy语言构建任务
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e46648c59c.jpg)
执行任务
~~~
qianhuis-Mac-mini:1225 qianhui$ gradle -q upper
Original:mY_nAmE
Upper case:MY_NAME
qianhuis-Mac-mini:1225 qianhui$
~~~
## 8.调用groovy中函数
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e4664a0ac9.jpg)
执行
~~~
qianhuis-Mac-mini:1225 qianhui$ gradle -q count
0
1
2
3
~~~
## 9.任务依赖
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e4664bd039.jpg)
执行
~~~
qianhuis-Mac-mini:1225 qianhui$ gradle -q intro
Hello world!
I'm Gradle
~~~
## 10.添加依赖时,无需所依赖的任务存在,意思可以先添加依赖,然后再定义所依赖的任务
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e4664cfc49.jpg)
~~~
qianhuis-Mac-mini:1225 qianhui$ gradle -q taskX
taskY
taskX
~~~
## 11.动态任务
~~~
4.times {
counter->task "task$counter" << {
println "I'm task number $counter"
}
}
~~~
上面的任务中并没有创建具体的任务,而是在执行的过程中,动态的创建任务。
~~~
qianhuis-Mac-mini:1225 qianhui$ gradle -q task1
I'm task number 1
~~~
## 12.通过api调用依赖任务
~~~
4.times {
counter->task "task$counter" << {
println "I'm task number $counter"
}
}
task0.dependsOn task1,task2
~~~
~~~
qianhuis-Mac-mini:1225 qianhui$ gradle -q task0
I'm task number 1
I'm task number 2
I'm task number 0
~~~
## 13.通过behavior方式代替依赖
~~~
task hello << {
println 'Hello world!'
}
hello.doFirst{
println 'Hello Venus'
}
hello.doLast{
println 'Hello Mars'
}
hello << {
println 'Hello Jupiter'
}
~~~
上面的任务的方法doFirst和doLast分别代表在任务之前调用、任务之后调用。