Gradle Build Cache Basics – Gradle构建缓存基础知识

最后修改: 2022年 4月 28日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Build caches can make the code build process faster and, thus, improve developers’ productivity. In this article, we’ll learn about the basics of Gradle build cache.

构建缓存可以使代码构建过程更快,从而提高开发人员的工作效率。在这篇文章中,我们将了解Gradle构建缓存的基本知识.

2. What Is Gradle Build Cache?

2.什么是Gradle Build Cache?

The Gradle build cache is semi-permanent storage that saves the build tasks’ outputs. It enables the reuse of already generated artifacts from the previous builds. The guiding principle behind the Gradle build cache is that it should avoid rebuilding the tasks that have already been built, provided the inputs are not changed. In this way, the time to complete the subsequent builds is reduced.

Gradle构建缓存是一个半永久性的存储,用于保存构建任务的输出。它可以重用以前的构建中已经生成的工件。Gradle构建缓存背后的指导原则是,只要输入没有改变,它就应该避免重建已经构建过的任务。这样一来,完成后续构建的时间就会减少。

In Gradle, a build cache key uniquely identifies an artifact or a task output. Before executing a task, Gradle computes the cache key by hashing each input to the task. It then looks at the remote or local cache to check if the task output corresponding to the computed cache key already exists. If it’s not present, then the task is executed. Otherwise, Gradle reuses the existing task output.

在Gradle中,构建缓存密钥唯一地标识了一个工件或任务输出。在执行一个任务之前,Gradle通过对任务的每个输入进行散列计算缓存密钥。然后,它查看远程或本地缓存,以检查与计算的缓存密钥相对应的任务输出是否已经存在。如果不存在,则执行该任务。否则,Gradle会重新使用现有的任务输出。

Now, let’s look at two different kinds of Gradle build caches.

现在,让我们看一下两种不同的Gradle构建缓存。

2.1. Local Build Cache

2.1.本地构建缓存

The local build cache uses a system directory to store tasks outputs. The default location is the Gradle user home directory that points to $USER_HOME/.gradle/caches. Every time we run the build in our system, artifacts will be stored here. It is enabled by default, and its location is configurable.

本地构建缓存使用一个系统目录来存储任务输出。默认位置是Gradle用户的主目录,指向$USER_HOME/.gradle/caches。每次我们在系统中运行构建时,工件都会被存储在这里。它默认是启用的,其位置是可配置的。

2.2. Remote Build Cache

2.2.远程构建缓存

The remote cache is a shared build cache. Reading from and writing to the remote cache is done via HTTP. One of the most common use cases of remote cache is continuous integration builds. On every clean build, the CI server populates the remote cache. So, the components that are changed will not be rebuilt, thereby speeding up the CI builds. Moreover, task outputs can also be shared among CI agents. The remote cache is not enabled by default.

远程缓存是一个共享的构建缓存。读取和写入远程缓存是通过HTTP完成的。远程缓存最常见的使用情况之一是持续集成构建。在每次干净的构建中,CI服务器都会填充远程缓存。因此,被改变的组件将不会被重建,从而加快了CI构建的速度。此外,任务输出也可以在CI代理之间共享。默认情况下,远程缓存没有被启用。

When both remote and local caches are enabled, then the build output is first checked in the local cache. If the output isn’t present in the local cache, it’ll be downloaded from the remote cache and also stored in the local cache. Then, in the next build, the same task output is taken from the local cache to speed up the build process.

当远程和本地缓存都启用时,首先在本地缓存中检查构建输出。如果输出不在本地缓存中,它就会从远程缓存中下载并存储在本地缓存中。然后,在下一次构建中,同样的任务输出会从本地缓存中获取,以加快构建过程。

3. Configuring Gradle Build Cache

3.配置Gradle Build Cache

We can configure the caches by providing the Settings.build-cache block in the settings.gradle file. Here, we’re using Groovy closures for writing configurations. Let’s look at how to configure different types of cache.

