[Gradle] Register sourcesJar during KotlinSoftwareComponent init

sourcesJar task should be available as soon as kotlin multiplatform
plugin is applied before actual evaluation.

Although copySpec of sourcesJar task will be updated in afterEvaluate
block since user should define themselves targets and intermediate
source sets between them.

^KT-55230
This commit is contained in:
Anton Lakotka
2022-12-05 14:23:16 +01:00
committed by Space Team
parent 290e50eebf
commit 6614714c94
@@ -16,6 +16,8 @@ import org.gradle.api.internal.component.SoftwareComponentInternal
import org.gradle.api.internal.component.UsageContext
import org.gradle.api.provider.SetProperty
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.TaskProvider
import org.gradle.jvm.tasks.Jar
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.MAIN_COMPILATION_NAME
@@ -79,10 +81,11 @@ abstract class KotlinSoftwareComponent(
}
}
configureSourcesJarArtifact()
val sourcesElements = metadataTarget.sourcesElementsConfigurationName
addSourcesJarArtifactToConfiguration(sourcesElements)
this += DefaultKotlinUsageContext(
compilation = metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
dependencyConfigurationName = metadataTarget.sourcesElementsConfigurationName,
dependencyConfigurationName = sourcesElements,
includeIntoProjectStructureMetadata = false,
)
}
@@ -92,11 +95,14 @@ abstract class KotlinSoftwareComponent(
return _usages
}
private fun configureSourcesJarArtifact(): PublishArtifact {
fun allPublishableCommonSourceSets() = getCommonSourceSetsForMetadataCompilation(project) +
getHostSpecificMainSharedSourceSets(project)
private fun allPublishableCommonSourceSets() = getCommonSourceSetsForMetadataCompilation(project) +
getHostSpecificMainSharedSourceSets(project)
val sourcesJarTask = sourcesJarTaskNamed(
/**
* Registration (during object init) of [sourcesJarTask] is required for cases when
* user build scripts want to have access to sourcesJar task to configure it
*/
private val sourcesJarTask: TaskProvider<Jar> = sourcesJarTaskNamed(
"sourcesJar",
name,
project,
@@ -104,7 +110,8 @@ abstract class KotlinSoftwareComponent(
name.toLowerCase()
)
return project.artifacts.add(metadataTarget.sourcesElementsConfigurationName, sourcesJarTask) { sourcesJarArtifact ->
private fun addSourcesJarArtifactToConfiguration(configurationName: String): PublishArtifact {
return project.artifacts.add(configurationName, sourcesJarTask) { sourcesJarArtifact ->
sourcesJarArtifact.classifier = "sources"
}
}