[Gradle] Add possibility to disable default multiplatform publications

Additionally, skip pom rewriting mechanism in case
if default publications were disabled to prevent unexpected poms.
This commit is contained in:
Anton Lakotka
2023-04-05 15:44:11 +02:00
committed by Space Team
parent 03e2152957
commit 11b7fe2c4a
13 changed files with 182 additions and 3 deletions
@@ -1152,6 +1152,36 @@ open class HierarchicalMppIT : KGPBaseTest() {
}
}
@GradleTest
@DisplayName("It should be possible to disable default publications for stdlib and other kotlin libraries")
fun `test disable default publications`(gradleVersion: GradleVersion, @TempDir tempDir: Path) {
project("mppCustomPublicationLayout", gradleVersion = gradleVersion, localRepoDir = tempDir) {
build(":libWithCustomLayout:publishKotlinPublicationToMavenRepository") {
listOf("jvm.jar", "linuxArm64.klib", "linuxX64.klib")
.map { tempDir.resolve("test/libWithCustomLayout/1.0/libWithCustomLayout-1.0-$it") }
.forEach { if (!it.exists()) fail("Artifact $it does not exist") }
}
build(":libWithDefaultLayout:publish") {
val pom = tempDir.resolve("test/libWithDefaultLayout-jvm/1.0/libWithDefaultLayout-jvm-1.0.pom").readText()
val expectedDependency = """
| <dependency>
| <groupId>test</groupId>
| <artifactId>libWithCustomLayout</artifactId>
| <version>1.0</version>
| <scope>compile</scope>
| </dependency>
""".trimMargin()
if (expectedDependency !in pom) {
fail("Expected to find:\n$expectedDependency\nin pom file:\n$pom")
}
}
build(":app:assemble")
}
}
private fun TestProject.testDependencyTransformations(
subproject: String? = null,
@@ -0,0 +1,24 @@
plugins {
kotlin("multiplatform")
}
repositories {
maven("<localRepo>")
}
kotlin {
targetHierarchy.default()
jvm()
linuxX64()
linuxArm64()
sourceSets {
commonMain {
dependencies {
api("test:libWithDefaultLayout:1.0")
}
}
}
}
@@ -0,0 +1,6 @@
package app
fun app() {
lib.libWithDefaultLayout()
lib.libWithCustomLayout()
}
@@ -0,0 +1,10 @@
plugins {
kotlin("multiplatform") apply false
}
allprojects {
repositories {
mavenLocal()
mavenCentral()
}
}
@@ -0,0 +1,45 @@
plugins {
kotlin("multiplatform")
`maven-publish`
}
group = "test"
version = "1.0"
kotlin {
targetHierarchy.default()
jvm()
linuxX64()
linuxArm64()
}
// Gradle magical spell to access SoftwareComponentFactory
abstract class SoftwareComponentFactoryProvider @Inject constructor(
val softwareComponentFactory: SoftwareComponentFactory
)
val softwareComponentFactoryProvider = project.objects.newInstance<SoftwareComponentFactoryProvider>()
val customComponent = softwareComponentFactoryProvider.softwareComponentFactory.adhoc("customKotlin")
afterEvaluate {
kotlin.targets.all {
val configuration = configurations.getByName(apiElementsConfigurationName)
configuration.artifacts.forEach {
it as ConfigurablePublishArtifact
it.classifier = targetName
}
customComponent.addVariantsFromConfiguration(configuration) {}
}
}
publishing {
repositories {
maven("<localRepo>")
}
publications {
create<MavenPublication>("kotlin") {
from(customComponent)
}
}
}
@@ -0,0 +1 @@
kotlin.internal.mpp.createDefaultMultiplatformPublications=false
@@ -0,0 +1,33 @@
plugins {
kotlin("multiplatform")
`maven-publish`
}
group = "test"
version = "1.0"
repositories {
maven("<localRepo>")
}
kotlin {
targetHierarchy.default()
jvm()
linuxX64()
linuxArm64()
sourceSets {
commonMain {
dependencies {
api(project(":libWithCustomLayout"))
}
}
}
}
publishing {
repositories {
maven("<localRepo>")
}
}
@@ -0,0 +1,5 @@
include(
":libWithDefaultLayout",
":libWithCustomLayout",
":app"
)
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLI
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_ABI_SNAPSHOT
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_COMPILER_KEEP_INCREMENTAL_COMPILATION_CACHES_IN_MEMORY
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_COMPILER_USE_PRECISE_COMPILATION_RESULTS_BACKUP
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_CREATE_DEFAULT_MULTIPLATFORM_PUBLICATIONS
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_JS_KARMA_BROWSERS
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_MPP_ANDROID_GRADLE_PLUGIN_COMPATIBILITY_NO_WARN
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_MPP_ANDROID_SOURCE_SET_LAYOUT_ANDROID_STYLE_NO_WARN
@@ -509,6 +510,9 @@ internal class PropertiesProvider private constructor(private val project: Proje
val suppressExperimentalICOptimizationsWarning: Boolean
get() = booleanProperty(KOTLIN_SUPPRESS_EXPERIMENTAL_IC_OPTIMIZATIONS_WARNING) ?: false
val createDefaultMultiplatformPublications: Boolean
get() = booleanProperty(KOTLIN_CREATE_DEFAULT_MULTIPLATFORM_PUBLICATIONS) ?: true
/**
* Retrieves a comma-separated list of browsers to use when running karma tests for [target]
* @see KOTLIN_JS_KARMA_BROWSERS
@@ -594,6 +598,7 @@ internal class PropertiesProvider private constructor(private val project: Proje
const val KOTLIN_COMPILER_USE_PRECISE_COMPILATION_RESULTS_BACKUP = "kotlin.compiler.preciseCompilationResultsBackup"
const val KOTLIN_COMPILER_KEEP_INCREMENTAL_COMPILATION_CACHES_IN_MEMORY = "kotlin.compiler.keepIncrementalCompilationCachesInMemory"
const val KOTLIN_SUPPRESS_EXPERIMENTAL_IC_OPTIMIZATIONS_WARNING = "kotlin.compiler.suppressExperimentalICOptimizationsWarning"
const val KOTLIN_CREATE_DEFAULT_MULTIPLATFORM_PUBLICATIONS = "kotlin.internal.mpp.createDefaultMultiplatformPublications"
}
companion object {
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.sources.KotlinDependencyScope
import org.jetbrains.kotlin.gradle.plugin.sources.sourceSetDependencyConfigurationByScope
import org.jetbrains.kotlin.gradle.plugin.whenEvaluated
@@ -28,9 +29,11 @@ import org.jetbrains.kotlin.gradle.targets.metadata.isKotlinGranularMetadataEnab
import org.jetbrains.kotlin.gradle.tooling.buildKotlinToolingMetadataTask
internal fun configurePublishingWithMavenPublish(project: Project) = project.pluginManager.withPlugin("maven-publish") {
project.extensions.configure(PublishingExtension::class.java) { publishing ->
createRootPublication(project, publishing)
createTargetPublications(project, publishing)
if (project.kotlinPropertiesProvider.createDefaultMultiplatformPublications) {
project.extensions.configure(PublishingExtension::class.java) { publishing ->
createRootPublication(project, publishing)
createTargetPublications(project, publishing)
}
}
project.components.add(project.multiplatformExtension.rootSoftwareComponent)
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilationToRunnableFiles
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetComponent
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsageContext.MavenScope
import org.jetbrains.kotlin.gradle.utils.getValue
@@ -152,6 +153,10 @@ private fun associateDependenciesWithActualModuleDependencies(
val dependencyProjectKotlinExtension = dependencyProject.multiplatformExtensionOrNull
?: return@associate noMapping
// Non-default publication layouts are not supported for pom rewriting
if (!dependencyProject.kotlinPropertiesProvider.createDefaultMultiplatformPublications)
return@associate noMapping
val resolved = resolvedDependencies[Triple(dependency.group!!, dependency.name, dependency.version!!)]
?: return@associate noMapping