第六十三章. 构建比较

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版本。主机构建必须运行在Gradle1.12或以上。
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版本可以避免遇到未知的构建结果。
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-builds可以用于方便地比较两个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配置中的一个构件。关于如何配置及添加构件,可以查阅 第五十一章,发布构件
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.