Java Spring Boot project version

Posted on

Question :

I need to get the value of the version in my spring boot project to create a service that returns this value, the value in question would be version in build.gradle.

group = ''
version = '0.0.2'
sourceCompatibility = 1.8

I have already tried several forms such as:


below is my gradle:

buildscript {

ext {
    springBootVersion = '1.5.9.RELEASE'
repositories {
dependencies {
    classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"

But this only brings me the spring boot version.
Anyone who can help me, thank you.


Answer :

Yes, it is possible. Spring has a bean named BuildProperties , which has some project information such as the version number.

For this, you need the following:

a) If the project is Maven, add the following to pom.xml :


b) If the project is Gradle, add the following to build.gradle :

springBoot {

When building the project, Spring will generate the buildresourcesmainMETA-INF path in the path and make the bean BuildProperties available for injection, which, among other methods, has getVersion() , which returns the version.

For code this is, however, if you run the project through IDE (which is most likely), you may get a bean error not found. To resolve this, if your IDE is Intellij, go to Edit > Settings > Build, Execution, Deployment > Gradle (ou Maven, depende do seu projeto) > Runner and click the Delegate IDE build / run actions to gradle (or maven) option. This is because the Gradle / Maven task that generates the file is bootBuildInfo , which is not the default task Intellij uses, hence the error.

When running System.out.println(build.getVersion()); on a test project following the steps I described, I received as output the version that is informed in the project build.gradle :


A useful article can be found here .


Leave a Reply

Your email address will not be published. Required fields are marked *