一、使用Gradle命令行安全
在这篇博客中,咱们将简要介绍Gradle命令行的使用。ide
1.1 执行多任务测试
经过在命令行列出每一个任务(task),你能够在一次构建(build)中执行多个任务。例如,命令gradle compile test会执行compile和test这两个任务,Gradle按照任务在命令行列出的顺序依次执行每一个任务,同时也会执行每一个任务的依赖任务。每一个任务无论如何被包含在build中,它只会被执行一次(无论它是被指定在命令行中,或者是做为另外一个task的依赖出现,或者二者兼有)。看一个例子,下图列出了4个任务,其中dist和test都依赖于compile。gradle
build.gradleui
1 task compile << { 2 println 'compiling source' 3 } 4 task compileTest(dependsOn: compile) << { 5 println 'compiling unit tests' 6 } 7 task test(dependsOn: [compile, compileTest]) << { 8 println 'running unit tests' 9 } 10 task dist(dependsOn: [compile, test]) << { 11 println 'building the distribution' 12 }
执行命令gradle dist test,输出结果以下spa
C:\Users\Administrator\Desktop\gradle>gradle dist test
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution
BUILD SUCCESSFUL
Total time: 2.012 secs
从上面输出结果能够看出,每一个任务只被执行了1次,所以gradle test test与gradle test效果彻底同样。命令行
1.2 移除任务code
你能够经过-x命令参数移除task,依然以上述代码为例,下面是测试结果输出blog
C:\Users\Administrator\Desktop\gradle>gradle dist -x test
:compile
compiling source
:dist
building the distribution
BUILD SUCCESSFUL
Total time: 1.825 secs
经过上述输出结果能够看出,任务test没有被执行,即使它被dist所依赖。同时你也会注意到任务test所依赖的任务之一compileTest也没有被执行,同时被test和dist所依赖的任务(如compile)仍然被执行了。博客
1.3 出错时的继续build
默认状况下,当gradle构建过程当中遇到任何task执行失败时将会马上中断。这样运行你立刻去解决这个构建错误,可是这样也会隐藏可能会发生的其余错误。为了在一次执行过程当中尽量暴露多的错误,你可使用--continue选项。当你执行命令的时候加上了--continue选项,Gradle将会执行每一个任务的全部依赖,而无论失败的发生,而不是在第一个错误发生时就立马中止。在执行过程当中所发生的全部错误在构建的结尾都会被列出来。若是一个任务失败了,随后其余依赖于这个任务的子任务,这么作是不安全的。例如,若是在test代码里面有一个编译错误,test任务将不会执行;因为test任务依赖于compilation任务(无论是直接仍是间接)。
1.4 简化task名字
在命令行指定task时,咱们不须要给出任务的全名,只须要给出可以足够惟一标识某个任务的简化的名字便可。例如,在上面的例子中,咱们可使用gradle di命令来执行task dist。此外,当task名是由多于1个的单词构成时,咱们也可使用每一个单词的首字母做为task名的简化形式。好比一个名字为compileTest的task,咱们可使用命令:gradle cT来进行编译(或者使用gradle compTest)。简化后的task名字也适用-x选项。
1.5 选择性的执行某个build文件
当咱们执行gradle命令时,会在当前路径下寻找build文件。可使用-b选项指定另外一个build文件。若是指定了-b选项,那么settings.gradle文件将不会起做用。看例子:
subdir/myproject.gradle:
task hello << { println "using build file '$buildFile.name' in '$buildFile.parentFile.name'." }
执行命令:gradle -q -b subdir/myproject.gradle hello输出结果以下:
using build file 'myproject.gradle' in 'subdir'.
或者,可使用-p选项指定要使用的工程目录。对多工程编译时,应该使用-p选项而不是-b选项。例子:
subdir/build.gradle
task hello << { println "using build file '$buildFile.name' in '$buildFile.parentFile.name'." }
执行命令:gradle -q -p subdir hello输出结果以下:
using build file 'build.gradle' in 'subdir'.
若是使用其它构建文件名,可与-b一块儿使用:gradle -q -p subdir -b subdir/myproject.gradle hello