Skip to content

Commit

Permalink
Make pom.xml depend on the JVM artifact (#7354)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
swankjesse committed Jun 27, 2022
1 parent 756cdf1 commit cd707a4
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 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
Expand Down Expand Up @@ -209,6 +211,7 @@ subprojects {
}

plugins.withId("com.vanniktech.maven.publish.base") {
val publishingExtension = extensions.getByType(PublishingExtension::class.java)
configure<MavenPublishBaseExtension> {
publishToMavenCentral(SonatypeHost.S01)
signAllPublications()
Expand All @@ -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<MavenPublication>()
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")
}
}
}
}
}

Expand Down

0 comments on commit cd707a4

Please sign in to comment.