Gradle 没有为任务启用构建缓存

Gradle build cache is not enabled for task

我已为我的项目启用 gradle 构建缓存。但是,我们正在使用一些非内置任务,例如来自第三方插件的 npm-install。 Gradle 没有为这些任务启用缓存。例如,当执行此类任务时,它会显示如下内容:

Task :data-export-ui-kjs:npm-configure
Build cache key for task ':data-export-ui-kjs:npm-configure' is bbe0dafcd467a2afb2834acafe2993f5
Caching disabled for task ':data-export-ui-kjs:npm-configure': Caching has not been enabled for the task

有没有办法为此类非内置任务启用构建缓存?

Gradle documentation 说:

There are third party plugins that work well with the build cache. The most prominent examples are the Android plugin 3.1+ and the Kotlin plugin 1.2.21+. For other third party plugins, check their documentation to find out whether they support the build cache.

所以这取决于提供任务的插件npm-configure

如果您使用 SRS 的 gradle-node-plugin,根据 their changelog.

,它应该从 1.2.0 版本开始工作

任务需要选择可缓存(参见 Cacheable tasks),因为让每个任务都可缓存是没有意义的。

可以通过使用 @CacheableTask 注释任务或使用 task.outputs.cacheIf { true } 来选择加入,因此您可以为 npm 配置任务执行此操作。

请注意,可缓存任务需要 declare their inputs and outputs correctly。如果他们不这样做,那么您可能会遇到无效的构建失败。