Skip to content

Commit b3225f8

Browse files
committed
fix: version file was not always generated on first jar generation
This was mostly due to the fact that this task was writing directly to the output dir which is replaced by a later task. This change makes this task generate the file in another location and makes the sourceSet use this task output as source dir.
1 parent bd72002 commit b3225f8

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

gradle/version.gradle

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
def getGitHash = {
2-
->
3-
def stdout = new ByteArrayOutputStream()
4-
exec {
5-
commandLine 'git', 'rev-parse', '--short', 'HEAD'
6-
standardOutput = stdout
7-
}
8-
return stdout.toString().trim()
9-
}
1+
abstract class WriteVersionNumberFile extends DefaultTask {
2+
@Input
3+
abstract Property<String> getVersion()
104

11-
tasks.register("writeVersionNumberFile") {
5+
@Input
6+
abstract Property<String> getGitHash()
127

13-
def versionFile = file("${sourceSets.main.output.resourcesDir}/${project.name}.version")
14-
inputs.property "version", project.version
15-
outputs.file versionFile
8+
@OutputDirectory
9+
abstract DirectoryProperty getOutputDirectory()
1610

17-
doFirst {
18-
assert versionFile.parentFile.mkdirs() || versionFile.parentFile.directory
19-
versionFile.text = "${project.version}~${getGitHash()}"
11+
WriteVersionNumberFile() {
12+
// Set conventions (defaults)
13+
this.version.convention(project.provider { project.version.toString() })
14+
this.gitHash.convention(project.provider {
15+
def stdout = new ByteArrayOutputStream()
16+
project.exec {
17+
commandLine 'git', 'rev-parse', '--short', 'HEAD'
18+
standardOutput = stdout
19+
}
20+
return stdout.toString().trim()
21+
})
22+
this.outputDirectory.convention(project.layout.buildDirectory.dir("generated/version"))
2023
}
21-
}
2224

23-
tasks.withType(JavaCompile).configureEach {
24-
dependsOn "writeVersionNumberFile"
25+
@TaskAction
26+
void writeVersionFile() {
27+
def versionFile = outputDirectory.file("${project.name}.version").get().asFile
28+
versionFile.parentFile.mkdirs()
29+
versionFile.text = "${version.get()}~${gitHash.get()}"
30+
}
2531
}
32+
33+
def versionTask = tasks.register("writeVersionNumberFile", WriteVersionNumberFile)
34+
sourceSets.main.resources.srcDir(versionTask)

0 commit comments

Comments
 (0)