第六十三章. 比较构建

Chapter 63. Comparing Builds

构建比较支持是一个实验性 功能。这表示它是不完整的,而且还没有稳定的 Gradle 生产质量。同时也意味着本章 Gradle 用户指南还在完善中。
Build comparison support is an incubating feature. This means that it is incomplete and not yet at regular Gradle production quality. This also means that this Gradle User Guide chapter is a work in progress.

Gradle 提供了用于比较两个构建的结果(例如生成的二进制归档 ) 的支持。让你可能想要比较两个构建的结果的原因有几个。你可能想要比较:
Gradle provides support for comparing the outcomes (e.g. the produced binary archives) of two builds. There are several reasons why you may want to compare the outcomes of two builds. You may want to compare:

通过比较这些场景中的构建,通过了解结果中的差异,你可以对 Gradle 升级,迁移到 Gradle 或构建更改做出明智的决策。比较过程会生成一份 HTML 报告,其中概述发现了哪些结果是相同的,并确定不同结果之间的差异。
By comparing builds in these scenarios you can make an informed decision about the Gradle upgrade, migration to Gradle or build change by understanding the differences in the outcomes. The comparison process produces a HTML report outlining which outcomes were found to be identical and identifying the differences between non-identical outcomes.

63.1. 术语定义

63.1. Definition of terms

以下是用于构建比较的术语及其定义。
The following are the terms used for build comparison and their definitions.

“构建”
“Build”

在构建比较的上下文中,一个构建不一定是一个 Gradle 构建。它可以是任何可调用的“进程”,产生可观察的“结果”。在比较中,至少有一个构建将是 Gradle 构建。
In the context of build comparison, a build is not necessarily a Gradle build. It can be any invokable “process” that produces observable “outcomes”. At least one of the builds in a comparison will be a Gradle build.

“构建结果”
“Build Outcome”

在构建期间以可观察的方式发生的事情,例如创建 zip 文件或测试执行。这些都是用于比较的东西。
Something that happens in an observable manner during a build, such as the creation of a zip file or test execution. These are the things that are compared.

“源构建”
“Source Build”

正在对其进行比较的构建,通常是其“当前”状态的构建。换句话说,也就是构建比较的左边。
The build that comparisons are being made against, typically the build in its “current” state. In other words, the left hand side of the comparison.

“目标构建”
“Target Build”

与源构建进行比较的构建,通常是“建议”的构建。换句话说,也就是构建比较的右边。
The build that is being compared to the source build, typically the “proposed” build. In other words, the right hand side of the comparison.

“主机构建”
“Host Build”

执行比较过程的 Gradle 构建。它可能与“目标”或“源”构建相同的项目,也可能是一个完全独立的项目。它不需要与“源”或“目标”构建相同的 Gradle 版本。主机构建必须运行在 Gradle 1.2 或更高的版本。
The Gradle build that executes the comparison process. It may be the same project as either the “target” or “source” build or may be a completely separate project. It does not need to be the same Gradle version as the “source” or “target” builds. The host build must be run with Gradle 1.2 or newer.

“比较构建结果”
“Compared Build Outcome”

构建结果原来的目的是成为在“源”构建和“目标”构建中的逻辑等价物,因此具有可比较性。
Build outcomes that are intended to be logically equivalent in the “source” and “target” builds, and are therefore meaningfully comparable.

“未比较的构建结果”
“Uncompared Build Outcome”

如果无法从其他构建中找到逻辑等价物(比如一个构建产生了一个zip文件,而其他的构建不会),那么这个构建结果就是不可比的。
A build outcome is uncompared if a logical equivalent from the other build cannot be found (e.g. a build produces a zip file that the other build does not).

“未知的构建结果”
“Unknown Build Outcome”

主机构建无法理解的构建结果。当源构建或目标构建是比主机构建更新的 Gradle 版本且 Gradle 版本有新增加的结果类型时,就会有这种情况。目前为止,未知的构建结果是可以被比较的,因为它们可以确定与其他构建的未知构建结果是否为逻辑等价物,但无法对其实际执行的构建结果进行有意义的比较。对主机构建使用最新的 Gradle 版本将避免遇到未知的构建结果。
A build outcome that cannot be understood by the host build. This can occur when the source or target build is a newer Gradle version than the host build and that Gradle version exposes new outcome types. Unknown build outcomes can be compared in so far as they can be identified to be logically equivalent to an unknown build outcome in the other build, but no meaningful comparison of what the build outcome actually is can be performed. Using the latest Gradle version for the host build will avoid encountering unknown build outcomes.

63.2. 当前功能

63.2. Current Capabilities

由于它还是一个孵化中的功能,目前只是实现了最终功能的一部分。
As this is an incubating feature, a limited set of the eventual functionality has been implemented at this time.

