diff --git a/buildSrc/src/main/kotlin/GradleCommon.kt b/buildSrc/src/main/kotlin/GradleCommon.kt index de9e8d6e76b..26da844bac9 100644 --- a/buildSrc/src/main/kotlin/GradleCommon.kt +++ b/buildSrc/src/main/kotlin/GradleCommon.kt @@ -8,8 +8,7 @@ import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.ModuleDependency import org.gradle.api.artifacts.type.ArtifactTypeDefinition -import org.gradle.api.attributes.Attribute -import org.gradle.api.attributes.LibraryElements +import org.gradle.api.attributes.* import org.gradle.api.attributes.java.TargetJvmEnvironment import org.gradle.api.attributes.java.TargetJvmVersion import org.gradle.api.attributes.plugin.GradlePluginApiVersion @@ -319,9 +318,8 @@ fun Project.reconfigureMainSourcesSetForGradlePlugin( } } - val javaComponent = project.components["java"] as AdhocComponentWithVariants - // Workaround for https://youtrack.jetbrains.com/issue/KT-52987 + val javaComponent = project.components["java"] as AdhocComponentWithVariants listOf( runtimeElementsConfigurationName, apiElementsConfigurationName @@ -354,7 +352,7 @@ fun Project.reconfigureMainSourcesSetForGradlePlugin( } plugins.withType { - tasks.named(compileJavaTaskName) { + tasks.named(compileJavaTaskName).get().apply { attribute( TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, targetCompatibility.toInt() @@ -363,12 +361,28 @@ fun Project.reconfigureMainSourcesSetForGradlePlugin( } } + val expectedAttributes = setOf( + Category.CATEGORY_ATTRIBUTE, + Bundling.BUNDLING_ATTRIBUTE, + Usage.USAGE_ATTRIBUTE, + LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, + TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, + TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE + ) + if (attributes.keySet() != expectedAttributes) { + error("Wrong set of attributes:\n" + + " Expected: ${expectedAttributes.joinToString(", ")}\n" + + " Actual: ${attributes.keySet().joinToString(", ") { "${it.name}=${attributes.getAttribute(it)}" }}") + } + javaComponent.addVariantsFromConfiguration(this) { - if (originalConfiguration.name.startsWith("api")) { - mapToMavenScope("compile") - } else { - mapToMavenScope("runtime") - } + mapToMavenScope( + when (originalConfiguration.name) { + runtimeElementsConfigurationName -> "runtime" + apiElementsConfigurationName -> "compile" + else -> error("Unsupported configuration name") + } + ) } // Make original configuration unpublishable and not visible