Fix gradle api dependency version in variants

Common SourceSet was forcing gradle api version to be the same
in all plugin variants via 'extendsFrom(..)'.

^KT-49227 In Progress
This commit is contained in:
Yahor Berdnikau
2022-04-06 16:08:53 +02:00
committed by Space
parent 1e444e0451
commit 78cc2365b0
5 changed files with 52 additions and 31 deletions
+42 -15
View File
@@ -23,7 +23,7 @@ import org.gradle.kotlin.dsl.*
import org.gradle.plugin.devel.plugins.JavaGradlePluginPlugin import org.gradle.plugin.devel.plugins.JavaGradlePluginPlugin
import org.jetbrains.dokka.DokkaVersion import org.jetbrains.dokka.DokkaVersion
import org.jetbrains.dokka.gradle.DokkaTask import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet import org.jetbrains.kotlin.gradle.dsl.KotlinSingleTargetExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.project.model.KotlinPlatformTypeAttribute import org.jetbrains.kotlin.project.model.KotlinPlatformTypeAttribute
import plugins.configureDefaultPublishing import plugins.configureDefaultPublishing
@@ -95,9 +95,17 @@ fun Project.createGradleCommonSourceSet(): SourceSet {
val commonSourceSet = sourceSets.create("common") { val commonSourceSet = sourceSets.create("common") {
excludeGradleCommonDependencies(this) excludeGradleCommonDependencies(this)
// Adding Gradle API to separate configuration, so version will not leak into variants
val commonGradleApiConfiguration = configurations.create("commonGradleApiCompileOnly") {
isVisible = false
isCanBeConsumed = false
isCanBeResolved = true
}
configurations[compileClasspathConfigurationName].extendsFrom(commonGradleApiConfiguration)
dependencies { dependencies {
compileOnlyConfigurationName(kotlinStdlib()) compileOnlyConfigurationName(kotlinStdlib())
compileOnlyConfigurationName("dev.gradleplugins:gradle-api:7.2") "commonGradleApiCompileOnly"("dev.gradleplugins:gradle-api:7.2")
if (this@createGradleCommonSourceSet.name != "kotlin-gradle-plugin-api" && if (this@createGradleCommonSourceSet.name != "kotlin-gradle-plugin-api" &&
this@createGradleCommonSourceSet.name != "android-test-fixes" this@createGradleCommonSourceSet.name != "android-test-fixes"
) { ) {
@@ -190,11 +198,22 @@ fun Project.wireGradleVariantToCommonGradleVariant(
) { ) {
wireSourceSet.compileClasspath += commonSourceSet.output wireSourceSet.compileClasspath += commonSourceSet.output
wireSourceSet.runtimeClasspath += commonSourceSet.output wireSourceSet.runtimeClasspath += commonSourceSet.output
@Suppress("deprecation") // Needs support from KGP
wireSourceSet.withConvention(KotlinSourceSet::class) { // Allowing to use 'internal' classes/methods from common source code
val wireKotlinSourceSet = this (extensions.getByName("kotlin") as KotlinSingleTargetExtension).target.compilations.run {
commonSourceSet.withConvention(KotlinSourceSet::class) { getByName(wireSourceSet.name).associateWith(getByName(commonSourceSet.name))
wireKotlinSourceSet.dependsOn(this) }
// Common outputs will also produce '${project.name}.kotlin_module' file, so we need to avoid
// files clash
val compileTaskName = if (wireSourceSet.name == SourceSet.MAIN_SOURCE_SET_NAME) {
"compileKotlin"
} else {
"compile${wireSourceSet.name.capitalize()}Kotlin"
}
tasks.named<KotlinCompile>(compileTaskName) {
kotlinOptions {
moduleName = "${this@wireGradleVariantToCommonGradleVariant.name}_${wireSourceSet.name}"
} }
} }
@@ -251,14 +270,11 @@ fun Project.reconfigureMainSourcesSetForGradlePlugin(
excludeGradleCommonDependencies(this) excludeGradleCommonDependencies(this)
wireGradleVariantToCommonGradleVariant(this, commonSourceSet) wireGradleVariantToCommonGradleVariant(this, commonSourceSet)
tasks.withType<Jar>().configureEach { // https://youtrack.jetbrains.com/issue/KT-51913
if (name == jarTaskName) { configurations["default"].attributes.attribute(
setupPublicJar(archiveBaseName.get()) TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE,
addEmbeddedRuntime() objects.named(TargetJvmEnvironment::class, "no-op")
} else if (name == sourcesJarTaskName) { )
addEmbeddedSources()
}
}
plugins.withType<JavaLibraryPlugin>().configureEach { plugins.withType<JavaLibraryPlugin>().configureEach {
this@reconfigureMainSourcesSetForGradlePlugin this@reconfigureMainSourcesSetForGradlePlugin
@@ -295,6 +311,17 @@ fun Project.reconfigureMainSourcesSetForGradlePlugin(
} }
} }
} }
// Fix common sources visibility for tests
sourceSets.named(SourceSet.TEST_SOURCE_SET_NAME) {
compileClasspath += commonSourceSet.output
runtimeClasspath += commonSourceSet.output
}
// Allowing to use 'internal' classes/methods from common source code
(extensions.getByName("kotlin") as KotlinSingleTargetExtension).target.compilations.run {
getByName(SourceSet.TEST_SOURCE_SET_NAME).associateWith(getByName(commonSourceSet.name))
}
} }
/** /**
+7 -1
View File
@@ -71,7 +71,13 @@ fun Jar.addEmbeddedRuntime() {
project.configurations.findByName("embedded")?.let { embedded -> project.configurations.findByName("embedded")?.let { embedded ->
dependsOn(embedded) dependsOn(embedded)
from { from {
embedded.map(project::zipTree) embedded.map {
if (it.extension.equals("jar", ignoreCase = true)) {
project.zipTree(it)
} else {
it
}
}
} }
} }
} }
@@ -14,11 +14,7 @@ dependencies {
implementation(kotlinStdlib()) implementation(kotlinStdlib())
testImplementation(gradleApi()) testImplementation(gradleApi())
testImplementation(gradleKotlinDsl()) testImplementation(gradleKotlinDsl())
testImplementation(project(":kotlin-gradle-plugin")) { testImplementation(project(":kotlin-gradle-plugin"))
capabilities {
requireCapability("org.jetbrains.kotlin:kotlin-gradle-plugin-common")
}
}
testImplementation(project(":kotlin-gradle-statistics")) testImplementation(project(":kotlin-gradle-statistics"))
testImplementation(project(":kotlin-test:kotlin-test-junit")) testImplementation(project(":kotlin-test:kotlin-test-junit"))
@@ -99,11 +99,7 @@ class UpToDateIT : KGPBaseTest() {
val originalPaths get() = originalCompilerCp.map { it.replace("\\", "/") }.joinToString(", ") { "'$it'" } val originalPaths get() = originalCompilerCp.map { it.replace("\\", "/") }.joinToString(", ") { "'$it'" }
override fun initProject(project: TestProject) = with(project) { override fun initProject(project: TestProject) = with(project) {
val pluginSuffix = if (project.gradleVersion < GradleVersion.version("7.0")) { val pluginSuffix = "kotlin_gradle_plugin"
"kotlin_gradle_plugin"
} else {
"kotlin_gradle_plugin_gradle70"
}
buildGradle.appendText( buildGradle.appendText(
"\nafterEvaluate { println 'compiler_cp=' + compileKotlin.getDefaultCompilerClasspath\$$pluginSuffix().toList() }" "\nafterEvaluate { println 'compiler_cp=' + compileKotlin.getDefaultCompilerClasspath\$$pluginSuffix().toList() }"
) )
+1 -5
View File
@@ -17,11 +17,7 @@ dependencies {
testImplementation(commonDependency("junit:junit")) testImplementation(commonDependency("junit:junit"))
testImplementation(projectTests(":compiler:tests-common")) testImplementation(projectTests(":compiler:tests-common"))
testRuntimeOnly(project(":native:kotlin-klib-commonizer")) testRuntimeOnly(project(":native:kotlin-klib-commonizer"))
testImplementation(project(":kotlin-gradle-plugin")) { testImplementation(project(":kotlin-gradle-plugin"))
capabilities {
requireCapability("org.jetbrains.kotlin:kotlin-gradle-plugin-common")
}
}
testImplementation(project(":kotlin-gradle-statistics")) testImplementation(project(":kotlin-gradle-statistics"))
testImplementation(project(":kotlin-gradle-plugin-model")) testImplementation(project(":kotlin-gradle-plugin-model"))
testImplementation(gradleApi()) testImplementation(gradleApi())