未为任务启用 Gradle 构建缓存

dha*_*sav 10 gradle incremental-build kotlin gradle-cache

我已经为我的项目启用了 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
Run Code Online (Sandbox Code Playgroud)

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

wol*_*s42 7

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

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

请注意,可缓存任务需要正确声明其输入和输出。如果他们不这样做,那么您可能会遇到无效的构建失败。

  • 我的答案已经包含(@CacheableTask 或`task.outputs.cacheIf { true }`)。有关更多文档,请参阅 [Gradle 用户指南部分](https://docs.gradle.org/current/userguide/build_cache.html#enable_caching_of_non_cacheable_tasks)。 (2认同)