Skip to content

Commit

Permalink
Don't eagerly configure jar tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
chali committed Jun 19, 2020
1 parent a2f6587 commit 81d1a11
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
Expand Up @@ -23,6 +23,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.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Jar

/**
Expand All @@ -34,35 +35,36 @@ class InfoJarPropertiesFilePlugin implements Plugin<Project>, InfoReporterPlugin
void apply(Project project) {
project.plugins.withType(InfoBrokerPlugin) { manifestPlugin ->
InfoPropertiesFilePlugin propFilePlugin = project.plugins.apply(InfoPropertiesFilePlugin) as InfoPropertiesFilePlugin
InfoPropertiesFile manifestTask = propFilePlugin.getManifestTask()
TaskProvider<InfoPropertiesFile> manifestTask = propFilePlugin.getManifestTask()

//TODO: this should probably be using lazy API but needs some refactoring
project.afterEvaluate {
project.tasks.withType(Jar) { 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 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
def taskName = jarTask.name.capitalize()
File propertiesFile = new File(project.buildDir, "properties_for_${taskName}/temp.properties")
Task prepareFile = project.tasks.create("createPropertiesFileFor${taskName}") { Task task ->
task.outputs.file(propertiesFile)
task.doLast {
//Task action is intentionally creating empty file.
propertiesFile.text = ""
}
}
//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 = ""
}
}

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.outputs).rename("temp.properties", manifestTask.propertiesFile.name)
}
jarTask.doFirst {
//when we are after all caching decisions we fill the file with all the data
new PropertiesWriter().writeProperties(propertiesFile, project)
}
//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 = ""
}
}

Expand Down
Expand Up @@ -22,34 +22,36 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.plugins.BasePluginConvention
import org.gradle.api.tasks.TaskProvider

/**
* Write contents of the manifest to a file, as a property file.
*/
class InfoPropertiesFilePlugin implements Plugin<Project>, InfoReporterPlugin {

InfoPropertiesFile manifestTask
TaskProvider<InfoPropertiesFile> manifestTask

void apply(Project project) {

project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin basePlugin ->

manifestTask = project.tasks.create('writeManifestProperties', InfoPropertiesFile)
manifestTask.conventionMapping.map('propertiesFile') {
// A little clunky, because there is no way to say, "If there's no convention, run this". E.g.
// timing is improtant here, this should be running after the BasePlugin is applied if it's going
// to be applied.
if (project.plugins.hasPlugin(BasePlugin)) {
BasePluginConvention baseConvention = project.getConvention().getPlugin(BasePluginConvention)
new File(project.buildDir, "manifest/${baseConvention.archivesBaseName}.properties")
} else {
new File(project.buildDir, "manifest/info.properties")
manifestTask = project.tasks.register('writeManifestProperties', InfoPropertiesFile) { task ->
task.conventionMapping.map('propertiesFile') {
// A little clunky, because there is no way to say, "If there's no convention, run this". E.g.
// timing is improtant here, this should be running after the BasePlugin is applied if it's going
// to be applied.
if (project.plugins.hasPlugin(BasePlugin)) {
BasePluginConvention baseConvention = project.getConvention().getPlugin(BasePluginConvention)
new File(project.buildDir, "manifest/${baseConvention.archivesBaseName}.properties")
} else {
new File(project.buildDir, "manifest/info.properties")
}
}
}
}
}

InfoPropertiesFile getManifestTask() {
TaskProvider<InfoPropertiesFile> getManifestTask() {
assert manifestTask != null, 'InfoPropertiesFilePlugin has to be applied first'
return manifestTask
}
Expand Down
Expand Up @@ -31,7 +31,7 @@ class InfoPropertiesFilePluginSpec extends ProjectSpec {
project.plugins.apply(InfoJavaPlugin)
def infoPropertiesFilePlugin = project.plugins.apply(InfoPropertiesFilePlugin)
project.evaluate() // For Java plugin compatibility fields
InfoPropertiesFile manifestTask = infoPropertiesFilePlugin.getManifestTask()
InfoPropertiesFile manifestTask = infoPropertiesFilePlugin.getManifestTask().get()

then:
manifestTask.getPropertiesFile() == new File(projectDir, 'build/manifest/ensure-reporter-is-doing-work.properties')
Expand Down

0 comments on commit 81d1a11

Please sign in to comment.