gradle--第十一章 使用Gradle命令行


这一章讨论Gradle命令行的一些基础知识。正如在前面章节中看到的,你可以使用gradle命令行来运行一个构建脚本。
11.1 执行多个任务
你可以通过在命令行列出各个要执行的任务来在一个构建中执行多个任务。例如命令gradle compile test将会执行compile和test任务。gradle将会根据他们在命令行上显示的顺序依次执行各个命令,同时也会执行各个任务的依赖。每个任务只会执行一次,而不管它在构建中被包含了多少次:不管它是在命令行显示的描述了,还是其他任务的一个依赖,又或者两者都是。让我们来看下面一个例子。
下面这个图定义了4个任务.dist和test任务都依赖于compile这个任务。当我们为这个构建脚本运行gradle dist test时,comiple任务只会执行一次。
Figure 11.1. Task dependencies


Task dependencies
Example 11.1. Executing multiple tasks


build.gradle
task compile << {
    println 'compiling source'
}


task compileTest(dependsOn: compile) << {
    println 'compiling unit tests'
}


task test(dependsOn: [compile, compileTest]) << {
    println 'running unit tests'
}


task dist(dependsOn: [compile, test]) << {
    println 'building the distribution'
}
Output of 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: 1 secs


每个任务之后执行一次,所以上面这个例子执行gradle test test 跟执行gradle test是一样的。
11.2 排除任务
你可以通过使用命令行选项-x来排除你不需要执行的任务,提供你需要执行的任务,并在-x选项后面列出你排除不需要执行的任务。下面是一个例子,在上面那个例子的基础上 。
Example 11.2. Excluding tasks


Output of gradle dist -x test
> gradle dist -x test
:compile
compiling source
:dist
building the distribution


BUILD SUCCESSFUL


Total time: 1 secs
从这个任务的输出结果可以看出,test任务没有被执行,尽管他是dist的任务的一个依赖。通用你还可以看到test的任务的依赖如comipleTest任务也不会被执行。test任务的其他依赖如comiple同样也被其他任务依赖的。仍然会执行。
11.3 当失败发生时继续执行构建
默认情况下,当gradle构建过程中遇到任何task执行失败时将会立刻中断。这样运行你马上去解决这个构建错误,但是这样也会隐藏可能会发生的其他错误。为了在一次执行过程中尽可能暴露多的错误,你可以使用--continue选项。
当你执行命令的时候加上了--continue选项,Gradle将会执行每个任务的所有依赖,而不管失败的发生,而不是在第一个错误发生时就立马停止。在执行过程中所发生的所有错误在构建的结尾都会被列出来。
如果一个任务失败了,随后其他依赖于这个任务的子任务,这么做是不安全的。例如,如果在test代码里面有一个编译错误,test任务将不会执行;由于test任务依赖于compilation任务(不管是直接还是间接)
11.4 简化task名字
当你在命令行输入一个任务的时候,你不需要提供这个任务的全名,你只需要提供这个名字足够的前缀来区别于其他任务。例如,在上面的例子中,,你可以通过运行gradle d来执行dist任务:
Example 11.3. Abbreviated task name


Output of gradle di
> gradle di
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution


BUILD SUCCESSFUL


Total time: 1 secs
你也可以通过把驼峰式命名任务中每个单词的简写来简写这个任务,例如,你可以通过运行gradle comTest 甚至gradle cT来运行compileTest任务。
Example 11.4. Abbreviated camel case task name


Output of gradle cT
> gradle cT
:compile
compiling source
:compileTest
compiling unit tests


BUILD SUCCESSFUL


Total time: 1 secs
你也可以在-x选项中使用简写任务。


11.5选择要执行的构建任务
当你运行gradle命令的时候,他会查找当前目录下的构建文件,你也可以使用-b选项来选择其他的构建文件,如果你使用-b选项,settings.gradle将不会被使用,例如:
Example 11.5. Selecting the project using a build file


subdir/myproject.gradle
task hello << {
    println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
}
Output of gradle -q -b subdir/myproject.gradle hello
> gradle -q -b subdir/myproject.gradle hello
using build file 'myproject.gradle' in 'subdir'.
或者你也可以使用-p选项来描叙一个要使用的工程目录。对于多任务来说你应该使用-p选项来代替-b选项。
Example 11.6. Selecting the project using project directory


Output of gradle -q -p subdir hello
> gradle -q -p subdir hello
using build file 'build.gradle' in 'subdir'.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值