From 058be9697ae248f5978ec73f2f12e308d23dcff9 Mon Sep 17 00:00:00 2001 From: Rene Groeschke Date: Mon, 18 Nov 2024 10:29:54 +0100 Subject: [PATCH] [Gradle] Fix PublishPlugin configuration cache compatibility (#116887) Relying on GenerateMavenPom#getPom breaks configuration cache compatibility as this is a transient property that is not serialized. --- .../internal/conventions/PublishPlugin.java | 54 +++++++++++-------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/build-conventions/src/main/java/org/elasticsearch/gradle/internal/conventions/PublishPlugin.java b/build-conventions/src/main/java/org/elasticsearch/gradle/internal/conventions/PublishPlugin.java index d19a1d492d9ed..c3124812e5089 100644 --- a/build-conventions/src/main/java/org/elasticsearch/gradle/internal/conventions/PublishPlugin.java +++ b/build-conventions/src/main/java/org/elasticsearch/gradle/internal/conventions/PublishPlugin.java @@ -17,11 +17,9 @@ import org.elasticsearch.gradle.internal.conventions.info.GitInfo; import org.elasticsearch.gradle.internal.conventions.precommit.PomValidationPrecommitPlugin; import org.elasticsearch.gradle.internal.conventions.util.Util; -import org.gradle.api.Action; import org.gradle.api.NamedDomainObjectSet; import org.gradle.api.Plugin; import org.gradle.api.Project; -import org.gradle.api.Task; import org.gradle.api.XmlProvider; import org.gradle.api.file.ProjectLayout; import org.gradle.api.plugins.BasePlugin; @@ -69,6 +67,7 @@ public void apply(Project project) { configureSourcesJar(project); configurePomGeneration(project); configurePublications(project); + formatGeneratedPom(project); } private void configurePublications(Project project) { @@ -127,42 +126,27 @@ private void configurePomGeneration(Project project) { projectVersion.get() ) ); - pomTask.doFirst(t -> pomTask.getPom().withXml(xml -> formatDependencies(xml))); }); var publishing = extensions.getByType(PublishingExtension.class); final var mavenPublications = publishing.getPublications().withType(MavenPublication.class); - addNameAndDescriptionToPom(project, mavenPublications); mavenPublications.configureEach(publication -> { - // Add git origin info to generated POM files for internal builds - publication.getPom().withXml(xml -> addScmInfo(xml, gitInfo.get())); + publication.getPom().withXml(xml -> { + // Add git origin info to generated POM files for internal builds + addScmInfo(xml, gitInfo.get()); + }); // have to defer this until archivesBaseName is set project.afterEvaluate(p -> publication.setArtifactId(archivesBaseName.get())); generatePomTask.configure(t -> t.dependsOn(generateMavenPoms)); }); } - /** - * just ensure we put dependencies to the end. more a cosmetic thing than anything else - * */ - private void formatDependencies(XmlProvider xml) { - Element rootElement = xml.asElement(); - var dependencies = rootElement.getElementsByTagName("dependencies"); - if (dependencies.getLength() == 1 && dependencies.item(0) != null) { - org.w3c.dom.Node item = dependencies.item(0); - rootElement.removeChild(item); - rootElement.appendChild(item); - } - } - private void addNameAndDescriptionToPom(Project project, NamedDomainObjectSet mavenPublications) { var name = project.getName(); var description = providerFactory.provider(() -> project.getDescription() != null ? project.getDescription() : ""); mavenPublications.configureEach(p -> p.getPom().withXml(xml -> { var root = xml.asNode(); - // Node versionNode = root.get("version"); - // versionNode.plus(1, "name", name); root.appendNode("name", name); root.appendNode("description", description.get()); })); @@ -209,4 +193,32 @@ static void configureSourcesJar(Project project) { project.getTasks().named(BasePlugin.ASSEMBLE_TASK_NAME).configure(t -> t.dependsOn(sourcesJarTask)); }); } + + + /** + * Format the generated pom files to be in a sort of reproducible order. + */ + private void formatGeneratedPom(Project project) { + var publishing = project.getExtensions().getByType(PublishingExtension.class); + final var mavenPublications = publishing.getPublications().withType(MavenPublication.class); + mavenPublications.configureEach(publication -> { + publication.getPom().withXml(xml -> { + // Add some pom formatting + formatDependencies(xml); + }); + }); + } + + /** + * just ensure we put dependencies to the end. more a cosmetic thing than anything else + * */ + private void formatDependencies(XmlProvider xml) { + Element rootElement = xml.asElement(); + var dependencies = rootElement.getElementsByTagName("dependencies"); + if (dependencies.getLength() == 1 && dependencies.item(0) != null) { + org.w3c.dom.Node item = dependencies.item(0); + rootElement.removeChild(item); + rootElement.appendChild(item); + } + } }