Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 6365bce

Browse files
committed
generate version class
1 parent adeacba commit 6365bce

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ check.dependsOn testInt
6868
sourceSets {
6969
main {
7070
java {
71-
srcDir 'build/antlr'
71+
srcDirs 'build/antlr', "${buildDir}/version"
7272
}
7373
}
7474
}
@@ -89,6 +89,7 @@ compileKotlin {
8989
}
9090

9191
compileKotlin.dependsOn generateGrammarSource
92+
compileKotlin.dependsOn "generateVersion"
9293

9394
compileTestKotlin {
9495
kotlinOptions.jvmTarget = "1.8"
@@ -194,5 +195,6 @@ nexusPublishing {
194195
}
195196

196197

198+
apply plugin: VersionPlugin
197199
apply from: "${rootProject.rootDir}/gradle/publishing.gradle"
198200
apply from: "${rootProject.rootDir}/gradle/publishing.tasks.gradle.kts"
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.core;
10+
*
11+
* public class Version {
12+
* public static final String version = "${project.version}";
13+
* }
14+
* }</pre>
15+
*
16+
*
17+
* The io/openapiprocessor/core/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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 (targetDir, "version", "io", "openapiprocessor", "core")
32+
Files.createDirectories(path)
33+
34+
def target = path.resolve ("Version.java")
35+
36+
target.text = """\
37+
/*
38+
* DO NOT MODIFY - this file was auto generated by buildSrc/src/main/groovy/VersionPlugin.groovy
39+
*
40+
* ${Instant.now ().toString ()}
41+
*/
42+
43+
package io.openapiprocessor.core;
44+
45+
public class Version {
46+
public static final String version = "${version}";
47+
}
48+
49+
"""
50+
}
51+
52+
}

0 commit comments

Comments
 (0)