Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PublishPlugin gradle configuration cache compliant #74828

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,47 @@
import org.gradle.api.Plugin;
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);
Expand All @@ -56,19 +68,18 @@ public void apply(Project project) {
private void configurePublications(Project project) {
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 -> {
var node = xml.asNode();
node.appendNode("inceptionYear", "2009");
var licensesNode = node.appendNode("licenses");
var projectLicenses = (MapProperty<String, String>) project.getExtensions().getExtraProperties().get("projectLicenses");
projectLicenses.get().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
Node license = licensesNode.appendNode("license");
license.appendNode("name", entry.getKey());
Expand All @@ -81,58 +92,59 @@ 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();

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()
)
)
);
var publishing = project.getExtensions().getByType(PublishingExtension.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 -> {
var root = xml.asNode();
root.appendNode("name", project.getName());
var description = project.getDescription() != null ? project.getDescription() : "";
root.appendNode("description", description);
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 @@ -161,7 +173,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