https://github.com/msdx/gradledoc
本文翻译所在分支:
https://github.com/msdx/gradledoc/tree/2.0 。
本章介绍了Gradle命令行的基础知识,正如你在前面章节所看到的使用gradle命令来运行构建。
This chapter introduces the basics of the Gradle command-line. You run a build using the gradle command, which you have already seen in action in previous chapters.
你可以通过在命令行中列出每个任务来在单个构建中执行多个任务。例如,gradle compile test
命令将执行compile
和test
任务。 Gradle将按照命令行中的顺序依次执行这些任务以及每一个任务所依赖的其他任务。每个任务只会被执行一次,无论它们是如何被包含在构建中的:即无论是在命令行中指定的,还是另一个任务的依赖,抑或两者都有。来看下面的例子。
You can execute multiple tasks in a single build by listing each of the tasks on the command-line. For example, the command gradle compile test
will execute the compile
and test
tasks. Gradle will execute the tasks in the order that they are listed on the command-line, and will also execute the dependencies for each task. Each task is executed once only, regardless of how it came to be included in the build: whether it was specified on the command-line, or it a dependency of another task, or both. Let's look at an example.
下面定义了四个任务,其中dist
和test
都依赖于compile
任务。对于这个构建脚本,运行gradle dist test
只会使compile
任务被执行一次。
Below four tasks are defined. Both dist
and test
depend on the compile
task. Running gradle dist test
for this build script results in the compile
task being executed only once.
示例 11.1. 执行多个任务 - 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' }
gradle dist test
的输出结果
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
的结果是完全相同的。
Because each task is executed once only, executing gradle test test
is exactly the same as executing gradle test
.
你可以使用-x
命令行选项加上任务名称来排除某些会被执行的任务。让我们用上面的例子来演示一下。
You can exclude a task from being executed using the -x
command-line option and providing the name of the task to exclude. Let's try this with the sample build file above.
示例 11.2. 排除任务 - Example 11.2. Excluding tasks
gradle dist -x test
的输出结果
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
任务所依赖的任务,如compileTest
也一样没有执行。而test
所依赖的同时也是另一个任务所需要的任务,如compile
,则仍然执行。
You can see from the output of this example, that the test
task is not executed, even though it is a dependency of the dist
task. You will also notice that the test
task's dependencies, such as compileTest
are not executed either. Those dependencies of test
that are required by another task, such as compile
, are still executed.
默认情况下,只要有任务失败,Gradle就将中断执行让构建失败。这样做会使构建更快地结束,但也会让其他可能发生的失败不被发现。因此你可以使用--continue
选项来在单个构建执行中发现尽可能多的失败。
By default, Gradle will abort execution and fail the build as soon as any task fails. This allows the build to complete sooner, but hides other failures that would have occurred. In order to discover as many failures as possible in a single build execution, you can use the --continue
option.
当带上--continue
参数执行时,Gradle将执行所有要执行的任务,即该任务的所有依赖任务都会完成执行,而不是一出现错误就停止。所有失败的信息将在构建结束时报告出来。
When executed with --continue
, Gradle will execute every task to be executed where all of the dependencies for that task completed without failure, instead of stopping as soon as the first failure is encountered. Each of the encountered failures will be reported at the end of the build.
如果任务失败了,则依赖于它的所有后续任务将不会被执行,因为那样做是不安全的。例如,如果在测试代码中存在编译失败,测试将不会运行;因为测试任务会(直接或间接地)依赖于编译任务。
If a task fails, any subsequent tasks that were depending on it will not be executed, as it is not safe to do so. For example, tests will not run if there is a compilation failure in the code under test; because the test task will depend on the compilation task (either directly or indirectly).
在命令行中指定任务时,可以不输入任务的全名,只需要提供可以足够唯一标识任务的任务名。例如,在上面的示例构建中,你可以通过运行gradle d
来执行dist
任务:
When you specify tasks on the command-line, you don't have to provide the full name of the task. You only need to provide enough of the task name to uniquely identify the task. For example, in the sample build above, you can execute task dist
by running gradle d
:
示例11.3. 缩写任务名称 - Example 11.3. Abbreviated task name
gradle di
的输出结果
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 compTest
或gradle cT
执行compileTest
任务
You can also abbreviate each word in a camel case task name. For example, you can execute task compileTest
by running gradle compTest
or even gradle cT
示例11.4. 缩写驼峰任务名称 - Example 11.4. Abbreviated camel case task name
gradle cT
的输出结果
Output of gradle cT
> gradle cT :compile compiling source :compileTest compiling unit tests BUILD SUCCESSFUL Total time: 1 secs
在使用-x
命令行选项时,依然可以使用这些缩写。
You can also use these abbreviations with the -x
command-line option.
当你运行gradle这个命令时,它会在当前目录中查找一个构建文件。你也可以使用-b
选项来选择另一个构建文件。如果你使用-b
选项,那么Gradle就不会去用到settings.gradle
文件。例:
When you run the gradle command, it looks for a build file in the current directory. You can use the -b
option to select another build file. If you use -b
option then settings.gradle
file is not used. Example:
示例11.5. 使用构建文件选择项目 - 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'."
}
gradle -q -b subdir/myproject.gradle hello
的输出结果
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
选项。
Alternatively, you can use the -p
option to specify the project directory to use. For multi-project builds you should use -p
option instead of -b
option.