From 948d4f03b4ff38bf0acaede5e83b337b67d2de7c Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Sun, 26 Jun 2022 14:15:19 -0400 Subject: [PATCH] Make pom.xml depend on the JVM artifact This should prevent Maven users from needing to change their dependencies from okhttp to okhttp-jvm. More importantly, it should also prevent them from inadvertently getting two different OkHttp JVM artifacts on their classpaths at once. --- build.gradle.kts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index b78dad738973..66dd9371df02 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,7 @@ import com.vanniktech.maven.publish.MavenPublishBaseExtension import com.vanniktech.maven.publish.SonatypeHost +import groovy.util.Node +import groovy.util.NodeList import java.net.URL import kotlinx.validation.ApiValidationExtension import org.gradle.api.tasks.testing.logging.TestExceptionFormat @@ -209,6 +211,7 @@ subprojects { } plugins.withId("com.vanniktech.maven.publish.base") { + val publishingExtension = extensions.getByType(PublishingExtension::class.java) configure { publishToMavenCentral(SonatypeHost.S01) signAllPublications() @@ -234,6 +237,31 @@ subprojects { } } } + + // Configure the kotlinMultiplatform artifact to depend on the JVM artifact in pom.xml only. + // This hack allows Maven users to continue using our original OkHttp artifact names (like + // com.squareup.okhttp3:okhttp:5.x.y) even though we changed that artifact from JVM-only + // to Kotlin Multiplatform. Note that module.json doesn't need this hack. + val mavenPublications = publishingExtension.publications.withType() + mavenPublications.configureEach { + if (name != "jvm") return@configureEach + val jvmPublication = this + val kmpPublication = mavenPublications.getByName("kotlinMultiplatform") + kmpPublication.pom.withXml { + val root = asNode() + val dependencies = (root["dependencies"] as NodeList).firstOrNull() as Node? + ?: root.appendNode("dependencies") + for (child in dependencies.children().toList()) { + dependencies.remove(child as Node) + } + dependencies.appendNode("dependency").apply { + appendNode("groupId", jvmPublication.groupId) + appendNode("artifactId", jvmPublication.artifactId) + appendNode("version", jvmPublication.version) + appendNode("scope", "compile") + } + } + } } }