[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:
+9
-15
@@ -9,9 +9,7 @@ import org.gradle.api.DomainObjectSet
|
|||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.api.artifacts.ConfigurablePublishArtifact
|
import org.gradle.api.artifacts.ConfigurablePublishArtifact
|
||||||
import org.gradle.api.artifacts.PublishArtifact
|
import org.gradle.api.artifacts.PublishArtifact
|
||||||
import org.gradle.api.attributes.Attribute
|
import org.gradle.api.attributes.*
|
||||||
import org.gradle.api.attributes.AttributeContainer
|
|
||||||
import org.gradle.api.attributes.Usage.JAVA_RUNTIME_JARS
|
|
||||||
import org.gradle.api.component.ComponentWithCoordinates
|
import org.gradle.api.component.ComponentWithCoordinates
|
||||||
import org.gradle.api.component.ComponentWithVariants
|
import org.gradle.api.component.ComponentWithVariants
|
||||||
import org.gradle.api.component.SoftwareComponent
|
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.KotlinMultiplatformExtension
|
||||||
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
|
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
|
||||||
import org.jetbrains.kotlin.gradle.plugin.*
|
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.dashSeparatedName
|
||||||
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
|
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
|
||||||
|
|
||||||
@@ -106,16 +105,13 @@ abstract class AbstractKotlinTarget(
|
|||||||
internal open fun createUsageContexts(
|
internal open fun createUsageContexts(
|
||||||
producingCompilation: KotlinCompilation<*>
|
producingCompilation: KotlinCompilation<*>
|
||||||
): Set<DefaultKotlinUsageContext> {
|
): 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(
|
return listOfNotNull(
|
||||||
javaApiUsageForMavenScoping() to apiElementsConfigurationName,
|
COMPILE to apiElementsConfigurationName,
|
||||||
(JAVA_RUNTIME_JARS to runtimeElementsConfigurationName).takeIf { producingCompilation is KotlinCompilationToRunnableFiles }
|
(RUNTIME to runtimeElementsConfigurationName).takeIf { producingCompilation is KotlinCompilationToRunnableFiles }
|
||||||
).mapTo(mutableSetOf()) { (usageName, dependenciesConfigurationName) ->
|
).mapTo(mutableSetOf()) { (mavenScope, dependenciesConfigurationName) ->
|
||||||
DefaultKotlinUsageContext(
|
DefaultKotlinUsageContext(
|
||||||
producingCompilation,
|
producingCompilation,
|
||||||
project.usageByName(usageName),
|
mavenScope,
|
||||||
dependenciesConfigurationName
|
dependenciesConfigurationName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -202,12 +198,10 @@ internal fun Project.buildAdhocComponentsFromKotlinVariants(kotlinVariants: Set<
|
|||||||
}
|
}
|
||||||
|
|
||||||
adhocVariant.addVariantsFromConfiguration(configuration) { configurationVariantDetails ->
|
adhocVariant.addVariantsFromConfiguration(configuration) { configurationVariantDetails ->
|
||||||
val mavenScope = when (kotlinUsageContext.usage.name) {
|
val mavenScope = kotlinUsageContext.mavenDependenciesScope
|
||||||
"java-api-jars" -> "compile"
|
if (mavenScope != null) {
|
||||||
"java-runtime-jars" -> "runtime"
|
configurationVariantDetails.mapToMavenScope(mavenScope)
|
||||||
else -> error("unexpected usage value '${kotlinUsageContext.usage.name}'")
|
|
||||||
}
|
}
|
||||||
configurationVariantDetails.mapToMavenScope(mavenScope)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-9
@@ -55,9 +55,6 @@ abstract class KotlinSoftwareComponent(
|
|||||||
}
|
}
|
||||||
|
|
||||||
mutableSetOf<UsageContext>().apply {
|
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 allMetadataJar = project.tasks.named(KotlinMetadataTargetConfigurator.ALL_METADATA_JAR_NAME)
|
||||||
val allMetadataArtifact = project.artifacts.add(Dependency.ARCHIVES_CONFIGURATION, allMetadataJar) { allMetadataArtifact ->
|
val allMetadataArtifact = project.artifacts.add(Dependency.ARCHIVES_CONFIGURATION, allMetadataJar) { allMetadataArtifact ->
|
||||||
allMetadataArtifact.classifier = if (project.isCompatibilityMetadataVariantEnabled) "all" else ""
|
allMetadataArtifact.classifier = if (project.isCompatibilityMetadataVariantEnabled) "all" else ""
|
||||||
@@ -65,7 +62,7 @@ abstract class KotlinSoftwareComponent(
|
|||||||
|
|
||||||
this += DefaultKotlinUsageContext(
|
this += DefaultKotlinUsageContext(
|
||||||
compilation = metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
|
compilation = metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
|
||||||
usage = javaApiUsage,
|
usageScope = KotlinUsageContext.UsageScope.COMPILE,
|
||||||
dependencyConfigurationName = metadataTarget.apiElementsConfigurationName,
|
dependencyConfigurationName = metadataTarget.apiElementsConfigurationName,
|
||||||
overrideConfigurationArtifacts = project.setProperty { listOf(allMetadataArtifact) }
|
overrideConfigurationArtifacts = project.setProperty { listOf(allMetadataArtifact) }
|
||||||
)
|
)
|
||||||
@@ -77,7 +74,7 @@ abstract class KotlinSoftwareComponent(
|
|||||||
this += run {
|
this += run {
|
||||||
DefaultKotlinUsageContext(
|
DefaultKotlinUsageContext(
|
||||||
metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
|
metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
|
||||||
javaApiUsage,
|
KotlinUsageContext.UsageScope.COMPILE,
|
||||||
/** this configuration is created by [KotlinMetadataTargetConfigurator.createCommonMainElementsConfiguration] */
|
/** this configuration is created by [KotlinMetadataTargetConfigurator.createCommonMainElementsConfiguration] */
|
||||||
COMMON_MAIN_ELEMENTS_CONFIGURATION_NAME
|
COMMON_MAIN_ELEMENTS_CONFIGURATION_NAME
|
||||||
)
|
)
|
||||||
@@ -124,21 +121,39 @@ interface KotlinUsageContext : UsageContext {
|
|||||||
val compilation: KotlinCompilation<*>
|
val compilation: KotlinCompilation<*>
|
||||||
val dependencyConfigurationName: String
|
val dependencyConfigurationName: String
|
||||||
val includeIntoProjectStructureMetadata: Boolean
|
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(
|
class DefaultKotlinUsageContext(
|
||||||
override val compilation: KotlinCompilation<*>,
|
override val compilation: KotlinCompilation<*>,
|
||||||
private val usage: Usage,
|
override val usageScope: KotlinUsageContext.UsageScope? = null,
|
||||||
override val dependencyConfigurationName: String,
|
override val dependencyConfigurationName: String,
|
||||||
internal val overrideConfigurationArtifacts: SetProperty<PublishArtifact>? = null,
|
internal val overrideConfigurationArtifacts: SetProperty<PublishArtifact>? = null,
|
||||||
internal val overrideConfigurationAttributes: AttributeContainer? = null,
|
internal val overrideConfigurationAttributes: AttributeContainer? = null,
|
||||||
override val includeIntoProjectStructureMetadata: Boolean = true
|
override val includeIntoProjectStructureMetadata: Boolean = true,
|
||||||
|
override val includeDependenciesToMavenPublication: Boolean = true,
|
||||||
) : KotlinUsageContext {
|
) : KotlinUsageContext {
|
||||||
|
|
||||||
private val kotlinTarget: KotlinTarget get() = compilation.target
|
private val kotlinTarget: KotlinTarget get() = compilation.target
|
||||||
private val project: Project get() = kotlinTarget.project
|
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
|
override fun getName(): String = dependencyConfigurationName
|
||||||
|
|
||||||
|
|||||||
+10
-9
@@ -19,6 +19,7 @@ import org.gradle.api.provider.Provider
|
|||||||
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
|
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
|
||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilationToRunnableFiles
|
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilationToRunnableFiles
|
||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetComponent
|
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetComponent
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsageContext.UsageScope
|
||||||
import org.jetbrains.kotlin.gradle.utils.getValue
|
import org.jetbrains.kotlin.gradle.utils.getValue
|
||||||
|
|
||||||
internal data class ModuleCoordinates(
|
internal data class ModuleCoordinates(
|
||||||
@@ -37,7 +38,7 @@ internal class PomDependenciesRewriter(
|
|||||||
// Get the dependencies mapping according to the component's UsageContexts:
|
// Get the dependencies mapping according to the component's UsageContexts:
|
||||||
private val dependenciesMappingForEachUsageContext by project.provider {
|
private val dependenciesMappingForEachUsageContext by project.provider {
|
||||||
(component as SoftwareComponentInternal).usages.mapNotNull { usage ->
|
(component as SoftwareComponentInternal).usages.mapNotNull { usage ->
|
||||||
if (usage is KotlinUsageContext)
|
if (usage is KotlinUsageContext && usage.includeDependenciesToMavenPublication)
|
||||||
associateDependenciesWithActualModuleDependencies(usage)
|
associateDependenciesWithActualModuleDependencies(usage)
|
||||||
// We are only interested in dependencies that are mapped to some other dependencies:
|
// 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) }
|
.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 -> {
|
is KotlinJvmAndroidCompilation -> {
|
||||||
// TODO handle Android configuration names in a general way once we drop AGP < 3.0.0
|
// TODO handle Android configuration names in a general way once we drop AGP < 3.0.0
|
||||||
val variantName = compilation.name
|
val variantName = compilation.name
|
||||||
when (usageContext.usage.name) {
|
when (usageContext.usageScope) {
|
||||||
Usage.JAVA_API, "java-api-jars" -> variantName + "CompileClasspath"
|
UsageScope.COMPILE -> variantName + "CompileClasspath"
|
||||||
Usage.JAVA_RUNTIME_JARS -> variantName + "RuntimeClasspath"
|
UsageScope.RUNTIME -> variantName + "RuntimeClasspath"
|
||||||
else -> error("Unexpected Usage for usage context: ${usageContext.usage}")
|
null -> error("Usage for usage context is undefined")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> when (usageContext.usage.name) {
|
else -> when (usageContext.usageScope) {
|
||||||
Usage.JAVA_API, "java-api-jars" -> compilation.compileDependencyConfigurationName
|
UsageScope.COMPILE -> compilation.compileDependencyConfigurationName
|
||||||
Usage.JAVA_RUNTIME_JARS -> (compilation as KotlinCompilationToRunnableFiles).runtimeDependencyConfigurationName
|
UsageScope.RUNTIME -> (compilation as KotlinCompilationToRunnableFiles).runtimeDependencyConfigurationName
|
||||||
else -> error("Unexpected Usage for usage context: ${usageContext.usage}")
|
null -> error("Usage for usage context is undefined")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
+4
-7
@@ -193,17 +193,14 @@ abstract class KotlinAndroidTarget @Inject constructor(
|
|||||||
val apiElementsConfigurationName = lowerCamelCaseName(variantName, "apiElements")
|
val apiElementsConfigurationName = lowerCamelCaseName(variantName, "apiElements")
|
||||||
val runtimeElementsConfigurationName = lowerCamelCaseName(variantName, "runtimeElements")
|
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(
|
return listOf(
|
||||||
apiElementsConfigurationName to javaApiUsageForMavenScoping(),
|
apiElementsConfigurationName to KotlinUsageContext.UsageScope.COMPILE,
|
||||||
runtimeElementsConfigurationName to JAVA_RUNTIME_JARS
|
runtimeElementsConfigurationName to KotlinUsageContext.UsageScope.RUNTIME
|
||||||
).mapTo(mutableSetOf()) { (dependencyConfigurationName, usageName) ->
|
).mapTo(mutableSetOf()) { (dependencyConfigurationName, mavenScope) ->
|
||||||
val configuration = project.configurations.getByName(dependencyConfigurationName)
|
val configuration = project.configurations.getByName(dependencyConfigurationName)
|
||||||
DefaultKotlinUsageContext(
|
DefaultKotlinUsageContext(
|
||||||
compilation,
|
compilation,
|
||||||
project.usageByName(usageName),
|
mavenScope,
|
||||||
dependencyConfigurationName,
|
dependencyConfigurationName,
|
||||||
overrideConfigurationArtifacts = project.setProperty { listOf(artifact) },
|
overrideConfigurationArtifacts = project.setProperty { listOf(artifact) },
|
||||||
overrideConfigurationAttributes = HierarchyAttributeContainer(configuration.attributes) {
|
overrideConfigurationAttributes = HierarchyAttributeContainer(configuration.attributes) {
|
||||||
|
|||||||
+1
-1
@@ -97,7 +97,7 @@ constructor(
|
|||||||
return usageContexts +
|
return usageContexts +
|
||||||
DefaultKotlinUsageContext(
|
DefaultKotlinUsageContext(
|
||||||
compilation = compilations.getByName(MAIN_COMPILATION_NAME),
|
compilation = compilations.getByName(MAIN_COMPILATION_NAME),
|
||||||
usage = project.usageByName("java-api-jars"),
|
usageScope = KotlinUsageContext.UsageScope.COMPILE,
|
||||||
dependencyConfigurationName = commonFakeApiElementsConfigurationName,
|
dependencyConfigurationName = commonFakeApiElementsConfigurationName,
|
||||||
overrideConfigurationArtifacts = project.setProperty { emptyList() }
|
overrideConfigurationArtifacts = project.setProperty { emptyList() }
|
||||||
)
|
)
|
||||||
|
|||||||
+2
-1
@@ -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.KotlinCompilation.Companion.MAIN_COMPILATION_NAME
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.DefaultKotlinUsageContext
|
import org.jetbrains.kotlin.gradle.plugin.mpp.DefaultKotlinUsageContext
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinTargetWithBinaries
|
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.JsAggregatingExecutionSource
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsReportAggregatingTestRun
|
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsReportAggregatingTestRun
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
|
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
|
||||||
@@ -69,7 +70,7 @@ constructor(
|
|||||||
return usageContexts +
|
return usageContexts +
|
||||||
DefaultKotlinUsageContext(
|
DefaultKotlinUsageContext(
|
||||||
compilation = compilations.getByName(MAIN_COMPILATION_NAME),
|
compilation = compilations.getByName(MAIN_COMPILATION_NAME),
|
||||||
usage = project.usageByName("java-api-jars"),
|
usageScope = KotlinUsageContext.UsageScope.COMPILE,
|
||||||
dependencyConfigurationName = commonFakeApiElementsConfigurationName,
|
dependencyConfigurationName = commonFakeApiElementsConfigurationName,
|
||||||
overrideConfigurationArtifacts = project.setProperty { emptyList() }
|
overrideConfigurationArtifacts = project.setProperty { emptyList() }
|
||||||
)
|
)
|
||||||
|
|||||||
+1
-1
@@ -93,7 +93,7 @@ abstract class KotlinNativeTarget @Inject constructor(
|
|||||||
mutableUsageContexts.add(
|
mutableUsageContexts.add(
|
||||||
DefaultKotlinUsageContext(
|
DefaultKotlinUsageContext(
|
||||||
mainCompilation,
|
mainCompilation,
|
||||||
project.usageByName(javaApiUsageForMavenScoping()),
|
KotlinUsageContext.UsageScope.COMPILE,
|
||||||
metadataConfiguration.name,
|
metadataConfiguration.name,
|
||||||
includeIntoProjectStructureMetadata = false
|
includeIntoProjectStructureMetadata = false
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user