You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polaris.apache.org by sn...@apache.org on 2026/06/12 06:50:04 UTC

(polaris) branch main updated: Build: make Maven publication compatible with Gradle configuration cache (#4699)

This is an automated email from the ASF dual-hosted git repository.

snazy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/polaris.git


The following commit(s) were added to refs/heads/main by this push:
     new e1ebce437b Build: make Maven publication compatible with Gradle configuration cache (#4699)
e1ebce437b is described below

commit e1ebce437b948e8a39a9a4d26a4fa41422b28568
Author: Robert Stupp <sn...@snazy.de>
AuthorDate: Fri Jun 12 08:50:01 2026 +0200

    Build: make Maven publication compatible with Gradle configuration cache (#4699)
---
 .../src/main/kotlin/publishing/configurePom.kt     | 48 +++++++++-------------
 .../src/main/kotlin/publishing/maven-utils.kt      | 24 ++++++-----
 .../src/main/kotlin/publishing/shadowPub.kt        | 25 +++++++----
 3 files changed, 50 insertions(+), 47 deletions(-)

diff --git a/build-logic/src/main/kotlin/publishing/configurePom.kt b/build-logic/src/main/kotlin/publishing/configurePom.kt
index ee57589faf..09842db9d9 100644
--- a/build-logic/src/main/kotlin/publishing/configurePom.kt
+++ b/build-logic/src/main/kotlin/publishing/configurePom.kt
@@ -21,9 +21,9 @@ package publishing
 
 import groovy.namespace.QName
 import groovy.util.Node
+import org.gradle.api.GradleException
 import org.gradle.api.Project
 import org.gradle.api.Task
-import org.gradle.api.artifacts.component.ModuleComponentSelector
 import org.gradle.api.publish.maven.MavenPublication
 
 /**
@@ -55,6 +55,11 @@ internal fun configurePom(project: Project, mavenPublication: MavenPublication,
           }
         }
 
+        val parent = project.parent!!
+        val parentGroup = parent.group
+        val parentName = parent.name
+        val parentVersion = parent.version
+
         withXml {
           val projectNode = asNode()
 
@@ -74,20 +79,21 @@ internal fun configurePom(project: Project, mavenPublication: MavenPublication,
               }
             }
           }
-          val parent = project.parent!!
           // Add GAV to <parent> element
-          parentNode.appendNode("groupId", parent.group)
-          parentNode.appendNode("artifactId", parent.name)
-          parentNode.appendNode("version", parent.version)
+          parentNode.appendNode("groupId", parentGroup)
+          parentNode.appendNode("artifactId", parentName)
+          parentNode.appendNode("version", parentVersion)
 
-          addMissingMandatoryDependencyVersions(project, projectNode)
+          verifyMandatoryDependencyVersions(projectNode)
         }
       } else {
         val mavenPom = this
+        val effectiveAsfProject = project.provider { EffectiveAsfProject.forProject(project) }
+        val projectVersion = project.version.toString()
 
         task.doFirst {
           mavenPom.run {
-            val prj = EffectiveAsfProject.forProject(project)
+            val prj = effectiveAsfProject.get()
             val asfProjectId = prj.asfProject.apacheId
 
             organization {
@@ -120,10 +126,9 @@ internal fun configurePom(project: Project, mavenPublication: MavenPublication,
               connection.set("scm:git:$codeRepoString")
               developerConnection.set("scm:git:$codeRepoString")
               url.set("$codeRepoString/tree/main")
-              val version = project.version.toString()
-              if (!version.endsWith("-SNAPSHOT")) {
+              if (!projectVersion.endsWith("-SNAPSHOT")) {
                 val tagPrefix: String = prj.tagPrefix().get()
-                tag.set("$tagPrefix-$version")
+                tag.set("$tagPrefix-$projectVersion")
               }
             }
             issueManagement { url.set(prj.issueTracker()) }
@@ -141,32 +146,17 @@ internal fun configurePom(project: Project, mavenPublication: MavenPublication,
   }
 
 /**
- * Scans the generated `pom.xml` for `<dependencies>` in `<dependencyManagement>` that do not have a
- * `<version>` and adds one, if possible. Maven kinda requires `<version>` tags there, even if the
+ * Verifies that the generated `pom.xml` for `<dependencies>` in `<dependencyManagement>` all have
+ * the mandatory `<version>` element. Maven kinda requires `<version>` tags there, even if the
  * `<dependency>` without a `<version>` is a bom and that bom's version is available transitively.
  */
-fun addMissingMandatoryDependencyVersions(project: Project, projectNode: Node) {
+private fun verifyMandatoryDependencyVersions(projectNode: Node) {
   xmlNode(xmlNode(projectNode, "dependencyManagement"), "dependencies")?.children()?.forEach {
     val dependency = it as Node
     if (xmlNode(dependency, "version") == null) {
       val depGroup = xmlNode(dependency, "groupId")!!.text()
       val depName = xmlNode(dependency, "artifactId")!!.text()
-
-      var depResult =
-        findDependency(project.configurations.findByName("runtimeClasspath"), depGroup, depName)
-      if (depResult == null) {
-        depResult =
-          findDependency(
-            project.configurations.findByName("testRuntimeClasspath"),
-            depGroup,
-            depName,
-          )
-      }
-
-      if (depResult != null) {
-        val req = depResult.requested as ModuleComponentSelector
-        dependency.appendNode("version", req.version)
-      }
+      throw GradleException("Missing mandatory dependency version for $depGroup:$depName")
     }
   }
 }
diff --git a/build-logic/src/main/kotlin/publishing/maven-utils.kt b/build-logic/src/main/kotlin/publishing/maven-utils.kt
index 940fb54b9c..2301d374be 100644
--- a/build-logic/src/main/kotlin/publishing/maven-utils.kt
+++ b/build-logic/src/main/kotlin/publishing/maven-utils.kt
@@ -83,19 +83,23 @@ fun addAdditionalJarContent(project: Project): Unit =
       val generatePomProperties =
         tasks.register("generatePomProperties", GeneratePomProperties::class.java) {}
 
-      tasks.withType(Jar::class).configureEach {
-        // Only add the pom.xml to jars for 'release' builds, but not for developer/snapshot builds.
-        // Letting jars depend on the pom.xml (Gradle's `GenerateMavenPom` task, annotated with
-        // `@UntrackedTask`) breaks up-to-date checks for all jars and dependent project
-        // dependencies, leading to unnecessarily long build times.
-        val includePom =
-          rootProject.hasProperty("release") || rootProject.hasProperty("jarWithGitInfo")
-        if (includePom) {
-          from(tasks.named<GenerateMavenPom>("generatePomFileForMavenPublication")) {
+      // Only add the pom.xml to jars for 'release' builds, but not for developer/snapshot builds.
+      // Letting jars depend on the pom.xml (Gradle's `GenerateMavenPom` task, annotated with
+      // `@UntrackedTask`) breaks up-to-date checks for all jars and dependent project
+      // dependencies, leading to unnecessarily long build times.
+      if (rootProject.hasProperty("release") || rootProject.hasProperty("jarWithGitInfo")) {
+        val pomPath = "META-INF/maven/${project.group}/${project.name}/pom.xml"
+        val generatePomFileTask =
+          tasks.named<GenerateMavenPom>("generatePomFileForMavenPublication")
+        tasks.withType(Jar::class).configureEach {
+          from(generatePomFileTask) {
             include("pom-default.xml")
-            eachFile { path = "META-INF/maven/${project.group}/${project.name}/pom.xml" }
+            eachFile { path = pomPath }
           }
         }
+      }
+
+      tasks.withType(Jar::class).configureEach {
         from(generatePomProperties) {
           include("**")
           eachFile { path = sourcePath }
diff --git a/build-logic/src/main/kotlin/publishing/shadowPub.kt b/build-logic/src/main/kotlin/publishing/shadowPub.kt
index 2598e62594..80b7af881e 100644
--- a/build-logic/src/main/kotlin/publishing/shadowPub.kt
+++ b/build-logic/src/main/kotlin/publishing/shadowPub.kt
@@ -105,6 +105,15 @@ internal fun configureShadowPublishing(
     // This a replacement to add dependencies to the pom, if necessary. Equivalent to
     // 'shadowExtension.component(mavenPublication)', which we cannot use.
 
+    val pomDependencies =
+      project.provider {
+        project.configurations
+          .getByName("shadow")
+          .allDependencies
+          .filter { it is ProjectDependency }
+          .map { PomDependency(it.group, it.name, it.version) }
+      }
+
     mavenPublication.pom {
       withXml {
         val node = asNode()
@@ -112,15 +121,15 @@ internal fun configureShadowPublishing(
         val dependenciesNode =
           if ((depNode as NodeList).isNotEmpty()) depNode[0] as Node
           else node.appendNode("dependencies")
-        project.configurations.getByName("shadow").allDependencies.forEach {
-          if (it is ProjectDependency) {
-            val dependencyNode = dependenciesNode.appendNode("dependency")
-            dependencyNode.appendNode("groupId", it.group)
-            dependencyNode.appendNode("artifactId", it.name)
-            dependencyNode.appendNode("version", it.version)
-            dependencyNode.appendNode("scope", "runtime")
-          }
+        pomDependencies.get().forEach {
+          val dependencyNode = dependenciesNode.appendNode("dependency")
+          dependencyNode.appendNode("groupId", it.group)
+          dependencyNode.appendNode("artifactId", it.name)
+          dependencyNode.appendNode("version", it.version)
+          dependencyNode.appendNode("scope", "runtime")
         }
       }
     }
   }
+
+private data class PomDependency(val group: String?, val name: String, val version: String?)