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

InfoJarPropertiesFilePlugin: remove the need for renaming temp.properties file so the jar tasks are cacheable #93

Merged
merged 1 commit into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
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 @@ -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