We have a build.gradle
where the version
is defined in it. I need to implement a version endpoint ( like /version
) to get the version of the project. This version
property in build.gradle
has been there for a long time, I can't move it to the project.properties
file. How can I access this version
's values from my Java code?
Asked
Active
Viewed 1,916 times
3

Milindu Sanoj Kumarage
- 2,714
- 2
- 31
- 54
-
1Append the value you have in gradle to your properties file before the assemble task or include the version in the manifest file and read it using getResources – Boaz Aug 03 '18 at 09:37
2 Answers
6
There are many ways with which you can "plant" information into your code
First you can use the manifest file, and read from it
jar {
manifest {
attributes(
"lib-version": version
}
}
With conjunction to Reading my own Jar's Manifest
The other option is to add that info the your property files before the jar task
task('addVersion') {
doLast {
//append the version here, see example
file("src/main/resources/props.properties").append("version=$version")
}
}
jar.dependsOn(addVersion)

Boaz
- 4,549
- 2
- 27
- 40
1
One of our project needs not only the version information but also build time etc. We use a copy task with template substitution.
task updateVersions(type: Copy) {
Properties props = new Properties()
props.load(new FileInputStream(file('build.properties')))
props.put("buildtime", new Date().format("dd MMM yyyy hh:mm aa"))
props.put("version", "${version}")
from project(':main').file('Version.tmpl')
into file('src/main/java').path
expand(props)
rename('Version.tmpl', 'Version.java')
}

Dakshinamurthy Karra
- 5,353
- 1
- 17
- 28