Add back empty stub implementations of KotlinGradleSubplugin (KT-39809)

* If a KotlinGradleSubplugin implements
 KotlinCompilerPluginSupportPlugin, don't apply it as a legacy plugin

* Add the legacy KotlinGradleSubplugin implementations back so that
 when there's JAR hell with META-INF/services in the old artifacts
 pointing to the classes, loading the plugins with ServiceLoader (legacy
 implementation) from the new artifacts would not fail.

* There's a corner case for plugins not in kotlin-gradle-plugin
 If a newer and older version of such a subplugin is used with an older
 version of the Kotlin Gradle plugin, the latter will find the META-INF
 entries and will try to load the subplugins from the new version. With
 the original fix for KT-39809, this would result in silently ignored
 empty stub implementations.

 Given that the Kotlin Gradle plugin can now check if a subplugin
 supports the new API, it's OK to keep the old entries and make the
 stub implementations throw a build error when called, so that improper
 plugin versions are not ignored and are clearly reported.

 Note that this is only necessary for the subplugins not bundled in the
 kotlin-gradle-plugin module, as those will always be in sync with the
 Kotlin version.

Issue #KT-39809 Fixed
This commit is contained in:
Sergey Igushkin
2020-06-24 14:56:38 +03:00
parent cb936dd82e
commit 1f5fa5eb7c
15 changed files with 220 additions and 21 deletions
@@ -16,13 +16,21 @@
package org.jetbrains.kotlinx.serialization.gradle
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
import org.gradle.api.tasks.compile.AbstractCompile
import org.gradle.tooling.provider.model.ToolingModelBuilder
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
class SerializationGradleSubplugin :
KotlinCompilerPluginSupportPlugin,
@Suppress("DEPRECATION") // implementing to fix KT-39809
KotlinGradleSubplugin<AbstractCompile> {
class SerializationGradleSubplugin : KotlinCompilerPluginSupportPlugin {
companion object {
const val SERIALIZATION_GROUP_NAME = "org.jetbrains.kotlin"
const val SERIALIZATION_ARTIFACT_NAME = "kotlin-serialization"
@@ -39,8 +47,20 @@ class SerializationGradleSubplugin : KotlinCompilerPluginSupportPlugin {
override fun getPluginArtifact(): SubpluginArtifact =
SubpluginArtifact(SERIALIZATION_GROUP_NAME, SERIALIZATION_ARTIFACT_NAME)
override fun getNativeCompilerPluginArtifact(): SubpluginArtifact? =
override fun getPluginArtifactForNative(): SubpluginArtifact? =
SubpluginArtifact(SERIALIZATION_GROUP_NAME, SERIALIZATION_ARTIFACT_UNSHADED_NAME)
override fun getCompilerPluginId() = "org.jetbrains.kotlinx.serialization"
//region Stub implementation for legacy API, KT-39809
override fun isApplicable(project: Project, task: AbstractCompile): Boolean = true
override fun apply(
project: Project, kotlinCompile: AbstractCompile, javaCompile: AbstractCompile?, variantData: Any?, androidProjectHandler: Any?,
kotlinCompilation: KotlinCompilation<KotlinCommonOptions>?
): List<SubpluginOption> = throw GradleException(
"This version of the kotlin-serialization Gradle plugin is built for a newer Kotlin version. " +
"Please use an older version of kotlin-serialization or upgrade the Kotlin version to make them match."
)
//endregion
}
@@ -0,0 +1 @@
org.jetbrains.kotlinx.serialization.gradle.SerializationGradleSubplugin