[Kotlin, Gradle] Load generated sources in JVM projects only if KGP version >= 2.3.0

Since this version new API on KGP side was introduced. Otherwise, we should skip the reflective method call.

^KTIJ-37636


(cherry picked from commit 3fc9bcb627cb41e16315a1b8927ab0379585340f)

IJ-MR-192445

GitOrigin-RevId: c5bac9d9a4bc1af0a22c469b7f3f5bc026f4e429
This commit is contained in:
Yahor Berdnikau
2026-02-20 02:56:52 +00:00
committed by intellij-monorepo-bot
parent c2fa158f85
commit 938b4cf35c
3 changed files with 20 additions and 10 deletions
@@ -239,6 +239,8 @@ class KotlinGradleModelBuilder : AbstractKotlinGradleModelBuilder(), ModelBuilde
val extraProperties = HashMap<String, KotlinTaskProperties>()
val generatedSources = mutableSetOf<String>()
val kotlinPluginVersion = project.getKotlinPluginVersion()?.let { KotlinGradlePluginVersion.parse(it) }
val kotlinCompileTasks = target?.let { it.compilations ?: emptyList() }
?.mapNotNull { compilation -> compilation.getCompileKotlinTaskName(project) }
?: (project.getAllTasks(false)[project]?.filter { it.javaClass.name in kotlinCompileTaskClasses } ?: emptyList())
@@ -254,7 +256,7 @@ class KotlinGradleModelBuilder : AbstractKotlinGradleModelBuilder(), ModelBuilde
extraProperties.acknowledgeTask(compileTask, null)
}
generatedSources.addAll(project.getKotlinSourceSetGeneratedSourceRoots().map { it.absolutePathString() })
generatedSources.addAll(project.getKotlinSourceSetGeneratedSourceRoots(kotlinPluginVersion).map { it.absolutePathString() })
val platform = platformPluginId ?: pluginToPlatform.entries.singleOrNull { project.plugins.findPlugin(it.key) != null }?.value
@@ -77,14 +77,16 @@ private fun getJavaSourceRoot(project: Project, sourceSet: String): Set<File>? {
return javaSourceSet?.java?.srcDirs
}
private fun Task.getKotlinPluginVersion(): String? {
internal fun Project.getKotlinPluginVersion(): String? {
try {
val pluginWrapperClass = javaClass.classLoader.loadClass(kotlinPluginWrapper)
// Ensuring that the proper Gradle classpath with KGP is used by getting KGP/Kotlin extension
// otherwise we may get the parent classpath without any 3rd party plugins applied in the project
val kotlinExtension = extensions.findByName("kotlin") ?: return null
val pluginWrapperClass = kotlinExtension::class.java.classLoader.loadClass(kotlinPluginWrapper)
val getVersionMethod =
pluginWrapperClass.getMethod("getKotlinPluginVersion", javaClass.classLoader.loadClass("org.gradle.api.Project"))
return getVersionMethod.invoke(null, this.project) as String
} catch (e: Exception) {
}
} catch (_: Exception) {}
return null
}
@@ -98,6 +100,6 @@ fun getKotlinTaskProperties(compileTask: Task, classifier: String?): KotlinTaskP
compileTask.getIsIncremental(),
compileTask.getPackagePrefix(),
compileTask.getPureKotlinSourceRoots(compileTask.getSourceSetName(), classifier),
compileTask.getKotlinPluginVersion()
compileTask.project.getKotlinPluginVersion()
)
}
@@ -7,14 +7,20 @@ import org.gradle.api.Project
import org.jetbrains.kotlin.idea.gradleTooling.reflect.KotlinSourceSetReflection
import java.nio.file.Path
internal fun Project.getKotlinSourceSetGeneratedSourceRoots(): List<Path> {
// KotlinSourceSet.generatedKotlin was introduced in Kotlin Gradle plugin 2.3.0
private const val MIN_KOTLIN_PLUGIN_VERSION = "2.3.0"
internal fun Project.getKotlinSourceSetGeneratedSourceRoots(
kotlinPluginVersion: KotlinGradlePluginVersion?
): List<Path> {
if (kotlinPluginVersion == null || kotlinPluginVersion < MIN_KOTLIN_PLUGIN_VERSION) return emptyList()
val kotlinExtension = project.extensions.findByName("kotlin") ?: return emptyList()
val kotlinExtensionClass = kotlinExtension.javaClass
val getSourceSets = kotlinExtensionClass.getMethodOrNull("getSourceSets") ?: return emptyList()
val sourceSets = getSourceSets.invoke(kotlinExtension) as NamedDomainObjectCollection<*>
val generatedSourceRoots = sourceSets.map { it as Named }.fold(mutableListOf<Path>()) { acc, named ->
named.getKotlinSourceSetGeneratedSourceRoots()?.let { acc.addAll(it) }
acc
val generatedSourceRoots = sourceSets.map { it as Named }.flatMap {
it.getKotlinSourceSetGeneratedSourceRoots() ?: emptyList()
}
return generatedSourceRoots.toList()
}