我们可以通过在settings.gradle文件中提供Settings.build-cache块来配置缓冲区。在这里,我们使用Groovy closures来编写配置。我们来看看如何配置不同类型的缓存。

3.1. Configuring Local Build Cache

3.1.配置本地构建缓存

Let’s add the local build cache configuration in the settings.gradle file:

让我们在settings.gradle文件中添加本地构建缓存配置。

buildCache {
    local {
        directory = new File(rootDir, 'build-cache')
        removeUnusedEntriesAfterDays = 30
    }
}

In the above code block, the directory object represents the location to store the build outputs. Here, the rootDir variable represents the root directory of the project. We can also change the directory object to point to another location.

在上面的代码块中,directory对象代表了存储构建输出的位置。这里,rootDir变量代表项目的根目录。我们也可以改变directory对象,使其指向另一个位置。

To save space, the local build cache also removes the entries periodically that are not being used for a specified period of time. The property removeUnusedEntriesAfterDays is used to configure the number of days after which unused artifacts will be removed from the local cache. Its default value is seven days.

为了节省空间,本地构建缓存也会定期删除在指定时间内未被使用的条目。属性removeUnusedEntriesAfterDays用于配置未使用的工件将从本地缓存中删除的天数。它的默认值是七天。

We can also clean it manually by deleting the entries from the $USER_HOME/.gradle/caches folder. On a Linux system, we can use the rm command to clean the directory:

我们也可以通过删除$USER_HOME/.gradle/caches文件夹中的条目来手动清理。在Linux系统中,我们可以使用rm命令来清理该目录。

rm -r $HOME/.gradle/caches

We can also configure some additional properties. For instance, enabled is a boolean property that represents whether the local cache is enabled or not. Build outputs will be stored in the cache if the push property is set to true. Its value is true for the local build cache by default.

我们还可以配置一些额外的属性。例如,enabled是一个布尔属性,表示是否启用本地缓存。如果push属性被设置为true,构建输出将被存储在缓存中。对于本地构建缓存,其值默认为true

3.2. Configuring Remote Cache

3.2.配置远程缓存

Let’s add the buildCache block in settings.gradle file to configure the remote cache.

让我们在settings.gradle文件中添加buildCache块来配置远程缓存。

For remote cache, we need to provide the location in the form of URL as well as the username and password to access it:

对于远程缓存,我们需要以URL的形式提供位置,以及访问它的用户名密码

buildCache {
    remote(HttpBuildCache) {
        url = 'https://example.com:8123/cache/'
        credentials {
            username = 'build-cache-user-name'
            password = 'build-cache-password'
        }
    }
}

4. Advantages of Using Gradle Build Cache

4.使用Gradle Build Cache的优势

Now let’s look at some of the benefits of using Gradle build cache:

现在让我们来看看使用Gradle build cache的一些好处。

  • It can improve the productivity of a developer by reducing the build time.
  • Because task outputs are reused if the inputs are not changed, it can also help in reducing the build time when switching back and forth between version control branches.
  • Using remote cache can significantly reduce the amount of work CI agents need to do to generate the builds. This also reduces the infrastructure needed for the CI builds.
  • Reduces the network usage on the CI machines, as results from the remote cache will be stored in the local cache.
  • The remote cache can help in sharing the results among developers. This eliminates the need to re-build the changes locally made by other developers working on the same project.
  • The remote cache can also enable sharing of the builds among CI agents.
  • As build time is reduced, this leads to faster feedback cycles. Hence, builds will be generated more often. Consequently, this could improve the quality of the software.

5. Conclusion

5.总结

In this article, we learned about Gradle build cache and how it speeds up the build process. We also explored its different types and their configuration. Finally, we discussed the benefits of Gradle build cache.

在这篇文章中,我们了解了Gradle构建缓存以及它是如何加快构建过程的。我们还探讨了它的不同类型和它们的配置。最后,我们讨论了Gradle build cache的好处。