63.2.1. 支持的构建

63.2.1. Supported builds

目前只支持执行Gradle构建。源构建和目标构建必须使用Gradle1.0或以上的版本来执行。主机构建则必须是1.2版本。
Only support for executing Gradle builds is available at this time. Source and target build must execute with Gradle newer or equal to 1.0. Host build must be at least 1.2.

未来的版本会支持从其他构建系统执行的构建,比如Apache Ant或Apache Maven,以及支持执行任意进程(比如基于shell脚本的构建)。
Future versions will provide support for executing builds from other build systems such as Apache Ant or Apache Maven, as well as support for executing arbitrary processes (e.g. shell script based builds)

63.2.2. 支持的构建结果

63.2.2. Supported build outcomes

目前仅支持比较 zip 归档的构建结果。包括jarwarear归档。
Only support for comparing build outcomes that are zip archives is supported at this time. This includes jar, war and ear archives.

未来的版本会提供对类似于测试执行(例如哪些测试被执行了,哪些测试失败了,等等)的构建结果比较的支持。
Future versions will provide support for comparing outcomes such as test execution (i.e. which tests were executed, which tests failed, etc.)

63.3. 比较Gradle构建

63.3. Comparing Gradle Builds

compare-gradle-compile 插件可用于方便地比较两个 Gradle 构建。该插件向项目添加了一个叫做“compareGradleBuilds”的CompareGradleBuilds任务。这个任务的配置指定了要比较的内容。默认情况下,它被配置为对当前构建,与本身使用当前的 Gradle 版本所执行的任务:“clean assemble”进行比较。
The compare-gradle-builds plugin can be used to facilitate a comparison between two Gradle builds. The plugin adds a CompareGradleBuilds task named “compareGradleBuilds” to the project. The configuration of this task specifies what is to be compared. By default, it is configured to compare the current build with itself using the current Gradle version by executing the tasks: “clean assemble”.

apply plugin: 'compare-gradle-builds'

可以通过配置这个任务来更改所比较的内容。
This task can be configured to change what is compared.

compareGradleBuilds {
    sourceBuild {
        projectDir "/projects/project-a"
        gradleVersion "1.1"
    }
    targetBuild {
        projectDir "/projects/project-b"
        gradleVersion "1.2"
    }
}

以上示例配置了两个使用了不同的Gradle版本的项目之间的比较。
The example above configures a comparison between two different projects using two different Gradle versions.

63.3.1. 尝试 Gradle 升级

63.3.1. Trying Gradle upgrades

你可以使用这个构建比较功能来快速尝试在你的构建中使用新的Gradle版本。
You can use the build comparison functionality to very quickly try a new Gradle version with your build.

想尝试对你当前的构建使用一个不同的Gradle版本,只需要对 根项目build.gradle 添加以下的配置。
To try your current build with a different Gradle version, simply add the following to the build.gradle of the root project.

apply plugin: 'compare-gradle-builds'

compareGradleBuilds {
    targetBuild.gradleVersion = "«gradle version»"
}

然后,简单地执行compareGradleBuilds任务。当它们执行的时候,你将会看到“源”构建和“目标”构建的控制台输出。
Then simply execute the compareGradleBuilds task. You will see the console output of the “source” and “target” builds as they are executing.

63.3.2. 比较“结果”

63.3.2. The comparison “result”

如果在比较结果之间存在任何的差异,那么任务就会失败,然后会给出一个对比较深入分析的HTML报告的位置。如果所有比较结果都相同,并且没有未比较的结果和未知的构建结果,那么这个任务就会执行成功。
If there are any differences between the compared outcomes, the task will fail. The location of the HTML report providing insight into the comparison will be given. If all compared outcomes are found to be identical, and there are no uncompared outcomes, and there are no unknown build outcomes the task will succeed.

你也可以通过设置ignoreFailures属性为true,来配置构建结果不同时任务不失败。
You can configure the task to not fail on compared outcome differences by setting the ignoreFailures property to true.

compareGradleBuilds {
    ignoreFailures = true
}

63.3.3. 哪些归档文件会被比较?

63.3.3. Which archives are compared?

对于要用于比较的归档文件,它必须被添加为archives配置的构件。有关如何配置和添加构建件的更多信息,请参阅《第51章,发布构件》。
For an archive to be a candidate for comparison, it must be added as an artifact of the archives configuration. Take a look at Chapter 51, Publishing artifacts for more information on how to configure and add artifacts.

同时,这个归档还必须由ZipJarWarEar任务生成。未来的 Gradle 版本在这方面上会更加灵活。
The archive must also have been produced by a Zip, Jar, War, Ear task. Future versions of Gradle will support increased flexibility in this area.