diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildToolsApiJvmCompilationIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildToolsApiJvmCompilationIT.kt index c76c5d32277..cb5ef166c41 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildToolsApiJvmCompilationIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildToolsApiJvmCompilationIT.kt @@ -159,7 +159,7 @@ private fun TestProject.enableOtherVersionBuildToolsImpl() { buildGradle.append( """ kotlin { - useCompilerVersion("$OTHER_KOTLIN_VERSION") + compilerVersion.set("$OTHER_KOTLIN_VERSION") } """.trimIndent() ) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinProjectExtension.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinProjectExtension.kt index c3fba256cb4..22fa5385625 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinProjectExtension.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinProjectExtension.kt @@ -10,12 +10,11 @@ import org.gradle.api.Named import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Project import org.gradle.api.internal.plugins.DslObject +import org.gradle.api.provider.Property import org.gradle.jvm.toolchain.JavaLanguageVersion import org.gradle.jvm.toolchain.JavaToolchainSpec import org.jetbrains.kotlin.buildtools.api.ExperimentalBuildToolsApi import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi -import org.jetbrains.kotlin.gradle.internal.KOTLIN_BUILD_TOOLS_API_IMPL -import org.jetbrains.kotlin.gradle.internal.KOTLIN_MODULE_GROUP import org.jetbrains.kotlin.gradle.plugin.* import org.jetbrains.kotlin.gradle.plugin.KotlinPluginLifecycle.CoroutineStart.Undispatched import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget @@ -139,18 +138,26 @@ abstract class KotlinTopLevelExtension(internal val project: Project) : KotlinTo } } - /** - * Allows to use a different version of the Kotlin Build Tools API implementation and effectively a different version of the compiler. - * - * By default, the Kotlin Build Tools API implementation of the same version as the KGP is used. - * - * Currently only has an effect if the `kotlin.compiler.runViaBuildToolsApi` Gradle property is set to `true`. - */ + @Deprecated("This method is replaced by the `compilerVersion` property", ReplaceWith("compilerVersion"), DeprecationLevel.ERROR) @ExperimentalKotlinGradlePluginApi @ExperimentalBuildToolsApi fun useCompilerVersion(version: String) { - project.dependencies.add(BUILD_TOOLS_API_CLASSPATH_CONFIGURATION_NAME, "$KOTLIN_MODULE_GROUP:$KOTLIN_BUILD_TOOLS_API_IMPL:$version") + compilerVersion.set(version) } + + /** + * The version of the Kotlin compiler. + * + * By default, the Kotlin Build Tools API implementation of the same version as the KGP is used. + * + * Be careful with reading the property's value as eager reading will finalize the value and prevent it from being configured. + * + * Note: Currently only has an effect if the `kotlin.compiler.runViaBuildToolsApi` Gradle property is set to `true`. + */ + @ExperimentalKotlinGradlePluginApi + @ExperimentalBuildToolsApi + val compilerVersion: Property = + project.objects.propertyWithConvention(project.getKotlinPluginVersion()).chainedFinalizeValueOnRead() } internal fun ExplicitApiMode.toCompilerValue() = when (this) { diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginWrapper.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginWrapper.kt index 9d28cbb6ba7..7ad309ccada 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginWrapper.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginWrapper.kt @@ -20,6 +20,7 @@ import org.gradle.api.GradleException import org.gradle.api.NamedDomainObjectFactory import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.artifacts.ExternalDependency import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry @@ -102,10 +103,17 @@ abstract class DefaultKotlinBasePlugin : KotlinBasePlugin { project .configurations .maybeCreateResolvable(BUILD_TOOLS_API_CLASSPATH_CONFIGURATION_NAME) - .defaultDependencies { - it.add( - project.dependencies.create("$KOTLIN_MODULE_GROUP:$KOTLIN_BUILD_TOOLS_API_IMPL:${project.getKotlinPluginVersion()}") - ) + .also { + project.dependencies.add(it.name, "$KOTLIN_MODULE_GROUP:$KOTLIN_BUILD_TOOLS_API_IMPL") + it.withDependencies { dependencies -> + dependencies + .withType() + .configureEach { dependency -> + dependency.version { versionConstraint -> + versionConstraint.strictly(project.kotlinExtension.compilerVersion.get()) + } + } + } } project .tasks