第六十一章. Gradle 包装器

Chapter 61. The Gradle Wrapper

Gradle Wrapper(下称“包装器”)是启动 Gradle 构建的首选方法。包装器在 Windows 上是一个批处理脚本,而在其他操作系统上是个 shell 脚本。当你通过包装器启动 Gradle 构建时,将自动下载 Gradle 并用于运行构建。
The Gradle Wrapper (henceforth referred to as the “wrapper”) is the preferred way of starting a Gradle build. The wrapper is a batch script on Windows, and a shell script for other operating systems. When you start a Gradle build via the wrapper, Gradle will be automatically downloaded and used to run the build.

应该 将包装器纳入版本控制。通过将包装器分发到项目中,任何人都可以在未安装 Gradle 的情况下使用它。更好的效果是能让用户使用这个构建所设计的 Gradle 版本。当然,这对于 连续集成 服务器(即定期构建项目的服务器)也很重要,因为它不需要在服务器上进行配置。
The wrapper is something you should check into version control. By distributing the wrapper with your project, anyone can work with it without needing to install Gradle beforehand. Even better, users of the build are guaranteed to use the version of Gradle that the build was designed to work with. Of course, this is also great for continuous integration servers (i.e. servers that regularly build your project) as it requires no configuration on the server.

你可以通过在你的构建脚本中添加一个 Wrapper 任务并配置,然后执行它。
You install the wrapper into your project by adding and configuring a Wrapper task in your build script, and then executing it.

示例 61.1. Wrapper 任务 - Example 61.1. Wrapper task

build.gradle

task wrapper(type: Wrapper) {
    gradleVersion = '1.4'
}

执行之后,你会在你的项目目录中发现以下新生成或更新的文件(在使用的 wrapper 任务为默认配置的情况下)。
After such an execution you find the following new or updated files in your project directory (in case the default configuration of the wrapper task is used).

示例 61.2. 包装器生成的文件 - Example 61.2. Wrapper generated files

构建布局
Build layout

simple/
  gradlew
  gradlew.bat
  gradle/wrapper/
    gradle-wrapper.jar
    gradle-wrapper.properties

所有这些文件都 应该 提交到版本控制系统中。这只需要做一次。在将这些文件添加到项目之后,应该使用所添加的 gradlew 命令来构建项目。gradlew 命令可以和 gradle 命令完全一样地使用。
All of these files should be submitted to your version control system. This only needs to be done once. After these files have been added to the project, the project should then be built with the added gradlew command. The gradlew command can be used exactly the same way as the gradle command.

如果要切换到新版本的 Gradle,不用重新运行包装器任务,只要修改 gradle-wrapper.properties 文件中的相应属性就可以了。但是,如果有例如 gradle 包装器功能提升的情况的话,你还是需要重新生成包装器文件。
If you want to switch to a new version of Gradle you don't need to rerun the wrapper task. It is good enough to change the respective entry in the gradle-wrapper.properties file. But if there is for example an improvement in the gradle-wrapper functionality you need to regenerate the wrapper files.

60.1. 配置

61.1. Configuration

如果你使用 gradlew 运行 Gradle ,那么包装器会检查要使用的 Gradle 分发是否可用。如果不可用,会尝试下载它,可用的话会将传给 gradlew 命令的所有参数都委托给这个分发包的 gradle 命令。
If you run Gradle with gradlew, the wrapper checks if a Gradle distribution for the wrapper is available. If not it tries to download it, otherwise it delegates to the gradle command of this distribution with all the arguments passed originally to the gradlew command.

配置 Wrapper 任务时,可以指定要使用的 Gradle 版本。gradlew 命令将从 Gradle 仓库下载相应的分发。另外,你也可以指定 Gradle 分发包的下载 URL,gradlew 命令将使用这个 URL 下载分发。如果你没有指定 Gradle 版本或下载 URL,那么 gradlew 命令默认情况下将下载生成了包装器文件的 Gradle 版本。
When you configure the Wrapper task, you can specify the Gradle version you wish to use. The gradlew command will download the appropriate distribution from the Gradle repository. Alternatively, you can specify the download URL of the Gradle distribution. The gradlew command will use this URL to download the distribution. If you specify neither a Gradle version or download URL, the gradlew command will by default download whichever version of Gradle was used to generate the wrapper files.

有关如何配置包装器的详细信息,请参见 Wrapper
For the details on how to configure the wrapper, see Wrapper

如果你不想项目在通过 gradlew 构建时进行下载,那么只需将 Gradle 分发包的 zip 文件添加到版本控制中包装器配置所指定的位置上。它支持相对 URL——你可以通过相对于 gradle-wrapper.properties 文件的位置指定一个分发文件。
If you don't want any download to happen when your project is build via gradlew, simply add the Gradle distribution zip to your version control at the location specified by your wrapper configuration. A relative URL is supported - you can specify a distribution file relative to the location of gradle-wrapper.properties file.

如果你通过包装器构建,这台机器上已安装的任何 Gradle 分发包都会被忽略。
If you build via the wrapper, any existing Gradle distribution installed on the machine is ignored.

61.2. Unix 文件权限

61.2. Unix file permissions

Wrapper 任务添加了相应的文件权限,以允许 gradlew *NIX 命令的执行。Subversion 会保留此文件权限。我们不确定其他版本控制系统是如何处理这一问题的,所以能让它始终可以使用的方法是执行 sh gradlew
The Wrapper task adds appropriate file permissions to allow the execution for the gradlew *NIX command. Subversion preserves this file permission. We are not sure how other version control systems deal with this. What should always work is to execute sh gradlew.