Skip to content

Commit

Permalink
Make PublishPlugin gradle configuration cache compliant (#74828) (#75772
Browse files Browse the repository at this point in the history
)

* Make PublishPlugin gradle configuration cache compliant

for using --configuration-cache we need to remove some usages of the mutable project instance

* Polishing
  • Loading branch information
breskeby authored Jul 28, 2021
1 parent e637a64 commit 8ad7307
Showing 1 changed file with 54 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,71 @@
import org.elasticsearch.gradle.internal.conventions.info.GitInfo;
import org.gradle.api.NamedDomainObjectSet;
import org.gradle.api.Plugin;
import org.gradle.api.Task;
import org.gradle.api.Project;
import org.gradle.api.XmlProvider;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.BasePluginConvention;
import org.gradle.api.plugins.BasePluginExtension;
import org.gradle.api.plugins.ExtensionContainer;
import org.gradle.api.plugins.JavaLibraryPlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
import org.gradle.api.publish.maven.tasks.GenerateMavenPom;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.initialization.layout.BuildLayout;
import org.gradle.language.base.plugins.LifecycleBasePlugin;

import javax.inject.Inject;
import java.io.File;
import java.util.Map;
import java.util.concurrent.Callable;

public class PublishPlugin implements Plugin<Project> {

private ProjectLayout projectLayout;
private BuildLayout buildLayout;
private ProviderFactory providerFactory;

@Inject
public PublishPlugin(ProjectLayout projectLayout, BuildLayout buildLayout, ProviderFactory providerFactory) {
this.projectLayout = projectLayout;
this.buildLayout = buildLayout;
this.providerFactory = providerFactory;
}

@Override
public void apply(Project project) {
project.getPluginManager().apply(BasePlugin.class);
project.getPluginManager().apply(MavenPublishPlugin.class);
project.getPluginManager().apply(PomValidationPrecommitPlugin.class);
project.getPluginManager().apply(LicensingPlugin.class);

configureJavadocJar(project);
configureSourcesJar(project);
configurePomGeneration(project);
configurePublications(project);
}

private void configurePublications(Project project) {
PublishingExtension publishingExtension = project.getExtensions().getByType(PublishingExtension.class);
MavenPublication publication = publishingExtension.getPublications().create("elastic", MavenPublication.class);

var publishingExtension = project.getExtensions().getByType(PublishingExtension.class);
var publication = publishingExtension.getPublications().create("elastic", MavenPublication.class);
project.afterEvaluate(project1 -> {
if (project1.getPlugins().hasPlugin(ShadowPlugin.class)) {
configureWithShadowPlugin(project1, publication);
} else if (project1.getPlugins().hasPlugin(JavaPlugin.class)) {
publication.from(project.getComponents().getByName("java"));
}
});
var projectLicenses = (MapProperty<String, String>) project.getExtensions().getExtraProperties().get("projectLicenses");
publication.getPom().withXml(xml -> {
Node node = xml.asNode();
node.appendNode("inceptionYear", "2009");
Node licensesNode = node.appendNode("licenses");
MapProperty<String, String> projectLicenses = (MapProperty<String, String>) project.getExtensions().getExtraProperties().get("projectLicenses");
var licensesNode = node.appendNode("licenses");
projectLicenses.get().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
Node license = licensesNode.appendNode("license");
license.appendNode("name", entry.getKey());
Expand All @@ -82,58 +93,60 @@ private void configurePublications(Project project) {
});
publishingExtension.getRepositories().maven(mavenArtifactRepository -> {
mavenArtifactRepository.setName("test");
mavenArtifactRepository.setUrl(new File(project.getRootProject().getBuildDir(), "local-test-repo"));
mavenArtifactRepository.setUrl(new File(buildLayout.getRootDirectory(), "build/local-test-repo"));
});
}

private static String getArchivesBaseName(Project project) {
return project.getConvention().getPlugin(BasePluginConvention.class).getArchivesBaseName();
private static String getArchivesBaseName(ExtensionContainer extensions) {
return extensions.getByType(BasePluginExtension.class).getArchivesName().get();
}

/**
* Configuration generation of maven poms.
*/
private static void configurePomGeneration(Project project) {
Property<GitInfo> gitInfo = project.getRootProject().getPlugins().apply(GitInfoPlugin.class).getGitInfo();

TaskProvider<Task> generatePomTask = project.getTasks().register("generatePom");
private void configurePomGeneration(Project project) {
var gitInfo = project.getRootProject().getPlugins().apply(GitInfoPlugin.class).getGitInfo();
var generatePomTask = project.getTasks().register("generatePom");
project.getTasks().named(LifecycleBasePlugin.ASSEMBLE_TASK_NAME).configure(assemble -> assemble.dependsOn(generatePomTask));
project.getTasks()
.withType(GenerateMavenPom.class)
.configureEach(
pomTask -> pomTask.setDestination(
(Callable<String>) () -> String.format(
"%s/distributions/%s-%s.pom",
project.getBuildDir(),
getArchivesBaseName(project),
project.getVersion()
)
)
);
PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class);
final NamedDomainObjectSet<MavenPublication> mavenPublications = publishing.getPublications().withType(MavenPublication.class);
var extensions = project.getExtensions();
var archivesBaseName = providerFactory.provider(() -> getArchivesBaseName(extensions));
var projectVersion = providerFactory.provider(() -> project.getVersion());
var generateMavenPoms = project.getTasks().withType(GenerateMavenPom.class);
generateMavenPoms.all(
pomTask -> pomTask.setDestination(
(Callable<String>) () -> String.format(
"%s/distributions/%s-%s.pom",
projectLayout.getBuildDirectory().get().getAsFile().getPath(),
archivesBaseName.get(),
projectVersion.get()
)
)
);
var publishing = extensions.getByType(PublishingExtension.class);
final var mavenPublications = publishing.getPublications().withType(MavenPublication.class);
addNameAndDescriptiontoPom(project, mavenPublications);

mavenPublications.all(publication -> {
// Add git origin info to generated POM files for internal builds
publication.getPom().withXml((xmlProvider) -> addScmInfo(xmlProvider, gitInfo.get()));
publication.getPom().withXml(xml -> addScmInfo(xml, gitInfo.get()));
// have to defer this until archivesBaseName is set
project.afterEvaluate(p -> publication.setArtifactId(getArchivesBaseName(project)));
generatePomTask.configure(t -> t.dependsOn(project.getTasks().withType(GenerateMavenPom.class)));
project.afterEvaluate(p -> publication.setArtifactId(archivesBaseName.get()));
generatePomTask.configure(t -> t.dependsOn(generateMavenPoms));
});
}

private static void addNameAndDescriptiontoPom(Project project, NamedDomainObjectSet<MavenPublication> mavenPublications) {
private void addNameAndDescriptiontoPom(Project project, NamedDomainObjectSet<MavenPublication> mavenPublications) {
var name = project.getName();
var description = providerFactory.provider(() -> project.getDescription() != null ? project.getDescription() : "");
mavenPublications.all(p -> p.getPom().withXml(xml -> {
Node root = xml.asNode();
root.appendNode("name", project.getName());
String description = project.getDescription() != null ? project.getDescription() : "";
root.appendNode("description", description);
var root = xml.asNode();
root.appendNode("name", name);
root.appendNode("description", description.get());
}));
}

private static void configureWithShadowPlugin(Project project, MavenPublication publication) {
ShadowExtension shadow = project.getExtensions().getByType(ShadowExtension.class);
var shadow = project.getExtensions().getByType(ShadowExtension.class);
shadow.component(publication);
}

Expand Down Expand Up @@ -162,7 +175,7 @@ private static void configureJavadocJar(Project project) {

static void configureSourcesJar(Project project) {
project.getPlugins().withType(JavaLibraryPlugin.class, p -> {
TaskProvider<Jar> sourcesJarTask = project.getTasks().register("sourcesJar", Jar.class);
var sourcesJarTask = project.getTasks().register("sourcesJar", Jar.class);
sourcesJarTask.configure(jar -> {
jar.getArchiveClassifier().set("sources");
jar.setGroup("build");
Expand Down

0 comments on commit 8ad7307

Please sign in to comment.