Skip to content

Instantly share code, notes, and snippets.

@rdp
Created April 15, 2019 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdp/789e5c4cb6fda1841e86e722a2d152d4 to your computer and use it in GitHub Desktop.
Save rdp/789e5c4cb6fda1841e86e722a2d152d4 to your computer and use it in GitHub Desktop.
How to get the build version by having maven create a file with the "build.timestamp" etc. in it:
You can set it yourself by filtering a "resource" properties file that declares it, then read it as a resource, a la:
String retrieveVersionFromResource() throws IOException {
try (InputStream br = getClass().getResourceAsStream("/auto-version.txt")) { // maven pom.xml specifies to setup auto-version.txt with some values for us
Properties props = new Properties();
props.load(br);
return "build_version=" + props.get("project_version") + " " + props.get("build_date");
}
}
where a file src/main/resources/auto-filtered of name "auto-version.txt" or "auto-version.props" or whatever you want to call it, exists
https://stackoverflow.com/a/52939311/32453
in the pom build section:
<resources>
<resource>
<directory>src/main/resources-filtered
</directory> <!-- to get "current version" into resource properties file -->
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory> <!-- apparently have to call it out now? lame -->
</resource>
</resources>
contents (spring boot style project) would be:
build_date=@maven.build.timestamp@
project_version=@project.version@
contents (non springboot style):
build_date=${maven.build.timestamp}
project_version=${project.version}
ref:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment