Skip to content

Commit

Permalink
InfoJarPropertiesFilePlugin: remove the need for renaming temp.proper…
Browse files Browse the repository at this point in the history
…ties file so the jar tasks are cacheable
  • Loading branch information
rpalcolea committed Sep 30, 2021
1 parent 2cee004 commit 550a4bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,45 +38,47 @@ import org.gradle.normalization.MetaInfNormalization
class InfoJarPropertiesFilePlugin implements Plugin<Project>, InfoReporterPlugin {

void apply(Project project) {
project.plugins.withType(InfoBrokerPlugin) { manifestPlugin ->
InfoPropertiesFilePlugin propFilePlugin = project.plugins.apply(InfoPropertiesFilePlugin) as InfoPropertiesFilePlugin
TaskProvider<InfoPropertiesFile> manifestTask = propFilePlugin.getManifestTask()
project.plugins.withType(JavaBasePlugin) {
project.plugins.withType(InfoBrokerPlugin) { manifestPlugin ->
InfoPropertiesFilePlugin propFilePlugin = project.plugins.apply(InfoPropertiesFilePlugin) as InfoPropertiesFilePlugin
TaskProvider<InfoPropertiesFile> 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<Task> 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<Task> 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) {
Expand All @@ -88,4 +91,4 @@ class InfoJarPropertiesFilePlugin implements Plugin<Project>, InfoReporterPlugin
}
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class InfoJarManifestPluginLauncherSpec extends IntegrationSpec {
buildFile << """
${applyPlugin(InfoBrokerPlugin)}
${applyPlugin(BasicInfoPlugin)}
${applyPlugin(InfoJarPropertiesFilePlugin)}
${applyPlugin(InfoJarManifestPlugin)}
apply plugin: 'java'
Expand Down

0 comments on commit 550a4bc

Please sign in to comment.