执行多个任务
安装好Gradle之后,就可以使用Gradle命令行了。首先我们先新建一个文件夹gradle-learn
,然后在其中新建一个build.gradle
,这是Gradle的项目构建文件。
task compile {
doLast {
println 'compiling source'
}
}
task compileTest(dependsOn: compile) {
doLast {
println 'compiling unit tests'
}
}
task test(dependsOn: [compile, compileTest]) {
doLast {
println 'running unit tests'
}
}
task dist(dependsOn: [compile, test]) {
doLast {
println 'building the distribution'
}
}
Gradle执行的基本单元是任务,在上面我们定义了4个任务。这4个任务之间还存在依赖关系,使用dependsOn定义。然后我们使用Gradle运行dist和test任务,会发现Gradle会分析这几个任务之间的依赖关系,依次运行它们,gradle dist test
。
结果如下。
$ gradle dist test
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution
BUILD SUCCESSFUL
Total time: 2.22 secs
排除任务
可以用-x
参数排除某个任务。现在我们运行dist而不运行test:gradle dist -x test
。然后查看一下输出,我们会发现输出非常有