[Gradle] Don't publish sources when withSourcesJar(publish = false)
^KT-55881 In Progress
This commit is contained in:
committed by
Space Team
parent
1b3b83755e
commit
4329b66c60
+1
-1
@@ -66,7 +66,7 @@ abstract class KotlinMultiplatformExtension(project: Project) :
|
||||
fun metadata(configure: Action<KotlinOnlyTarget<KotlinMetadataCompilation<*>>>) = metadata { configure.execute(this) }
|
||||
|
||||
fun withSourcesJar(publish: Boolean = true) {
|
||||
targets.all { withSourcesJar(publish) }
|
||||
targets.all { it.withSourcesJar(publish) }
|
||||
}
|
||||
|
||||
fun <T : KotlinTarget> targetFromPreset(
|
||||
|
||||
+23
-17
@@ -8,7 +8,6 @@ import org.gradle.api.Action
|
||||
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.*
|
||||
import org.gradle.api.component.ComponentWithCoordinates
|
||||
import org.gradle.api.component.ComponentWithVariants
|
||||
@@ -28,6 +27,7 @@ import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
|
||||
import org.jetbrains.kotlin.tooling.core.MutableExtras
|
||||
import org.jetbrains.kotlin.tooling.core.mutableExtrasOf
|
||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
|
||||
internal const val PRIMARY_SINGLE_COMPONENT_NAME = "kotlin"
|
||||
|
||||
@@ -67,9 +67,9 @@ abstract class AbstractKotlinTarget(
|
||||
override val publishable: Boolean
|
||||
get() = true
|
||||
|
||||
internal var publishSources: Boolean = true
|
||||
internal var publishableSources: Boolean = true
|
||||
override fun withSourcesJar(publish: Boolean) {
|
||||
publishSources = publish
|
||||
publishableSources = publish
|
||||
}
|
||||
|
||||
@InternalKotlinGradlePluginApi
|
||||
@@ -82,18 +82,13 @@ abstract class AbstractKotlinTarget(
|
||||
targetName
|
||||
else PRIMARY_SINGLE_COMPONENT_NAME
|
||||
|
||||
val sourcesArtifact = configureSourcesJarArtifact(
|
||||
mainCompilation,
|
||||
componentName,
|
||||
dashSeparatedName(targetName.toLowerCaseAsciiOnly())
|
||||
)
|
||||
if (sourcesArtifact != null) {
|
||||
usageContexts += DefaultKotlinUsageContext(
|
||||
compilation = mainCompilation,
|
||||
dependencyConfigurationName = sourcesElementsConfigurationName,
|
||||
includeIntoProjectStructureMetadata = false,
|
||||
usageContexts.addIfNotNull(
|
||||
createSourcesJarAndUsageContextIfPublishable(
|
||||
producingCompilation = mainCompilation,
|
||||
componentName = componentName,
|
||||
artifactNameAppendix = dashSeparatedName(targetName.toLowerCaseAsciiOnly())
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
val result = createKotlinVariant(componentName, mainCompilation, usageContexts)
|
||||
|
||||
@@ -140,14 +135,18 @@ abstract class AbstractKotlinTarget(
|
||||
}
|
||||
}
|
||||
|
||||
protected fun configureSourcesJarArtifact(
|
||||
protected fun createSourcesJarAndUsageContextIfPublishable(
|
||||
producingCompilation: KotlinCompilation<*>,
|
||||
componentName: String,
|
||||
artifactNameAppendix: String,
|
||||
classifierPrefix: String? = null,
|
||||
sourcesElementsConfigurationName: String = this.sourcesElementsConfigurationName,
|
||||
): PublishArtifact? {
|
||||
overrideConfigurationAttributes: AttributeContainer? = null,
|
||||
): DefaultKotlinUsageContext? {
|
||||
// We want to create task anyway, even if sources are not going to be published by KGP
|
||||
// So users or other plugins can still use it
|
||||
val sourcesJarTask = sourcesJarTask(producingCompilation, componentName, artifactNameAppendix)
|
||||
if (!publishableSources) return null
|
||||
|
||||
// If sourcesElements configuration not found, don't create artifact.
|
||||
// This can happen in pure JVM plugin where source publication is delegated to Java Gradle Plugin.
|
||||
@@ -156,7 +155,14 @@ abstract class AbstractKotlinTarget(
|
||||
|
||||
val artifact = project.artifacts.add(sourcesElementsConfigurationName, sourcesJarTask) as ConfigurablePublishArtifact
|
||||
artifact.classifier = dashSeparatedName(classifierPrefix, "sources")
|
||||
return artifact
|
||||
|
||||
return DefaultKotlinUsageContext(
|
||||
compilation = producingCompilation,
|
||||
dependencyConfigurationName = sourcesElementsConfigurationName,
|
||||
overrideConfigurationAttributes = overrideConfigurationAttributes,
|
||||
includeIntoProjectStructureMetadata = false,
|
||||
publishOnlyIf = { publishableSources }
|
||||
)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
|
||||
+22
-10
@@ -50,13 +50,13 @@ abstract class KotlinSoftwareComponent(
|
||||
target.components.filter { it.name in targetPublishableComponentNames }
|
||||
}.toSet()
|
||||
|
||||
private val _usages: Set<UsageContext> by lazy {
|
||||
private val _usages: Set<DefaultKotlinUsageContext> by lazy {
|
||||
if (!project.isKotlinGranularMetadataEnabled) {
|
||||
val metadataCompilation = metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME)
|
||||
return@lazy metadataTarget.createUsageContexts(metadataCompilation)
|
||||
}
|
||||
|
||||
mutableSetOf<UsageContext>().apply {
|
||||
mutableSetOf<DefaultKotlinUsageContext>().apply {
|
||||
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 ""
|
||||
@@ -84,17 +84,20 @@ abstract class KotlinSoftwareComponent(
|
||||
}
|
||||
|
||||
val sourcesElements = metadataTarget.sourcesElementsConfigurationName
|
||||
addSourcesJarArtifactToConfiguration(sourcesElements)
|
||||
this += DefaultKotlinUsageContext(
|
||||
compilation = metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
|
||||
dependencyConfigurationName = sourcesElements,
|
||||
includeIntoProjectStructureMetadata = false,
|
||||
)
|
||||
if (metadataTarget.publishableSources) {
|
||||
addSourcesJarArtifactToConfiguration(sourcesElements)
|
||||
this += DefaultKotlinUsageContext(
|
||||
compilation = metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
|
||||
dependencyConfigurationName = sourcesElements,
|
||||
includeIntoProjectStructureMetadata = false,
|
||||
publishOnlyIf = { metadataTarget.publishableSources }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getUsages(): Set<UsageContext> {
|
||||
return _usages
|
||||
return _usages.publishableUsages()
|
||||
}
|
||||
|
||||
private fun allPublishableCommonSourceSets() = getCommonSourceSetsForMetadataCompilation(project) +
|
||||
@@ -149,7 +152,12 @@ class DefaultKotlinUsageContext(
|
||||
internal val overrideConfigurationArtifacts: SetProperty<PublishArtifact>? = null,
|
||||
internal val overrideConfigurationAttributes: AttributeContainer? = null,
|
||||
override val includeIntoProjectStructureMetadata: Boolean = true,
|
||||
internal val publishOnlyIf: PublishOnlyIf = PublishOnlyIf { true },
|
||||
) : KotlinUsageContext {
|
||||
fun interface PublishOnlyIf {
|
||||
fun predicate(): Boolean
|
||||
}
|
||||
|
||||
private val kotlinTarget: KotlinTarget get() = compilation.target
|
||||
private val project: Project get() = kotlinTarget.project
|
||||
|
||||
@@ -219,4 +227,8 @@ class DefaultKotlinUsageContext(
|
||||
*/
|
||||
it.name != "org.gradle.jvm.environment"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Iterable<DefaultKotlinUsageContext>.publishableUsages() = this
|
||||
.filter { it.publishOnlyIf.predicate() }
|
||||
.toSet()
|
||||
|
||||
+1
@@ -72,6 +72,7 @@ private fun createTargetPublications(project: Project, publishing: PublishingExt
|
||||
project.whenEvaluated { kotlinTarget.createMavenPublications(publishing.publications) }
|
||||
else
|
||||
kotlinTarget.createMavenPublications(publishing.publications)
|
||||
// project.whenEvaluated { kotlinTarget.createMavenPublications(publishing.publications) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -67,7 +67,7 @@ open class KotlinVariant(
|
||||
final override val target: KotlinTarget
|
||||
get() = producingCompilation.target
|
||||
|
||||
override fun getUsages(): Set<KotlinUsageContext> = usages
|
||||
override fun getUsages(): Set<KotlinUsageContext> = usages.publishableUsages()
|
||||
|
||||
override fun getName(): String = componentName ?: producingCompilation.target.targetName
|
||||
|
||||
|
||||
+17
-18
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.gradle.utils.forAllAndroidVariants
|
||||
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
|
||||
import org.jetbrains.kotlin.gradle.utils.setProperty
|
||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class KotlinAndroidTarget @Inject constructor(
|
||||
@@ -188,17 +189,6 @@ abstract class KotlinAndroidTarget @Inject constructor(
|
||||
apiElementsConfigurationName,
|
||||
sourcesElementsConfigurationName
|
||||
)
|
||||
configureSourcesJarArtifact(
|
||||
compilation,
|
||||
compilation.disambiguateName(""),
|
||||
dashSeparatedName(
|
||||
compilation.target.name.toLowerCaseAsciiOnly(),
|
||||
*flavorNames.map { it.toLowerCaseAsciiOnly() }.toTypedArray(),
|
||||
buildTypeName.takeIf { it != "release" }?.toLowerCaseAsciiOnly()
|
||||
),
|
||||
classifierPrefix = artifactClassifier,
|
||||
sourcesElementsConfigurationName = sourcesElementsConfigurationName
|
||||
)
|
||||
|
||||
fun AttributeContainer.filterOutAndroidVariantAttributes(): AttributeContainer =
|
||||
HierarchyAttributeContainer(this) {
|
||||
@@ -211,14 +201,20 @@ abstract class KotlinAndroidTarget @Inject constructor(
|
||||
filterOutAndroidAgpVersionAttribute(it)
|
||||
}
|
||||
|
||||
val sourcesUsageContext = DefaultKotlinUsageContext(
|
||||
compilation = compilation,
|
||||
dependencyConfigurationName = sourcesElementsConfigurationName,
|
||||
overrideConfigurationAttributes = sourcesElementsConfiguration.attributes.filterOutAndroidVariantAttributes(),
|
||||
includeIntoProjectStructureMetadata = false
|
||||
val sourcesUsageContext = createSourcesJarAndUsageContextIfPublishable(
|
||||
producingCompilation = compilation,
|
||||
componentName = compilation.disambiguateName(""),
|
||||
artifactNameAppendix = dashSeparatedName(
|
||||
compilation.target.name.toLowerCaseAsciiOnly(),
|
||||
*flavorNames.map { it.toLowerCaseAsciiOnly() }.toTypedArray(),
|
||||
buildTypeName.takeIf { it != "release" }?.toLowerCaseAsciiOnly()
|
||||
),
|
||||
classifierPrefix = artifactClassifier,
|
||||
sourcesElementsConfigurationName = sourcesElementsConfigurationName,
|
||||
overrideConfigurationAttributes = sourcesElementsConfiguration.attributes.filterOutAndroidVariantAttributes()
|
||||
)
|
||||
|
||||
return listOf(
|
||||
val usageContexts = listOf(
|
||||
apiElementsConfigurationName to KotlinUsageContext.MavenScope.COMPILE,
|
||||
runtimeElementsConfigurationName to KotlinUsageContext.MavenScope.RUNTIME,
|
||||
).mapTo(mutableSetOf()) { (dependencyConfigurationName, mavenScope) ->
|
||||
@@ -230,7 +226,10 @@ abstract class KotlinAndroidTarget @Inject constructor(
|
||||
overrideConfigurationArtifacts = project.setProperty { listOf(artifact) },
|
||||
overrideConfigurationAttributes = configuration.attributes.filterOutAndroidVariantAttributes()
|
||||
)
|
||||
} + sourcesUsageContext
|
||||
}
|
||||
usageContexts.addIfNotNull(sourcesUsageContext)
|
||||
|
||||
return usageContexts
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+7
-6
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.gradle.utils.dashSeparatedName
|
||||
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
|
||||
import org.jetbrains.kotlin.gradle.utils.setProperty
|
||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class KotlinJsTarget
|
||||
@@ -81,12 +82,12 @@ constructor(
|
||||
irTarget?.let { targetName.removeJsCompilerSuffix(LEGACY) } ?: targetName
|
||||
else PRIMARY_SINGLE_COMPONENT_NAME
|
||||
|
||||
configureSourcesJarArtifact(mainCompilation, componentName, dashSeparatedName(targetName.toLowerCaseAsciiOnly()))
|
||||
usageContexts += DefaultKotlinUsageContext(
|
||||
compilation = mainCompilation,
|
||||
mavenScope = KotlinUsageContext.MavenScope.RUNTIME,
|
||||
dependencyConfigurationName = sourcesElementsConfigurationName,
|
||||
includeIntoProjectStructureMetadata = false,
|
||||
usageContexts.addIfNotNull(
|
||||
createSourcesJarAndUsageContextIfPublishable(
|
||||
mainCompilation,
|
||||
componentName,
|
||||
dashSeparatedName(targetName.toLowerCaseAsciiOnly())
|
||||
)
|
||||
)
|
||||
|
||||
val result = createKotlinVariant(componentName, mainCompilation, usageContexts)
|
||||
|
||||
+7
-6
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.gradle.utils.dashSeparatedName
|
||||
import org.jetbrains.kotlin.konan.target.HostManager
|
||||
import org.jetbrains.kotlin.konan.target.KonanTarget
|
||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class KotlinNativeTarget @Inject constructor(
|
||||
@@ -102,13 +103,13 @@ abstract class KotlinNativeTarget @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
configureSourcesJarArtifact(mainCompilation, targetName, dashSeparatedName(targetName.toLowerCaseAsciiOnly()))
|
||||
val sourcesUsage = DefaultKotlinUsageContext(
|
||||
compilation = mainCompilation,
|
||||
dependencyConfigurationName = sourcesElementsConfigurationName,
|
||||
includeIntoProjectStructureMetadata = false,
|
||||
mutableUsageContexts.addIfNotNull(
|
||||
createSourcesJarAndUsageContextIfPublishable(
|
||||
mainCompilation,
|
||||
targetName,
|
||||
dashSeparatedName(targetName.toLowerCaseAsciiOnly())
|
||||
)
|
||||
)
|
||||
mutableUsageContexts += sourcesUsage
|
||||
|
||||
val result = createKotlinVariant(targetName, mainCompilation, mutableUsageContexts)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user