Gradle Task入门演示一
安装配置省略
在idea中新建gradle工程名字:Gradle-Showcase
➜ Gradle-Showcase gradle -v ------------------------------------------------------------ Gradle 2.6 ------------------------------------------------------------ Build time: 2015-08-10 13:15:06 UTC Build number: none Revision: 233bbf8e47c82f72cb898b3e0a96b85d0aad166e Groovy: 2.3.10 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.8.0_51 (Oracle Corporation 25.51-b03) OS: Mac OS X 10.10.5 x86_64 ➜ Gradle-Showcase type gradle gradle is /Users/xinxingegeya/IDE/gradle-2.6/bin/gradle
安装成功。
在build.gradle文件中编写第一个task,如下,
task helloworld() { doLast { println 'hello world' } }
运行task,
➜ Gradle-Showcase gradle -q helloworld hello world
task有多种写法,如下是简写的一种方式,使用<<定义task的body,
task helloworldByShortcut << { println 'hello world' }
还有如下的task,
task upper << { String someString = 'mY_nAmE' println "Original: " + someString println "Upper case: " + someString.toUpperCase() } task count << { 4.times { print "$it " } }
这些task定义中用到了groovy的代码和方法调用,当然也用到了groovy的闭包。在学习gradle前,应该了解一下groovy闭包的用法,groovy的闭包和其他语言的闭包相差不大。
gradle task可以之间可以相互依赖。同时在定义task时,可以声明task依赖的task,如下,
task hello << { println 'Hello world!' } task intro(dependsOn: hello) << { println "I'm Gradle" }
task intro依赖于hello 这个task。当运行intro时,hello task也会执行。如下,
➜ Gradle-Showcase gradle -q intro Hello world! I'm Gradle
当hello这个task不存在时,执行intro task时会报错,如下,
* What went wrong: A problem occurred evaluating root project 'Gradle-Showcase'. > Could not find property 'hello' on root project 'Gradle-Showcase'.
有什么办法可以解决被依赖的task不存在时报错呢?可以像下面这样声明和定义task,
task taskX(dependsOn: 'taskY') << { println 'taskX' } task taskY << { println 'taskY' }
运行task,
➜ Gradle-Showcase gradle -q taskX taskY taskX
这时taskY就是不存在也不会影响taskX。
定义动态的task,如下,
4.times { counter -> task "task$counter" << { println "I'm task number $counter" } }
执行task,
➜ Gradle-Showcase gradle -q task3 I'm task number 3 ➜ Gradle-Showcase gradle -q task1 I'm task number 1 ➜ Gradle-Showcase gradle -q task0 I'm task number 0 ➜ Gradle-Showcase gradle -q task4 FAILURE: Build failed with an exception. * What went wrong: Task 'task4' not found in root project 'Gradle-Showcase'. Some candidates are: 'task0', 'task1', 'task2', 'task3', 'taskX', 'taskY', 'tasks'.
可以看到运行task4 会报错。
我们可以使用dependsOn关键字声明和定义动态task的依赖,如下,
4.times { counter -> task "task$counter" << { println "I'm task number $counter" } } task0.dependsOn task1 ,task2
执行task0,如下,
➜ Gradle-Showcase gradle -q task0 I'm task number 1 I'm task number 2 I'm task number 0
当执行一个task时,用doFirst 和 doLast指定其前置动作和后置动作,
task oneTask << { println 'one' } oneTask.doFirst { println 'doFirst' } oneTask.doLast { println 'doLast' }
执行如下,
➜ Gradle-Showcase gradle -q oneTask doFirst one doLast
可以动态给task 增加额外的属性,如下所示,
task myTask { ext.myProperty = "myValue" } task printTaskProperties << { println myTask.myProperty }
执行task,
➜ Gradle-Showcase gradle -q printTaskProperties myValue
task loadfile << { def files = file('../.idea').listFiles().sort() files.each { File file -> if (file.isFile()) { ant.loadfile(srcFile: file, property: file.name) println " *** $file.name ***" println "${ant.properties[file.name]}" } } }
defaultTasks 'clean', 'run' task clean << { println 'Default Cleaning!' } task run << { println 'Default Running!' } task other << { println "I'm not a default task!" }
执行task,
➜ Gradle-Showcase gradle -q Default Cleaning! Default Running!
======END======