|
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() |
10 | 4 |
|
11 | | -tasks.register("writeVersionNumberFile") { |
| 5 | + @Input |
| 6 | + abstract Property<String> getGitHash() |
12 | 7 |
|
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() |
16 | 10 |
|
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")) |
20 | 23 | } |
21 | | -} |
22 | 24 |
|
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 | + } |
25 | 31 | } |
| 32 | + |
| 33 | +def versionTask = tasks.register("writeVersionNumberFile", WriteVersionNumberFile) |
| 34 | +sourceSets.main.resources.srcDir(versionTask) |
0 commit comments