Skip to content

Commit bbb65b3

Browse files
authored
Make MavenCompat compatible with configuration cache (#130)
1 parent c9acea7 commit bbb65b3

2 files changed

Lines changed: 28 additions & 49 deletions

File tree

librarian-gradle-plugin/src/main/kotlin/com/gradleup/librarian/gradle/maven.kt

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.gradleup.librarian.gradle
33
import groovy.util.Node
44
import groovy.util.NodeList
55
import org.gradle.api.Project
6-
import org.gradle.api.XmlProvider
76
import org.gradle.api.publish.PublishingExtension
87
import org.gradle.api.publish.maven.MavenPublication
98
import org.gradle.configurationcache.extensions.capitalized
@@ -12,57 +11,37 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
1211
/**
1312
* From https://github.com/arrow-kt/arrow-gradle-config/blob/main/arrow-gradle-config-publish/src/main/kotlin/internal/PublishMppRootModuleInPlatform.kt
1413
*/
15-
fun Project.publishPlatformArtifactsInRootModule() = afterEvaluate {
14+
fun Project.publishPlatformArtifactsInRootModule(group: String, version: String) = afterEvaluate {
1615
if (kotlinExtension !is KotlinMultiplatformExtension) {
1716
return@afterEvaluate
1817
}
19-
val platformPublication = extensions.getByType(PublishingExtension::class.java)
20-
.publications
21-
.findByName("jvm") as MavenPublication?
2218

23-
if (platformPublication == null) {
24-
return@afterEvaluate
25-
}
19+
val publication = extensions
20+
.findByType(PublishingExtension::class.java)
21+
?.publications
22+
?.getByName("kotlinMultiplatform")
23+
?.let { it as MavenPublication }
2624

27-
lateinit var platformXml: XmlProvider
28-
platformPublication.pom?.withXml { platformXml = it }
29-
30-
extensions
31-
.findByType(PublishingExtension::class.java)
32-
?.publications
33-
?.getByName("kotlinMultiplatform")
34-
?.let { it as MavenPublication }
35-
?.run {
36-
// replace pom
37-
pom.withXml {
38-
val xmlProvider = it
39-
val root = xmlProvider.asNode()
40-
// Remove the original content and add the content from the platform POM:
41-
root.children().toList().forEach { root.remove(it as Node) }
42-
platformXml.asNode().children().forEach { root.append(it as Node) }
43-
44-
// Adjust the self artifact ID, as it should match the root module's coordinates:
45-
((root.get("artifactId") as NodeList).get(0) as Node).setValue(artifactId)
46-
47-
// Set packaging to POM to indicate that there's no artifact:
48-
root.appendNode("packaging", "pom")
49-
50-
// Remove the original platform dependencies and add a single dependency on the platform
51-
// module:
52-
val dependencies = (root.get("dependencies") as NodeList).get(0) as Node
53-
dependencies.children().toList().forEach { dependencies.remove(it as Node) }
54-
val singleDependency = dependencies.appendNode("dependency")
55-
singleDependency.appendNode("groupId", platformPublication.groupId)
56-
singleDependency.appendNode("artifactId", platformPublication.artifactId)
57-
singleDependency.appendNode("version", platformPublication.version)
58-
singleDependency.appendNode("scope", "compile")
59-
}
60-
}
61-
62-
tasks
63-
.matching { it.name == "generatePomFileForKotlinMultiplatformPublication" }
64-
.configureEach {
65-
it.dependsOn("generatePomFileFor${platformPublication.name.capitalized()}Publication")
66-
}
25+
if (publication == null) {
26+
return@afterEvaluate
27+
}
28+
// Redirect to the JVM publication
29+
val artifactId = publication.artifactId + "-jvm"
30+
31+
publication.pom.withXml { xmlProvider ->
32+
val root = xmlProvider.asNode()
33+
// Set packaging to POM to indicate that there's no artifact:
34+
root.appendNode("packaging", "pom")
35+
36+
// Remove the original platform dependencies and add a single dependency on the platform
37+
// module:
38+
val dependencies = (root.get("dependencies") as NodeList).get(0) as Node
39+
dependencies.children().toList().forEach { dependencies.remove(it as Node) }
40+
val singleDependency = dependencies.appendNode("dependency")
41+
singleDependency.appendNode("groupId", group)
42+
singleDependency.appendNode("artifactId", artifactId)
43+
singleDependency.appendNode("version", version)
44+
singleDependency.appendNode("scope", "compile")
45+
}
6746
}
6847

librarian-gradle-plugin/src/main/kotlin/com/gradleup/librarian/gradle/publishing.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fun Project.configurePublishing(
108108
}
109109
configurePom(pomMetadata)
110110
if (publishPlatformArtifactsInRootModule) {
111-
publishPlatformArtifactsInRootModule()
111+
publishPlatformArtifactsInRootModule(group, version)
112112
}
113113

114114
pluginManager.apply("com.gradleup.nmcp")

0 commit comments

Comments
 (0)