From 235e3e818d6ddb1ddf3e46fad8db71abb7b53307 Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Tue, 19 May 2020 19:00:59 -0500 Subject: [PATCH 1/8] Allow ElasticsearchJavaPlugin for non-jar based projects Some standalone projects such as :distribution:archives:integ-test-zip have tests but are not intended to ever create a jar. Applying the new ElasticsearchJavaPlugin in favor over the elder "Standalone*Plugin" or BuildPlugin can result in these non-jar projects erroring due to the lack of the license or notice file. To avoid creating more permutations of ElasticsearchJavaPlugin (i.e. ElasticsearchJavaPluginWithoutJar), the conuming project should be able disable the jar task (e.g. `jar.enabled = false`) and the license checks should be skipped as well as the javadoc and sources jars. related: #56841 --- .../gradle/ElasticsearchJavaPlugin.java | 71 +++++++++---------- .../elasticsearch/gradle/PublishPlugin.java | 2 + 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java index 0cfe71a020167..45fb92b5644cb 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java @@ -398,48 +398,43 @@ static void configureJars(Project project) { ExtraPropertiesExtension ext = project.getExtensions().getExtraProperties(); ext.set("licenseFile", null); ext.set("noticeFile", null); - project.getTasks() - .withType(Jar.class) - .configureEach( - jarTask -> { - // we put all our distributable files under distributions - jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); - // fixup the jar manifest - jarTask.doFirst( - t -> { - // this doFirst is added before the info plugin, therefore it will run - // after the doFirst added by the info plugin, and we can override attributes - jarTask.getManifest() - .attributes( - Map.of( - "Build-Date", - BuildParams.getBuildDate(), - "Build-Java-Version", - BuildParams.getCompilerJavaVersion() - ) - ); - } - ); - } - ); + project.getTasks().withType(Jar.class).configureEach(jarTask -> { + if (jarTask.isEnabled()) { + // we put all our distributable files under distributions + jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); + // fixup the jar manifest + jarTask.doFirst( + t -> { + // this doFirst is added before the info plugin, therefore it will run + // after the doFirst added by the info plugin, and we can override attributes + jarTask.getManifest() + .attributes( + Map.of("Build-Date", BuildParams.getBuildDate(), "Build-Java-Version", BuildParams.getCompilerJavaVersion()) + ); + } + ); + } + }); // add license/notice files project.afterEvaluate(p -> project.getTasks().withType(Jar.class).configureEach(jarTask -> { - File licenseFile = (File) ext.get("licenseFile"); - File noticeFile = (File) ext.get("noticeFile"); - if (licenseFile == null || noticeFile == null) { - throw new GradleException("Must specify license and notice file for project"); - } + if (jarTask.isEnabled()) { + File licenseFile = (File) ext.get("licenseFile"); + File noticeFile = (File) ext.get("noticeFile"); + if (licenseFile == null || noticeFile == null) { + throw new GradleException("Must specify license and notice file for project"); + } - jarTask.metaInf(spec -> { - spec.from(licenseFile.getParent(), from -> { - from.include(licenseFile.getName()); - from.rename(s -> "LICENSE.txt"); + jarTask.metaInf(spec -> { + spec.from(licenseFile.getParent(), from -> { + from.include(licenseFile.getName()); + from.rename(s -> "LICENSE.txt"); + }); + spec.from(noticeFile.getParent(), from -> { + from.include(noticeFile.getName()); + from.rename(s -> "NOTICE.txt"); + }); }); - spec.from(noticeFile.getParent(), from -> { - from.include(noticeFile.getName()); - from.rename(s -> "NOTICE.txt"); - }); - }); + } })); project.getPluginManager().withPlugin("com.github.johnrengelman.shadow", p -> { project.getTasks() diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java index 857f5c7331694..23c16a7f8794d 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java @@ -132,6 +132,7 @@ private static void configureJavadocJar(Project project) { project.getPlugins().withId("elasticsearch.java", p -> { TaskProvider javadocJarTask = project.getTasks().register("javadocJar", Jar.class); javadocJarTask.configure(jar -> { + jar.setEnabled(project.getTasks().getByName("jar").getEnabled()); jar.getArchiveClassifier().set("javadoc"); jar.setGroup("build"); jar.setDescription("Assembles a jar containing javadocs."); @@ -145,6 +146,7 @@ static void configureSourcesJar(Project project) { project.getPlugins().withId("elasticsearch.java", p -> { TaskProvider sourcesJarTask = project.getTasks().register("sourcesJar", Jar.class); sourcesJarTask.configure(jar -> { + jar.setEnabled(project.getTasks().getByName("jar").getEnabled()); jar.getArchiveClassifier().set("sources"); jar.setGroup("build"); jar.setDescription("Assembles a jar containing source files."); From b1f5a23608b481e78290afed93dcafd04d77d8f0 Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Tue, 19 May 2020 19:13:52 -0500 Subject: [PATCH 2/8] remove unecessary enabled check --- .../gradle/ElasticsearchJavaPlugin.java | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java index 45fb92b5644cb..98646c641f794 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java @@ -398,23 +398,30 @@ static void configureJars(Project project) { ExtraPropertiesExtension ext = project.getExtensions().getExtraProperties(); ext.set("licenseFile", null); ext.set("noticeFile", null); - project.getTasks().withType(Jar.class).configureEach(jarTask -> { - if (jarTask.isEnabled()) { - // we put all our distributable files under distributions - jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); - // fixup the jar manifest - jarTask.doFirst( - t -> { - // this doFirst is added before the info plugin, therefore it will run - // after the doFirst added by the info plugin, and we can override attributes - jarTask.getManifest() - .attributes( - Map.of("Build-Date", BuildParams.getBuildDate(), "Build-Java-Version", BuildParams.getCompilerJavaVersion()) - ); - } - ); - } - }); + project.getTasks() + .withType(Jar.class) + .configureEach( + jarTask -> { + // we put all our distributable files under distributions + jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); + // fixup the jar manifest + jarTask.doFirst( + t -> { + // this doFirst is added before the info plugin, therefore it will run + // after the doFirst added by the info plugin, and we can override attributes + jarTask.getManifest() + .attributes( + Map.of( + "Build-Date", + BuildParams.getBuildDate(), + "Build-Java-Version", + BuildParams.getCompilerJavaVersion() + ) + ); + } + ); + } + ); // add license/notice files project.afterEvaluate(p -> project.getTasks().withType(Jar.class).configureEach(jarTask -> { if (jarTask.isEnabled()) { From fa86e2437a88465c759b51596a7f887ebd14f69f Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Thu, 21 May 2020 09:00:55 -0500 Subject: [PATCH 3/8] Revert "remove unecessary enabled check" This reverts commit b1f5a23608b481e78290afed93dcafd04d77d8f0. --- .../gradle/ElasticsearchJavaPlugin.java | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java index 98646c641f794..45fb92b5644cb 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java @@ -398,30 +398,23 @@ static void configureJars(Project project) { ExtraPropertiesExtension ext = project.getExtensions().getExtraProperties(); ext.set("licenseFile", null); ext.set("noticeFile", null); - project.getTasks() - .withType(Jar.class) - .configureEach( - jarTask -> { - // we put all our distributable files under distributions - jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); - // fixup the jar manifest - jarTask.doFirst( - t -> { - // this doFirst is added before the info plugin, therefore it will run - // after the doFirst added by the info plugin, and we can override attributes - jarTask.getManifest() - .attributes( - Map.of( - "Build-Date", - BuildParams.getBuildDate(), - "Build-Java-Version", - BuildParams.getCompilerJavaVersion() - ) - ); - } - ); - } - ); + project.getTasks().withType(Jar.class).configureEach(jarTask -> { + if (jarTask.isEnabled()) { + // we put all our distributable files under distributions + jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); + // fixup the jar manifest + jarTask.doFirst( + t -> { + // this doFirst is added before the info plugin, therefore it will run + // after the doFirst added by the info plugin, and we can override attributes + jarTask.getManifest() + .attributes( + Map.of("Build-Date", BuildParams.getBuildDate(), "Build-Java-Version", BuildParams.getCompilerJavaVersion()) + ); + } + ); + } + }); // add license/notice files project.afterEvaluate(p -> project.getTasks().withType(Jar.class).configureEach(jarTask -> { if (jarTask.isEnabled()) { From 2e38b55350e69141d8c659572a59c30c099cd32a Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Thu, 21 May 2020 09:01:06 -0500 Subject: [PATCH 4/8] Revert "Allow ElasticsearchJavaPlugin for non-jar based projects" This reverts commit 235e3e818d6ddb1ddf3e46fad8db71abb7b53307. --- .../gradle/ElasticsearchJavaPlugin.java | 71 ++++++++++--------- .../elasticsearch/gradle/PublishPlugin.java | 2 - 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java index 45fb92b5644cb..0cfe71a020167 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java @@ -398,43 +398,48 @@ static void configureJars(Project project) { ExtraPropertiesExtension ext = project.getExtensions().getExtraProperties(); ext.set("licenseFile", null); ext.set("noticeFile", null); - project.getTasks().withType(Jar.class).configureEach(jarTask -> { - if (jarTask.isEnabled()) { - // we put all our distributable files under distributions - jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); - // fixup the jar manifest - jarTask.doFirst( - t -> { - // this doFirst is added before the info plugin, therefore it will run - // after the doFirst added by the info plugin, and we can override attributes - jarTask.getManifest() - .attributes( - Map.of("Build-Date", BuildParams.getBuildDate(), "Build-Java-Version", BuildParams.getCompilerJavaVersion()) - ); - } - ); - } - }); + project.getTasks() + .withType(Jar.class) + .configureEach( + jarTask -> { + // we put all our distributable files under distributions + jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); + // fixup the jar manifest + jarTask.doFirst( + t -> { + // this doFirst is added before the info plugin, therefore it will run + // after the doFirst added by the info plugin, and we can override attributes + jarTask.getManifest() + .attributes( + Map.of( + "Build-Date", + BuildParams.getBuildDate(), + "Build-Java-Version", + BuildParams.getCompilerJavaVersion() + ) + ); + } + ); + } + ); // add license/notice files project.afterEvaluate(p -> project.getTasks().withType(Jar.class).configureEach(jarTask -> { - if (jarTask.isEnabled()) { - File licenseFile = (File) ext.get("licenseFile"); - File noticeFile = (File) ext.get("noticeFile"); - if (licenseFile == null || noticeFile == null) { - throw new GradleException("Must specify license and notice file for project"); - } + File licenseFile = (File) ext.get("licenseFile"); + File noticeFile = (File) ext.get("noticeFile"); + if (licenseFile == null || noticeFile == null) { + throw new GradleException("Must specify license and notice file for project"); + } - jarTask.metaInf(spec -> { - spec.from(licenseFile.getParent(), from -> { - from.include(licenseFile.getName()); - from.rename(s -> "LICENSE.txt"); - }); - spec.from(noticeFile.getParent(), from -> { - from.include(noticeFile.getName()); - from.rename(s -> "NOTICE.txt"); - }); + jarTask.metaInf(spec -> { + spec.from(licenseFile.getParent(), from -> { + from.include(licenseFile.getName()); + from.rename(s -> "LICENSE.txt"); }); - } + spec.from(noticeFile.getParent(), from -> { + from.include(noticeFile.getName()); + from.rename(s -> "NOTICE.txt"); + }); + }); })); project.getPluginManager().withPlugin("com.github.johnrengelman.shadow", p -> { project.getTasks() diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java index 23c16a7f8794d..857f5c7331694 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java @@ -132,7 +132,6 @@ private static void configureJavadocJar(Project project) { project.getPlugins().withId("elasticsearch.java", p -> { TaskProvider javadocJarTask = project.getTasks().register("javadocJar", Jar.class); javadocJarTask.configure(jar -> { - jar.setEnabled(project.getTasks().getByName("jar").getEnabled()); jar.getArchiveClassifier().set("javadoc"); jar.setGroup("build"); jar.setDescription("Assembles a jar containing javadocs."); @@ -146,7 +145,6 @@ static void configureSourcesJar(Project project) { project.getPlugins().withId("elasticsearch.java", p -> { TaskProvider sourcesJarTask = project.getTasks().register("sourcesJar", Jar.class); sourcesJarTask.configure(jar -> { - jar.setEnabled(project.getTasks().getByName("jar").getEnabled()); jar.getArchiveClassifier().set("sources"); jar.setGroup("build"); jar.setDescription("Assembles a jar containing source files."); From 845a2a8b7c4eee1f6c4ae6b0ae0e081242c7d9ae Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Thu, 21 May 2020 09:03:47 -0500 Subject: [PATCH 5/8] remove license check from ElasticsearchJavaPlugin --- .../src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy | 1 + .../java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index 76accc5c4756b..38268cd375823 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -82,6 +82,7 @@ class BuildPlugin implements Plugin { ) } project.pluginManager.apply('elasticsearch.java') + ElasticsearchJavaPlugin.configureJars(project) project.pluginManager.apply('elasticsearch.publish') project.pluginManager.apply(DependenciesInfoPlugin) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java index 0cfe71a020167..7a79e39205541 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java @@ -80,7 +80,6 @@ public void apply(Project project) { configureCompile(project); configureInputNormalization(project); configureTestTasks(project); - configureJars(project); configureJarManifest(project); configureJavadoc(project); } From 908ee17737f1a283cf0535d0bfe102fa65144fb4 Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Thu, 21 May 2020 12:55:46 -0500 Subject: [PATCH 6/8] resurect Groovy configureJars and remove from Java --- .../elasticsearch/gradle/BuildPlugin.groovy | 75 +++++++++++++++++- .../gradle/ElasticsearchJavaPlugin.java | 76 ------------------- 2 files changed, 73 insertions(+), 78 deletions(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index 38268cd375823..1a787277297b7 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -18,9 +18,11 @@ */ package org.elasticsearch.gradle - +import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import groovy.transform.CompileStatic import org.apache.commons.io.IOUtils +import org.elasticsearch.gradle.info.BuildParams import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin import org.elasticsearch.gradle.precommit.PrecommitTasks import org.elasticsearch.gradle.test.ErrorReportingTestListener @@ -30,6 +32,7 @@ import org.elasticsearch.gradle.util.GradleUtils import org.gradle.api.Action import org.gradle.api.GradleException import org.gradle.api.InvalidUserDataException +import org.gradle.api.JavaVersion import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Plugin import org.gradle.api.Project @@ -45,8 +48,11 @@ import org.gradle.api.artifacts.repositories.IvyPatternRepositoryLayout import org.gradle.api.artifacts.repositories.MavenArtifactRepository import org.gradle.api.credentials.HttpHeaderCredentials import org.gradle.api.execution.TaskActionListener +import org.gradle.api.file.CopySpec +import org.gradle.api.plugins.BasePlugin import org.gradle.api.plugins.ExtraPropertiesExtension import org.gradle.api.plugins.JavaPlugin +import org.gradle.api.tasks.bundling.Jar import org.gradle.api.tasks.testing.Test import org.gradle.authentication.http.HttpHeaderAuthentication import org.gradle.util.GradleVersion @@ -82,7 +88,7 @@ class BuildPlugin implements Plugin { ) } project.pluginManager.apply('elasticsearch.java') - ElasticsearchJavaPlugin.configureJars(project) + configureJars(project) project.pluginManager.apply('elasticsearch.publish') project.pluginManager.apply(DependenciesInfoPlugin) @@ -257,6 +263,7 @@ class BuildPlugin implements Plugin { } } + private static class TestFailureReportingPlugin implements Plugin { @Override void apply(Project project) { @@ -289,4 +296,68 @@ class BuildPlugin implements Plugin { private static inFipsJvm(){ return Boolean.parseBoolean(System.getProperty("tests.fips.enabled")); } + + /** Adds additional manifest info to jars */ + static void configureJars(Project project) { + ExtraPropertiesExtension ext = project.extensions.getByType(ExtraPropertiesExtension) + ext.set('licenseFile', null) + ext.set('noticeFile', null) + project.tasks.withType(Jar).configureEach { Jar jarTask -> + // we put all our distributable files under distributions + jarTask.destinationDirectory.set(new File(project.buildDir, 'distributions')) + // fixup the jar manifest + jarTask.doFirst { + // this doFirst is added before the info plugin, therefore it will run + // after the doFirst added by the info plugin, and we can override attributes + JavaVersion compilerJavaVersion = BuildParams.compilerJavaVersion + jarTask.manifest.attributes( + 'Build-Date': BuildParams.buildDate, + 'Build-Java-Version': BuildParams.compilerJavaVersion) + } + } + // add license/notice files + project.afterEvaluate { + project.tasks.withType(Jar).configureEach { Jar jarTask -> + if (ext.has('licenseFile') == false || ext.get('licenseFile') == null || ext.has('noticeFile') == false || ext.get('noticeFile') == null) { + throw new GradleException("Must specify license and notice file for project ${project.path}") + } + + File licenseFile = ext.get('licenseFile') as File + File noticeFile = ext.get('noticeFile') as File + + jarTask.metaInf { CopySpec spec -> + spec.from(licenseFile.parent) { CopySpec from -> + from.include licenseFile.name + from.rename { 'LICENSE.txt' } + } + spec.from(noticeFile.parent) { CopySpec from -> + from.include noticeFile.name + from.rename { 'NOTICE.txt' } + } + } + } + } + project.pluginManager.withPlugin('com.github.johnrengelman.shadow') { + project.tasks.getByName(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME).configure { ShadowJar shadowJar -> + /* + * Replace the default "-all" classifier with null + * which will leave the classifier off of the file name. + */ + shadowJar.archiveClassifier.set((String) null) + /* + * Not all cases need service files merged but it is + * better to be safe + */ + shadowJar.mergeServiceFiles() + } + // Add "original" classifier to the non-shadowed JAR to distinguish it from the shadow JAR + project.tasks.getByName(JavaPlugin.JAR_TASK_NAME).configure { Jar jar -> + jar.archiveClassifier.set('original') + } + // Make sure we assemble the shadow jar + project.tasks.named(BasePlugin.ASSEMBLE_TASK_NAME).configure { Task task -> + task.dependsOn 'shadowJar' + } + } + } } diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java index 7a79e39205541..68a9ad5c53e73 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java @@ -20,7 +20,6 @@ package org.elasticsearch.gradle; import com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin; -import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar; import nebula.plugin.info.InfoBrokerPlugin; import org.elasticsearch.gradle.info.BuildParams; import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin; @@ -37,13 +36,10 @@ import org.gradle.api.artifacts.ProjectDependency; import org.gradle.api.artifacts.ResolutionStrategy; import org.gradle.api.file.FileCollection; -import org.gradle.api.plugins.BasePlugin; -import org.gradle.api.plugins.ExtraPropertiesExtension; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; -import org.gradle.api.tasks.bundling.Jar; import org.gradle.api.tasks.compile.CompileOptions; import org.gradle.api.tasks.compile.GroovyCompile; import org.gradle.api.tasks.compile.JavaCompile; @@ -392,78 +388,6 @@ public void execute(Task t) { }); } - /** Adds additional manifest info to jars */ - static void configureJars(Project project) { - ExtraPropertiesExtension ext = project.getExtensions().getExtraProperties(); - ext.set("licenseFile", null); - ext.set("noticeFile", null); - project.getTasks() - .withType(Jar.class) - .configureEach( - jarTask -> { - // we put all our distributable files under distributions - jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); - // fixup the jar manifest - jarTask.doFirst( - t -> { - // this doFirst is added before the info plugin, therefore it will run - // after the doFirst added by the info plugin, and we can override attributes - jarTask.getManifest() - .attributes( - Map.of( - "Build-Date", - BuildParams.getBuildDate(), - "Build-Java-Version", - BuildParams.getCompilerJavaVersion() - ) - ); - } - ); - } - ); - // add license/notice files - project.afterEvaluate(p -> project.getTasks().withType(Jar.class).configureEach(jarTask -> { - File licenseFile = (File) ext.get("licenseFile"); - File noticeFile = (File) ext.get("noticeFile"); - if (licenseFile == null || noticeFile == null) { - throw new GradleException("Must specify license and notice file for project"); - } - - jarTask.metaInf(spec -> { - spec.from(licenseFile.getParent(), from -> { - from.include(licenseFile.getName()); - from.rename(s -> "LICENSE.txt"); - }); - spec.from(noticeFile.getParent(), from -> { - from.include(noticeFile.getName()); - from.rename(s -> "NOTICE.txt"); - }); - }); - })); - project.getPluginManager().withPlugin("com.github.johnrengelman.shadow", p -> { - project.getTasks() - .withType(ShadowJar.class) - .configureEach( - shadowJar -> { - /* - * Replace the default "-all" classifier with null - * which will leave the classifier off of the file name. - */ - shadowJar.getArchiveClassifier().set((String) null); - /* - * Not all cases need service files merged but it is - * better to be safe - */ - shadowJar.mergeServiceFiles(); - } - ); - // Add "original" classifier to the non-shadowed JAR to distinguish it from the shadow JAR - project.getTasks().named(JavaPlugin.JAR_TASK_NAME, Jar.class).configure(jar -> jar.getArchiveClassifier().set("original")); - // Make sure we assemble the shadow jar - project.getTasks().named(BasePlugin.ASSEMBLE_TASK_NAME).configure(task -> task.dependsOn("shadowJar")); - }); - } - private static void configureJarManifest(Project project) { project.getPlugins().withType(InfoBrokerPlugin.class).whenPluginAdded(manifestPlugin -> { manifestPlugin.add("Module-Origin", toStringable(BuildParams::getGitOrigin)); From 5812825ca03da18918f8b817a8650840d6b97165 Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Thu, 21 May 2020 13:19:38 -0500 Subject: [PATCH 7/8] only move license stuff back to groovy --- .../elasticsearch/gradle/BuildPlugin.groovy | 54 ++---------------- .../gradle/ElasticsearchJavaPlugin.java | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+), 50 deletions(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index 1a787277297b7..c4a706bb54a9c 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -18,25 +18,16 @@ */ package org.elasticsearch.gradle -import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin -import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar + import groovy.transform.CompileStatic import org.apache.commons.io.IOUtils -import org.elasticsearch.gradle.info.BuildParams import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin import org.elasticsearch.gradle.precommit.PrecommitTasks import org.elasticsearch.gradle.test.ErrorReportingTestListener import org.elasticsearch.gradle.testclusters.ElasticsearchCluster import org.elasticsearch.gradle.testclusters.TestClustersPlugin import org.elasticsearch.gradle.util.GradleUtils -import org.gradle.api.Action -import org.gradle.api.GradleException -import org.gradle.api.InvalidUserDataException -import org.gradle.api.JavaVersion -import org.gradle.api.NamedDomainObjectContainer -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.Task +import org.gradle.api.* import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.Dependency import org.gradle.api.artifacts.ModuleDependency @@ -49,7 +40,6 @@ import org.gradle.api.artifacts.repositories.MavenArtifactRepository import org.gradle.api.credentials.HttpHeaderCredentials import org.gradle.api.execution.TaskActionListener import org.gradle.api.file.CopySpec -import org.gradle.api.plugins.BasePlugin import org.gradle.api.plugins.ExtraPropertiesExtension import org.gradle.api.plugins.JavaPlugin import org.gradle.api.tasks.bundling.Jar @@ -88,7 +78,7 @@ class BuildPlugin implements Plugin { ) } project.pluginManager.apply('elasticsearch.java') - configureJars(project) + configureLicenseAndNotice(project) project.pluginManager.apply('elasticsearch.publish') project.pluginManager.apply(DependenciesInfoPlugin) @@ -297,24 +287,10 @@ class BuildPlugin implements Plugin { return Boolean.parseBoolean(System.getProperty("tests.fips.enabled")); } - /** Adds additional manifest info to jars */ - static void configureJars(Project project) { + static void configureLicenseAndNotice(Project project) { ExtraPropertiesExtension ext = project.extensions.getByType(ExtraPropertiesExtension) ext.set('licenseFile', null) ext.set('noticeFile', null) - project.tasks.withType(Jar).configureEach { Jar jarTask -> - // we put all our distributable files under distributions - jarTask.destinationDirectory.set(new File(project.buildDir, 'distributions')) - // fixup the jar manifest - jarTask.doFirst { - // this doFirst is added before the info plugin, therefore it will run - // after the doFirst added by the info plugin, and we can override attributes - JavaVersion compilerJavaVersion = BuildParams.compilerJavaVersion - jarTask.manifest.attributes( - 'Build-Date': BuildParams.buildDate, - 'Build-Java-Version': BuildParams.compilerJavaVersion) - } - } // add license/notice files project.afterEvaluate { project.tasks.withType(Jar).configureEach { Jar jarTask -> @@ -337,27 +313,5 @@ class BuildPlugin implements Plugin { } } } - project.pluginManager.withPlugin('com.github.johnrengelman.shadow') { - project.tasks.getByName(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME).configure { ShadowJar shadowJar -> - /* - * Replace the default "-all" classifier with null - * which will leave the classifier off of the file name. - */ - shadowJar.archiveClassifier.set((String) null) - /* - * Not all cases need service files merged but it is - * better to be safe - */ - shadowJar.mergeServiceFiles() - } - // Add "original" classifier to the non-shadowed JAR to distinguish it from the shadow JAR - project.tasks.getByName(JavaPlugin.JAR_TASK_NAME).configure { Jar jar -> - jar.archiveClassifier.set('original') - } - // Make sure we assemble the shadow jar - project.tasks.named(BasePlugin.ASSEMBLE_TASK_NAME).configure { Task task -> - task.dependsOn 'shadowJar' - } - } } } diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java index 68a9ad5c53e73..cefc189921e7a 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java @@ -20,6 +20,7 @@ package org.elasticsearch.gradle; import com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin; +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar; import nebula.plugin.info.InfoBrokerPlugin; import org.elasticsearch.gradle.info.BuildParams; import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin; @@ -36,10 +37,12 @@ import org.gradle.api.artifacts.ProjectDependency; import org.gradle.api.artifacts.ResolutionStrategy; import org.gradle.api.file.FileCollection; +import org.gradle.api.plugins.BasePlugin; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; +import org.gradle.api.tasks.bundling.Jar; import org.gradle.api.tasks.compile.CompileOptions; import org.gradle.api.tasks.compile.GroovyCompile; import org.gradle.api.tasks.compile.JavaCompile; @@ -388,6 +391,58 @@ public void execute(Task t) { }); } + /** + * Adds additional manifest info to jars + */ + static void configureJars(Project project) { + project.getTasks() + .withType(Jar.class) + .configureEach( + jarTask -> { + // we put all our distributable files under distributions + jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions")); + // fixup the jar manifest + jarTask.doFirst( + t -> { + // this doFirst is added before the info plugin, therefore it will run + // after the doFirst added by the info plugin, and we can override attributes + jarTask.getManifest() + .attributes( + Map.of( + "Build-Date", + BuildParams.getBuildDate(), + "Build-Java-Version", + BuildParams.getCompilerJavaVersion() + ) + ); + } + ); + } + ); + project.getPluginManager().withPlugin("com.github.johnrengelman.shadow", p -> { + project.getTasks() + .withType(ShadowJar.class) + .configureEach( + shadowJar -> { + /* + * Replace the default "-all" classifier with null + * which will leave the classifier off of the file name. + */ + shadowJar.getArchiveClassifier().set((String) null); + /* + * Not all cases need service files merged but it is + * better to be safe + */ + shadowJar.mergeServiceFiles(); + } + ); + // Add "original" classifier to the non-shadowed JAR to distinguish it from the shadow JAR + project.getTasks().named(JavaPlugin.JAR_TASK_NAME, Jar.class).configure(jar -> jar.getArchiveClassifier().set("original")); + // Make sure we assemble the shadow jar + project.getTasks().named(BasePlugin.ASSEMBLE_TASK_NAME).configure(task -> task.dependsOn("shadowJar")); + }); + } + private static void configureJarManifest(Project project) { project.getPlugins().withType(InfoBrokerPlugin.class).whenPluginAdded(manifestPlugin -> { manifestPlugin.add("Module-Origin", toStringable(BuildParams::getGitOrigin)); From e504625e549ef9e7715772b8bea948e5bfd486d5 Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Thu, 21 May 2020 13:34:02 -0500 Subject: [PATCH 8/8] call java configureJars --- .../java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java | 1 + 1 file changed, 1 insertion(+) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java index cefc189921e7a..10531102f5706 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java @@ -79,6 +79,7 @@ public void apply(Project project) { configureCompile(project); configureInputNormalization(project); configureTestTasks(project); + configureJars(project); configureJarManifest(project); configureJavadoc(project); }