KT-27675: Following the Gradle Java plugin, enable Kapt cache by default

As the Gradle Java plugin considers Java annotation processing cacheable
with any annotation processors, we can enable build cache by default
for Kapt tasks, leaving the opt-out flag just in case.

Issue #KT-27675 Fixed
This commit is contained in:
Sergey Igushkin
2019-03-14 16:17:49 +03:00
parent ffbd0e8af1
commit 914f7c8719
3 changed files with 21 additions and 22 deletions
@@ -135,27 +135,12 @@ class BuildCacheIT : BaseGradleIT() {
}
@Test
fun testKaptCachingDisabledByDefault() = with(Project("simple", GRADLE_VERSION, directoryPrefix = "kapt2")) {
fun testKaptCachingEnabledByDefault() = with(Project("simple", GRADLE_VERSION, directoryPrefix = "kapt2")) {
prepareLocalBuildCache()
build("build") {
assertSuccessful()
assertContains("Packing task ':kaptGenerateStubsKotlin'")
assertNotContains("Packing task ':kaptKotlin'")
assertContains("Caching disabled for task ':kaptKotlin': 'Caching is disabled by default for kapt")
}
File(projectDir, "build.gradle").appendText(
"\n" + """
afterEvaluate {
kaptKotlin.useBuildCache = true
}
""".trimIndent()
)
build("clean", "build") {
assertSuccessful()
assertContains(":kaptGenerateStubsKotlin FROM-CACHE")
assertContains("Packing task ':kaptGenerateStubsKotlin'")
assertContains("Packing task ':kaptKotlin'")
}
@@ -164,6 +149,21 @@ class BuildCacheIT : BaseGradleIT() {
assertContains(":kaptGenerateStubsKotlin FROM-CACHE")
assertContains(":kaptKotlin FROM-CACHE")
}
File(projectDir, "build.gradle").appendText(
"\n" + """
afterEvaluate {
kaptKotlin.useBuildCache = false
}
""".trimIndent()
)
build("clean", "build") {
assertSuccessful()
assertContains(":kaptGenerateStubsKotlin FROM-CACHE")
assertNotContains(":kaptKotlin FROM-CACHE")
assertContains("Caching disabled for task ':kaptKotlin': 'Caching is disabled for kapt")
}
}
}
@@ -19,8 +19,7 @@ abstract class KaptTask : ConventionTask(), TaskWithLocalState {
cacheOnlyIfEnabledForKotlin()
if (isBuildCacheSupported()) {
val reason = "Caching is disabled by default for kapt because of arbitrary behavior of external " +
"annotation processors. You can enable it by adding 'kapt.useBuildCache = true' to the build script."
val reason = "Caching is disabled for kapt with 'kapt.useBuildCache'"
outputs.cacheIf(reason) { useBuildCache }
}
}
@@ -42,10 +42,10 @@ open class KaptExtension {
@Deprecated("Use `annotationProcessor()` and `annotationProcessors()` instead")
open var processors: String = ""
/** Explicit opt-in switch for Kapt caching. Should be used when annotation processors used by this project are
* certain NOT to use anything aside from the task inputs in their logic and are guaranteed to produce the same
/** Opt-out switch for Kapt caching. Should be used when annotation processors used by this project are suspected of
* using anything aside from the task inputs in their logic and are not guaranteed to produce the same
* output on subsequent runs without input changes. */
var useBuildCache: Boolean = false
var useBuildCache: Boolean = true
private val apOptionsActions =
mutableListOf<(KaptAnnotationProcessorOptions) -> Unit>()