Skip to content

Commit 7bc3f0e

Browse files
committed
generate a version class
1 parent 4c0d4f1 commit 7bc3f0e

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ repositories {
5555
}
5656
}
5757

58+
sourceSets {
59+
main {
60+
java {
61+
srcDirs += "${buildDir}/version/java/main"
62+
}
63+
}
64+
}
65+
66+
compileJava.dependsOn "generateVersion"
67+
68+
5869
test {
5970
useJUnitPlatform()
6071
}
@@ -156,4 +167,5 @@ dokka {
156167
outputDirectory = "$buildDir/docs/kotlin"
157168
}
158169

170+
apply plugin: VersionPlugin
159171
apply from: "${rootProject.rootDir}/gradle/publishing.gradle"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import org.gradle.api.Action
2+
import org.gradle.api.Plugin
3+
import org.gradle.api.Project
4+
5+
/**
6+
* provides a "generateVersion" task to a create a simple Version.java class:
7+
*
8+
* <pre>{@code
9+
* package io.openapiprocessor.spring;
10+
*
11+
* public class Version {
12+
* public static final String version = "${project.version}";
13+
* }
14+
* }</pre>
15+
*
16+
*
17+
* The io/openapiprocessor/spring/Version.java file is generated to:
18+
*
19+
* $(project.buildDir}/main/java
20+
*
21+
* Add it as a source directory to include it in compilation.
22+
*/
23+
class VersionPlugin implements Plugin<Project> {
24+
25+
void apply(Project project) {
26+
project.afterEvaluate (new Action<Project> () {
27+
28+
@Override
29+
void execute (Project prj) {
30+
prj.tasks.register ('generateVersion', VersionTask , new Action<VersionTask>() {
31+
32+
@Override
33+
void execute (VersionTask task) {
34+
task.targetDir = prj.buildDir
35+
task.version = prj.version
36+
}
37+
38+
})
39+
}
40+
41+
})
42+
}
43+
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import org.gradle.api.DefaultTask
2+
import org.gradle.api.tasks.Internal
3+
import org.gradle.api.tasks.OutputDirectory
4+
import org.gradle.api.tasks.TaskAction
5+
6+
import java.nio.file.Files
7+
import java.nio.file.Path
8+
import java.time.Instant
9+
10+
/**
11+
* simple task to create a Version class.
12+
*/
13+
class VersionTask extends DefaultTask {
14+
15+
/**
16+
* Target directory for the generated version class.
17+
*
18+
* Used by gradle for the up-to-date check.
19+
*/
20+
@OutputDirectory
21+
String targetDir
22+
23+
@Internal
24+
String version
25+
26+
/**
27+
* generate the version class.
28+
*/
29+
@TaskAction
30+
void generateVersion () {
31+
def path = Path.of (
32+
targetDir, "version", "main", "java", "io", "openapiprocessor", "spring")
33+
34+
Files.createDirectories(path)
35+
36+
def target = path.resolve ("Version.java")
37+
38+
target.text = """\
39+
/*
40+
* DO NOT MODIFY - this file was auto generated by buildSrc/src/main/groovy/VersionPlugin.groovy
41+
*
42+
* ${Instant.now ().toString ()}
43+
*/
44+
45+
package io.openapiprocessor.spring;
46+
47+
public class Version {
48+
public static final String version = "${version}";
49+
}
50+
51+
"""
52+
}
53+
54+
}

0 commit comments

Comments
 (0)