I believe that what you are looking for generates this version number during build (usually using build tools like Ant, Maven or Gradle) as part of the build task chain.
I believe that a fairly common approach is to either put the version number in Manifest.mf from a prepared JAR and then read it or create a file that is part of the created JAR that can be read by your application.
Another solution would be to use Spring Boot banner settings described here: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-banner However, this will only allow you to change the spring-boot banner.
I also believe that Spring Boot provides the product version installed in Manifest.MF of your application. To do this, you need to make sure that the Implementation-Version attribute of the manifest is set.
Custom solution for access anywhere in the code
Suppose you would like to have a version.properties file in src/main/resources that contains information about your version. It will contain placeholders instead of actual values so that these placeholders can be expanded during build time.
version=${prodVersion} build=${prodBuild} timestamp=${buildTimestamp}
Now that you have such a file, you need to fill it with actual data. I use Gradle, so I have to make sure that the processResources task, which starts automatically for assembly, extends resources. Something like this should do the trick in the build.gradle file for Git-restricted code:
import org.codehaus.groovy.runtime.* import org.eclipse.jgit.api.* def getGitBranchCommit() { try { def git = Git.open(project.file(project.getRootProject().getProjectDir())); def repo = git.getRepository(); def id = repo.resolve(repo.getFullBranch()); return id.abbreviate(7).name() } catch (IOException ex) { return "UNKNOWN" } } processResources { filesMatching("**/version.properties") { expand ( "prodVersion": version, "prodBuild": getGitBranchCommit(), "buildTimestamp": DateGroovyMethods.format(new Date(), 'yyyy-MM-dd HH:mm') ) } } processResources.outputs.upToDateWhen{ false }
The following happens in the code:
- We have defined a function that can accept a build number from VCS (in this case Git). The commit hash is limited to 7 characters.
- We set up the
processResources task to handle version.properties and populate it with our variables. prodVersion is taken from the version of the Gradle project. Usually it is installed as version in the gradle.properties file (part of the general assembly is customized). - As a last step, we guarantee that it is always updated (Gradle has some mechanics to determine if files should be processed
Given that you are on SVN, you will need to use the getSvnBranchCommit() method. For example, you can use SVNKit or similar for this.
The last thing that is missing right now is reading the file for use in your application.
This can be achieved by simply reading the pathpath resource and parsing it in java.util.Properties . You can do this one more step and, for example, create access methods for each field, for example getVersion() , getBuild() , etc.
Hope this helps a bit (even if it cannot be 100% inapplicable)