From 550a4bc23e0e4b49934d89293efd200e410e5bf8 Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Thu, 30 Sep 2021 14:18:15 -0700 Subject: [PATCH] InfoJarPropertiesFilePlugin: remove the need for renaming temp.properties file so the jar tasks are cacheable --- .../InfoJarPropertiesFilePlugin.groovy | 67 ++++++++++--------- .../InfoJarManifestPluginLauncherSpec.groovy | 1 + 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/main/groovy/nebula/plugin/info/reporting/InfoJarPropertiesFilePlugin.groovy b/src/main/groovy/nebula/plugin/info/reporting/InfoJarPropertiesFilePlugin.groovy index c05e949..c903583 100644 --- a/src/main/groovy/nebula/plugin/info/reporting/InfoJarPropertiesFilePlugin.groovy +++ b/src/main/groovy/nebula/plugin/info/reporting/InfoJarPropertiesFilePlugin.groovy @@ -26,6 +26,7 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.Task import org.gradle.api.file.CopySpec +import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.tasks.TaskProvider import org.gradle.api.tasks.bundling.Jar import org.gradle.normalization.MetaInfNormalization @@ -37,45 +38,47 @@ import org.gradle.normalization.MetaInfNormalization class InfoJarPropertiesFilePlugin implements Plugin, InfoReporterPlugin { void apply(Project project) { - project.plugins.withType(InfoBrokerPlugin) { manifestPlugin -> - InfoPropertiesFilePlugin propFilePlugin = project.plugins.apply(InfoPropertiesFilePlugin) as InfoPropertiesFilePlugin - TaskProvider manifestTask = propFilePlugin.getManifestTask() + project.plugins.withType(JavaBasePlugin) { + project.plugins.withType(InfoBrokerPlugin) { manifestPlugin -> + InfoPropertiesFilePlugin propFilePlugin = project.plugins.apply(InfoPropertiesFilePlugin) as InfoPropertiesFilePlugin + TaskProvider manifestTask = propFilePlugin.getManifestTask() - //we cannot use the right module name because touching manifest task to early causes incorrect name computation - //temp.properties is renamed later when placed into jar - File propertiesFile = new File(project.buildDir, "properties_for_jar/temp.properties") - TaskProvider prepareFile = project.tasks.register("createPropertiesFileForJar") { Task task -> - task.outputs.file(propertiesFile) - task.doLast { - //Task action is intentionally creating empty file. - propertiesFile.text = "" + File propertiesFile = new File(project.buildDir, "properties_for_jar/${manifestTask.get().propertiesFile.name}") + TaskProvider prepareFile = project.tasks.register("createPropertiesFileForJar") { Task task -> + task.outputs.file(propertiesFile) + task.doLast { + //Task action is intentionally creating empty file. + propertiesFile.text = "" + } } - } - project.tasks.withType(Jar).configureEach { Jar jarTask -> - //explicit dependency on original task to keep contract that `jar` task invocation will produce properties file - //even this file is actually not bundled into jar - jarTask.dependsOn(manifestTask) + project.tasks.withType(Jar).configureEach { Jar jarTask -> + //explicit dependency on original task to keep contract that `jar` task invocation will produce properties file + //even this file is actually not bundled into jar + jarTask.dependsOn(manifestTask) - //we link the output file from the task to the spec to add it into jar, but the file is empty, it will - //help to ignore changes in its content for caching - jarTask.metaInf { CopySpec spec -> - spec.from(prepareFile).rename("temp.properties", manifestTask.get().propertiesFile.name) - } - jarTask.doFirst { - //when we are after all caching decisions we fill the file with all the data - new PropertiesWriter().writeProperties(propertiesFile, project) - } - jarTask.doLast { - //we need to cleanup file in case we got multiple jar tasks - propertiesFile.text = "" + //we link the output file from the task to the spec to add it into jar, but the file is empty, it will + //help to ignore changes in its content for caching + jarTask.metaInf { CopySpec spec -> + spec.from(prepareFile) + } + + jarTask.doFirst { + //when we are after all caching decisions we fill the file with all the data + new PropertiesWriter().writeProperties(propertiesFile, project) + } + jarTask.doLast { + //we need to cleanup file in case we got multiple jar tasks + propertiesFile.text = "" + } } - } - if (GradleKt.versionGreaterThan(project.gradle, "6.6-rc-1")) { - configureMetaInfNormalization(project) + if (GradleKt.versionGreaterThan(project.gradle, "6.6-rc-1")) { + configureMetaInfNormalization(project) + } } } + } private static void configureMetaInfNormalization(Project project) { @@ -88,4 +91,4 @@ class InfoJarPropertiesFilePlugin implements Plugin, InfoReporterPlugin } }) } -} \ No newline at end of file +} diff --git a/src/test/groovy/nebula/plugin/info/reporting/InfoJarManifestPluginLauncherSpec.groovy b/src/test/groovy/nebula/plugin/info/reporting/InfoJarManifestPluginLauncherSpec.groovy index 87fe7ec..4988aa2 100644 --- a/src/test/groovy/nebula/plugin/info/reporting/InfoJarManifestPluginLauncherSpec.groovy +++ b/src/test/groovy/nebula/plugin/info/reporting/InfoJarManifestPluginLauncherSpec.groovy @@ -231,6 +231,7 @@ class InfoJarManifestPluginLauncherSpec extends IntegrationSpec { buildFile << """ ${applyPlugin(InfoBrokerPlugin)} ${applyPlugin(BasicInfoPlugin)} + ${applyPlugin(InfoJarPropertiesFilePlugin)} ${applyPlugin(InfoJarManifestPlugin)} apply plugin: 'java'