KT-31127: add only generated Java sources to JavaCompile inputs

This commit changes how generated sources are added to the Gradle JavaCompile
task. Previously, directory was added to sources which caused issues with
annotation processors that only generate Kotlin sources. I.e. JavaCompile task
was executed but no java sources existed.

Now, only generated Java sources are added to the JavaCompile task. This
means that if only Kotlin sources are generated JavaCompile task will be
skipped.

Fixes: KT-31127
Test: Kapt3IT.testKotlinProcessorUsingFiler
This commit is contained in:
Ivan Gavrilovic
2019-05-28 17:19:26 +01:00
committed by Yan Zhulanow
parent 55ba985cd2
commit 695f202e46
6 changed files with 86 additions and 5 deletions
@@ -517,6 +517,12 @@ abstract class BaseGradleIT {
assertTasksNotRealized(*tasks)
}
fun CompiledProject.assertTasksSkipped(vararg tasks: String) {
for (task in tasks) {
assertContains("Skipping task '$task'")
}
}
fun CompiledProject.getOutputForTask(taskName: String): String {
val taskOutputRegex = ("(?:\\[LIFECYCLE] \\[class org\\.gradle(?:\\.internal\\.buildevents)?\\.TaskExecutionLogger] :$taskName|" +
"\\[org\\.gradle\\.execution\\.plan\\.DefaultPlanExecutor\\] :$taskName.*?started)" +
@@ -612,4 +612,33 @@ open class Kapt3IT : Kapt3BaseIT() {
assertTasksExecuted(":dac:jdk:kaptGenerateStubsKotlin", ":dac:jdk:compileKotlin")
}
}
/** Regression test for KT-31127. */
@Test
fun testKotlinProcessorUsingFiler() {
val project = Project("kotlinProject").apply {
setupWorkingDir()
gradleBuildScript().appendText("""
apply plugin: 'kotlin-kapt'
dependencies {
kapt "org.jetbrains.kotlin:annotation-processor-example:${"$"}kotlin_version"
implementation "org.jetbrains.kotlin:annotation-processor-example:${"$"}kotlin_version"
}
""".trimIndent())
// The test must not contain any java sources in order to detect the issue.
Assert.assertEquals(emptyList<File>(), projectDir.allJavaFiles().toList())
projectDir.getFileByName("Dummy.kt").modify {
it.replace("class Dummy", "@example.KotlinFilerGenerated class Dummy")
}
}
project.build("build") {
assertSuccessful()
assertFileExists("build/generated/source/kapt/main/demo/DummyGenerated.kt")
assertTasksExecuted(":compileKotlin")
assertTasksSkipped(":compileJava")
}
}
}
@@ -266,8 +266,6 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
pluginOptions += SubpluginOption("aptMode", aptMode)
disableAnnotationProcessingInJavaTask()
javaCompile?.source(generatedFilesDir)
pluginOptions += FilesSubpluginOption("sources", listOf(generatedFilesDir))
pluginOptions += FilesSubpluginOption("classes", listOf(getKaptGeneratedClassesDir(project, sourceSetName)))
@@ -527,7 +525,9 @@ private val artifactType = Attribute.of("artifactType", String::class.java)
internal fun registerGeneratedJavaSource(kaptTask: KaptTask, javaTask: AbstractCompile) {
javaTask.source(kaptTask.destinationDir)
val generatedJavaSources = javaTask.project.fileTree(kaptTask.destinationDir)
generatedJavaSources.include("**/*.java")
javaTask.source(generatedJavaSources)
}
internal fun Configuration.getNamedDependencies(): List<Dependency> = allDependencies.filter { it.group != null && it.name != null }