Gradle Build Cache 将自动跟踪任务的输入和输出,并跳过任何未更改的。
启用 Gradle 构建缓存
可以通过添加gradle.properties在本地启用
org.gradle.caching=true
或者通过在命令行中添加标志
./gradlew tests --build-cache
共享构建缓存
通过 HTTP 跨多台机器的项目 can be shared 的构建缓存。
注册任务输入
Gradle 需要了解任务的所有输入和输出,否则任务可能会被跳过,因此请确保它们已正确注册。
例如,如果某些集成测试依赖于环境变量,则将环境变量注册为测试任务输入。
// build.gradle.kts
tasks.named("integrationTest") {
// TEST_TASK_QUALITY is used in integration tests to change <blah blah blah>
// register it as an input so Gradle knows when to re-run the tests
inputs.property("TEST_TASK_QUALITY", providers.environmentVariable("TEST_TASK_QUALITY"))
}
稳定的任务输出
Gradle 会将某些任务的输出用作其他任务的输入。如果输出不稳定,Gradle 将始终重新运行相关任务。
因此,值得在所有项目中启用reproducible builds。
// build.gradle.kts
tasks.withType<AbstractArchiveTask>().configureEach {
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
}
此外,请考虑使用 input normalization 来获取您项目中的任何自定义文件。