第六十一章. Gradle包装器

Chapter 61. The Gradle Wrapper

Gradle 包装器(下称“包装器”)是启动 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.

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. 甚至还有更好的效果,能保证用户在构建中所使用的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版本,你不需要重新运行wrapper任务。只需要修改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.

61.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.