[Gradle] Stop using Usage of UsageContext for maven scoping

Replace it instead with dedicated property [usageScope].

Gradle no longer supports `Usage` and KGP used it only for mapping
dependencies to appropriate maven scopes.
This commit is contained in:
Anton Lakotka
2022-11-28 10:55:11 +01:00
committed by teamcity
parent 672be7ecb5
commit 8e85e556ce
7 changed files with 51 additions and 43 deletions
@@ -9,9 +9,7 @@ import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import org.gradle.api.artifacts.ConfigurablePublishArtifact
import org.gradle.api.artifacts.PublishArtifact
import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.AttributeContainer
import org.gradle.api.attributes.Usage.JAVA_RUNTIME_JARS
import org.gradle.api.attributes.*
import org.gradle.api.component.ComponentWithCoordinates
import org.gradle.api.component.ComponentWithVariants
import org.gradle.api.component.SoftwareComponent
@@ -24,6 +22,7 @@ import org.jetbrains.kotlin.gradle.InternalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsageContext.UsageScope.*
import org.jetbrains.kotlin.gradle.utils.dashSeparatedName
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
@@ -106,16 +105,13 @@ abstract class AbstractKotlinTarget(
internal open fun createUsageContexts(
producingCompilation: KotlinCompilation<*>
): Set<DefaultKotlinUsageContext> {
// Here, the Java Usage values are used intentionally as Gradle needs this for
// ordering of the usage contexts (prioritizing the dependencies) when merging them into the POM;
// These Java usages should not be replaced with the custom Kotlin usages.
return listOfNotNull(
javaApiUsageForMavenScoping() to apiElementsConfigurationName,
(JAVA_RUNTIME_JARS to runtimeElementsConfigurationName).takeIf { producingCompilation is KotlinCompilationToRunnableFiles }
).mapTo(mutableSetOf()) { (usageName, dependenciesConfigurationName) ->
COMPILE to apiElementsConfigurationName,
(RUNTIME to runtimeElementsConfigurationName).takeIf { producingCompilation is KotlinCompilationToRunnableFiles }
).mapTo(mutableSetOf()) { (mavenScope, dependenciesConfigurationName) ->
DefaultKotlinUsageContext(
producingCompilation,
project.usageByName(usageName),
mavenScope,
dependenciesConfigurationName
)
}
@@ -202,12 +198,10 @@ internal fun Project.buildAdhocComponentsFromKotlinVariants(kotlinVariants: Set<
}
adhocVariant.addVariantsFromConfiguration(configuration) { configurationVariantDetails ->
val mavenScope = when (kotlinUsageContext.usage.name) {
"java-api-jars" -> "compile"
"java-runtime-jars" -> "runtime"
else -> error("unexpected usage value '${kotlinUsageContext.usage.name}'")
val mavenScope = kotlinUsageContext.mavenDependenciesScope
if (mavenScope != null) {
configurationVariantDetails.mapToMavenScope(mavenScope)
}
configurationVariantDetails.mapToMavenScope(mavenScope)
}
}
}
@@ -55,9 +55,6 @@ abstract class KotlinSoftwareComponent(
}
mutableSetOf<UsageContext>().apply {
// This usage value is only needed for Maven scope mapping. Don't replace it with a custom Kotlin Usage value
val javaApiUsage = project.usageByName("java-api-jars")
val allMetadataJar = project.tasks.named(KotlinMetadataTargetConfigurator.ALL_METADATA_JAR_NAME)
val allMetadataArtifact = project.artifacts.add(Dependency.ARCHIVES_CONFIGURATION, allMetadataJar) { allMetadataArtifact ->
allMetadataArtifact.classifier = if (project.isCompatibilityMetadataVariantEnabled) "all" else ""
@@ -65,7 +62,7 @@ abstract class KotlinSoftwareComponent(
this += DefaultKotlinUsageContext(
compilation = metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
usage = javaApiUsage,
usageScope = KotlinUsageContext.UsageScope.COMPILE,
dependencyConfigurationName = metadataTarget.apiElementsConfigurationName,
overrideConfigurationArtifacts = project.setProperty { listOf(allMetadataArtifact) }
)
@@ -77,7 +74,7 @@ abstract class KotlinSoftwareComponent(
this += run {
DefaultKotlinUsageContext(
metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
javaApiUsage,
KotlinUsageContext.UsageScope.COMPILE,
/** this configuration is created by [KotlinMetadataTargetConfigurator.createCommonMainElementsConfiguration] */
COMMON_MAIN_ELEMENTS_CONFIGURATION_NAME
)
@@ -124,21 +121,39 @@ interface KotlinUsageContext : UsageContext {
val compilation: KotlinCompilation<*>
val dependencyConfigurationName: String
val includeIntoProjectStructureMetadata: Boolean
val usageScope: UsageScope?
val includeDependenciesToMavenPublication: Boolean
enum class UsageScope {
COMPILE, RUNTIME;
}
}
val KotlinUsageContext.mavenDependenciesScope get() =
when (usageScope.takeIf { includeDependenciesToMavenPublication }) {
KotlinUsageContext.UsageScope.COMPILE -> "compile"
KotlinUsageContext.UsageScope.RUNTIME -> "runtime"
null -> null
}
class DefaultKotlinUsageContext(
override val compilation: KotlinCompilation<*>,
private val usage: Usage,
override val usageScope: KotlinUsageContext.UsageScope? = null,
override val dependencyConfigurationName: String,
internal val overrideConfigurationArtifacts: SetProperty<PublishArtifact>? = null,
internal val overrideConfigurationAttributes: AttributeContainer? = null,
override val includeIntoProjectStructureMetadata: Boolean = true
override val includeIntoProjectStructureMetadata: Boolean = true,
override val includeDependenciesToMavenPublication: Boolean = true,
) : KotlinUsageContext {
private val kotlinTarget: KotlinTarget get() = compilation.target
private val project: Project get() = kotlinTarget.project
override fun getUsage(): Usage = usage
@Deprecated(
message = "Usage is no longer supported. Use `usageScope`",
replaceWith = ReplaceWith("usageScope"),
level = DeprecationLevel.ERROR
)
override fun getUsage(): Usage = error("Usage is no longer supported. Use `usageScope`")
override fun getName(): String = dependencyConfigurationName
@@ -19,6 +19,7 @@ import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilationToRunnableFiles
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetComponent
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsageContext.UsageScope
import org.jetbrains.kotlin.gradle.utils.getValue
internal data class ModuleCoordinates(
@@ -37,7 +38,7 @@ internal class PomDependenciesRewriter(
// Get the dependencies mapping according to the component's UsageContexts:
private val dependenciesMappingForEachUsageContext by project.provider {
(component as SoftwareComponentInternal).usages.mapNotNull { usage ->
if (usage is KotlinUsageContext)
if (usage is KotlinUsageContext && usage.includeDependenciesToMavenPublication)
associateDependenciesWithActualModuleDependencies(usage)
// We are only interested in dependencies that are mapped to some other dependencies:
.filter { (from, to) -> Triple(from.group, from.name, from.version) != Triple(to.group, to.name, to.version) }
@@ -120,16 +121,16 @@ private fun associateDependenciesWithActualModuleDependencies(
is KotlinJvmAndroidCompilation -> {
// TODO handle Android configuration names in a general way once we drop AGP < 3.0.0
val variantName = compilation.name
when (usageContext.usage.name) {
Usage.JAVA_API, "java-api-jars" -> variantName + "CompileClasspath"
Usage.JAVA_RUNTIME_JARS -> variantName + "RuntimeClasspath"
else -> error("Unexpected Usage for usage context: ${usageContext.usage}")
when (usageContext.usageScope) {
UsageScope.COMPILE -> variantName + "CompileClasspath"
UsageScope.RUNTIME -> variantName + "RuntimeClasspath"
null -> error("Usage for usage context is undefined")
}
}
else -> when (usageContext.usage.name) {
Usage.JAVA_API, "java-api-jars" -> compilation.compileDependencyConfigurationName
Usage.JAVA_RUNTIME_JARS -> (compilation as KotlinCompilationToRunnableFiles).runtimeDependencyConfigurationName
else -> error("Unexpected Usage for usage context: ${usageContext.usage}")
else -> when (usageContext.usageScope) {
UsageScope.COMPILE -> compilation.compileDependencyConfigurationName
UsageScope.RUNTIME -> (compilation as KotlinCompilationToRunnableFiles).runtimeDependencyConfigurationName
null -> error("Usage for usage context is undefined")
}
}
)
@@ -193,17 +193,14 @@ abstract class KotlinAndroidTarget @Inject constructor(
val apiElementsConfigurationName = lowerCamelCaseName(variantName, "apiElements")
val runtimeElementsConfigurationName = lowerCamelCaseName(variantName, "runtimeElements")
// Here, the Java Usage values are used intentionally as Gradle needs this for
// ordering of the usage contexts (prioritizing the dependencies) when merging them into the POM;
// These Java usages should not be replaced with the custom Kotlin usages.
return listOf(
apiElementsConfigurationName to javaApiUsageForMavenScoping(),
runtimeElementsConfigurationName to JAVA_RUNTIME_JARS
).mapTo(mutableSetOf()) { (dependencyConfigurationName, usageName) ->
apiElementsConfigurationName to KotlinUsageContext.UsageScope.COMPILE,
runtimeElementsConfigurationName to KotlinUsageContext.UsageScope.RUNTIME
).mapTo(mutableSetOf()) { (dependencyConfigurationName, mavenScope) ->
val configuration = project.configurations.getByName(dependencyConfigurationName)
DefaultKotlinUsageContext(
compilation,
project.usageByName(usageName),
mavenScope,
dependencyConfigurationName,
overrideConfigurationArtifacts = project.setProperty { listOf(artifact) },
overrideConfigurationAttributes = HierarchyAttributeContainer(configuration.attributes) {
@@ -97,7 +97,7 @@ constructor(
return usageContexts +
DefaultKotlinUsageContext(
compilation = compilations.getByName(MAIN_COMPILATION_NAME),
usage = project.usageByName("java-api-jars"),
usageScope = KotlinUsageContext.UsageScope.COMPILE,
dependencyConfigurationName = commonFakeApiElementsConfigurationName,
overrideConfigurationArtifacts = project.setProperty { emptyList() }
)
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.gradle.plugin.AbstractKotlinTargetConfigurator.Compa
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.MAIN_COMPILATION_NAME
import org.jetbrains.kotlin.gradle.plugin.mpp.DefaultKotlinUsageContext
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinTargetWithBinaries
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsageContext
import org.jetbrains.kotlin.gradle.targets.js.JsAggregatingExecutionSource
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsReportAggregatingTestRun
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
@@ -69,7 +70,7 @@ constructor(
return usageContexts +
DefaultKotlinUsageContext(
compilation = compilations.getByName(MAIN_COMPILATION_NAME),
usage = project.usageByName("java-api-jars"),
usageScope = KotlinUsageContext.UsageScope.COMPILE,
dependencyConfigurationName = commonFakeApiElementsConfigurationName,
overrideConfigurationArtifacts = project.setProperty { emptyList() }
)
@@ -93,7 +93,7 @@ abstract class KotlinNativeTarget @Inject constructor(
mutableUsageContexts.add(
DefaultKotlinUsageContext(
mainCompilation,
project.usageByName(javaApiUsageForMavenScoping()),
KotlinUsageContext.UsageScope.COMPILE,
metadataConfiguration.name,
includeIntoProjectStructureMetadata = false
)