How to Retrieve Spring Boot Project Version via API and Build Info
This article explains several ways to obtain a Spring Boot application's version number at runtime, including reading the MANIFEST.MF, loading pom.properties, using configuration files, and leveraging Spring Boot's built‑in BuildProperties via the spring‑boot‑maven‑plugin.
When you need the version number of a Spring Boot project, hard‑coding it is the simplest but not ideal. You can extract the version defined in pom.xml through the generated JAR file.
Most Spring Boot projects are packaged as JARs, so WAR/EAR files are not considered here.
Jar file secrets
Inspecting the META-INF directory of a Spring Boot JAR reveals MANIFEST.MF:
Manifest-Version: 1.0
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Implementation-Title: spring-boot-version
Implementation-Version: 1.0.23
Spring-Boot-Version: 2.4.5
...The Implementation-Version entry contains the version 1.0.23, which can be accessed in code:
String version = this.getClass().getPackage().getImplementationVersion();Running the application from an IDE returns null, while launching with java -jar correctly yields version = 1.0.23, because the IDE does not use the JAR.
Another file, pom.properties, also holds the version:
artifactId=spring-boot-version
groupId=cn.felord
version=1.0.23You can read this properties file directly, but it still requires the JAR.
Reading from configuration files
Maven can filter resources so that the project version is injected into a properties file. For example, in application.properties you can write: application.version=${project.version} Or in application.yml:
application:
version: '@project.version@'This approach does not depend on the JAR and is straightforward to use.
Spring Boot built‑in support
Spring Boot provides an auto‑configuration class ProjectInfoAutoConfiguration that creates a BuildProperties bean when META-INF/build-info.properties is present. The spring-boot-maven-plugin can generate this file with: mvn spring-boot:build-info Configure the plugin in pom.xml:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>After building, BuildProperties provides fields such as version, group, artifact, name, and build time, e.g.:
{
"version":"1.0.23",
"group":"cn.felord",
"artifact":"spring-boot-version",
"name":"spring-boot-version",
"time":{"epochSecond":1620664643,"nano":591000000}
}Summary
The article presents multiple techniques to programmatically obtain a Spring Boot project's version information, useful for monitoring, release auditing, and DevOps scenarios, including reading the JAR manifest, loading pom properties, using filtered configuration files, and leveraging Spring Boot's built‑in BuildProperties support.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
