Skip to content

Commit

Permalink
[Gradle] Fix PublishPlugin configuration cache compatibility (elastic…
Browse files Browse the repository at this point in the history
…#116887)

Relying on GenerateMavenPom#getPom breaks configuration cache compatibility
as this is a transient property that is not serialized.
  • Loading branch information
breskeby authored Nov 18, 2024
1 parent 3253c2a commit 058be96
Showing 1 changed file with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +67,7 @@ public void apply(Project project) {
configureSourcesJar(project);
configurePomGeneration(project);
configurePublications(project);
formatGeneratedPom(project);
}

private void configurePublications(Project project) {
Expand Down Expand Up @@ -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<MavenPublication> 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());
}));
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 058be96

Please sign in to comment.