task hello { doLast{ println "Hello World!" } }
运行gradle -q hello 得到如下结果(-q选型时关闭gradle日志信息,直接使用gradle hello也可以得到相同的结果,但会打印出一些Gradle的日志)
1
2
3 |
task hello << { println
‘Hello World‘ } |
1
2
3
4
5
6 |
task hello << { println
‘Hello World‘ } task intro(dependsOn: hello) << { println
"I‘m Gradle" } |
1
2
3
4
5
6 |
task taskX(dependsOn: ‘taskY‘ ) << { println
"taskX" } task taskY << { println
"taskY" } |
动态Task
1
2
3
4
5
6 |
4 . times
{ counter -> task "task$counter"
<<{ println
"I‘m task number $counter" } } |
1
2
3
4
5
6
7 |
4 . times
{ counter -> task "task$counter"
<<{ println
"I‘m task number $counter" } } task0.dependsOn task2, task3 |
1
2
3
4
5
6
7
8
9
10
11
12 |
task hello << { println
‘Hello Earth‘ } hello.doFirst { println
"Hello Venus" } hello.doLast { println
"Hello Mars" } hello << { println
"Hello Jupiter" } |
doFirst和doLast可以被定义并执行多次(定义同样的操作类型则按照定义的顺序执行) hello << 等价于hello.doLast
额外的Task配置
1
2
3
4
5
6 |
task myTask { ext.myProperty = "myValue" } task printTaskProperties <<{ println
myTask.myProperty } |
定义默认任务
1
2
3
4
5
6
7 |
defaultTasks ‘clean‘ , ‘run‘ task clean << { println
"Default Cleaning!" } task run << { println
"Default Running!" } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
task distribution << { println
"We build the zip with version=$version" } task release(dependsOn: ‘distribution‘ ) <<{ println
"We release now" } gradle.taskGraph.whenReady { taskGraph -> if (taskGraph.hasTask(release)) { version = "1.0" } else
{ version = "1.0-SNAPSHOT" } } |
当release task被执行的时候version = "1.0" 没被执行的时候 version = "1.0-SNAPSHOT"
原文地址:http://www.cnblogs.com/getto/p/3759905.html