Gradle2.0用户指南翻译——第十一章. 使用Gradle命令行

简介: 翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2.0 。
翻译项目请关注Github上的地址:
https://github.com/msdx/gradledoc
本文翻译所在分支:
https://github.com/msdx/gradledoc/tree/2.0 。


第十一章. 使用Gradle命令行

Chapter 11. Using the Gradle Command-Line

本章介绍了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.

11.1. 执行多个任务

11.1. Executing multiple tasks

你可以通过在命令行中列出每个任务来在单个构建中执行多个任务。例如,gradle compile test命令将执行compiletest任务。 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.

下面定义了四个任务,其中disttest都依赖于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. 任务依赖 - Figure 11.1. Task dependencies

任务依赖

示例 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.

11.2. 排除任务

11.2. Excluding tasks

你可以使用-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.

11.3. 失败后继续构建

11.3. Continuing the build when a failure occurs

默认情况下,只要有任务失败,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).

11.4. 任务名缩写

11.4. Task name abbreviation

在命令行中指定任务时,可以不输入任务的全名,只需要提供可以足够唯一标识任务的任务名。例如,在上面的示例构建中,你可以通过运行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 compTestgradle 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.

11.5. 选择执行哪个构建

11.5. Selecting which build to execute

当你运行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.

示例11.6. 使用项目目录选择项目 - Example 11.6. Selecting the project using project directory

gradle -q -p subdir hello的输出结果
Output of gradle -q -p subdir hello

> gradle -q -p subdir hello
using build file 'build.gradle' in 'subdir'.

11.6. 获取构建的相关信息

目录
相关文章
|
开发工具
Gradle使用命令行打包apk
Gradle使用命令行打包apk
487 0
Gradle使用命令行打包apk
|
Java 容器
翻译:Gradle之 Java插件
原文地址 http://www.gradle.org/docs/current/userguide/java_plugin.html 23.1. Usage用法 要使用Java插件,在脚本里加入: Example 23.1. Using the Java plugin build.gradle apply plugin: 'java' 23.2. Source sets源集 Java插件引入了一个概念:源集(source set),一个源集就是一组被一起编译一起执行的源文件。
804 0
|
Java API
翻译:Gradle之构建脚本编写
原文地址 http://www.gradle.org/docs/current/userguide/writing_build_scripts.html 13.1. The Gradle build language构建语言 Gradle提供了一种“领域专用语言”(domain specific language) 或者 DSL对构建进行描述。
962 0
|
Java 数据库连接 Maven
翻译:Gradle之依赖管理
  原文地址 http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html   8.1. What is dependency management?何谓?为何? 依赖管理大致有两块:首先Gradle需要找到你工程需要的东西,这些东西就是所谓的“依赖”。
844 0
|
API
翻译:Gradle之构建脚本入门
原文地址http://www.gradle.org/docs/current/userguide/tutorial_using_tasks.html 6.1. Projects and tasks工程和任务 Gradle中的任何东西都基于俩概念: projects 工程 和 tasks 任务。
848 0
|
Java 测试技术 API
翻译--Gradle之Java工程入门
原文地址 http://www.gradle.org/docs/current/userguide/tutorial_java_projects.html   7.1. The Java plugin插件 我们已经看到Gradle是一个通用构建工具,它可以完成相当多的任务,只要你能在脚本里定义好。
1269 0
|
人工智能 移动开发 Java
Android Studio插件版本与Gradle 版本对应关系
Android Studio插件版本与Gradle 版本对应关系
2727 0
Android Studio插件版本与Gradle 版本对应关系
|
存储 Java Android开发
Android 开发 - 充分利用Gradle
Android 开发 - 充分利用Gradle
195 2
|
5天前
|
前端开发 Java Shell
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
56 20
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
4月前
|
Android开发
Android基于gradle task检查各个module之间资源文件冲突情况
Android基于gradle task检查各个module之间资源文件冲突情况
Android基于gradle task检查各个module之间资源文件冲突情况