Getting the Gradle.build version into Spring Boot
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Spring Boot can expose the project version defined in build.gradle through the /actuator/info endpoint or by injecting it into application properties. The standard approach is to use the spring-boot-gradle-plugin's build info task, which generates a build-info.properties file at build time. This file is automatically picked up by Spring Boot's BuildProperties bean, making the version available for injection throughout the application.
Method 1: Build Info with Spring Boot Plugin (Recommended)
Configure build.gradle
This generates build/resources/main/META-INF/build-info.properties containing:
Inject BuildProperties
Method 2: Expose via Actuator Info Endpoint
Spring Boot Actuator automatically picks up build-info.properties:
Access GET /actuator/info:
Method 3: Property Expansion in application.properties
Gradle can expand properties directly in Spring Boot's configuration files:
Kotlin DSL (build.gradle.kts)
Method 4: Custom Build Info Properties
Add custom properties to the build info:
Method 5: Manifest File
Write the version to MANIFEST.MF and read it at runtime:
This works but is less reliable than BuildProperties because the manifest is only available when running from a packaged JAR, not during development with gradle bootRun.
Dynamic Version from Git
This sets the version automatically based on Git tags, including commit count and hash for non-tagged commits.
Common Pitfalls
BuildPropertiesbean not found: If you getNo qualifying bean of type 'BuildProperties', you forgotspringBoot { buildInfo() }inbuild.gradle. Without this, nobuild-info.propertiesis generated and Spring cannot create the bean.- Property expansion breaking YAML:
expand(project.properties)inprocessResourcesreplaces${...}tokens in all resource files, includingapplication.yml. This conflicts with Spring's ${}placeholder syntax. Use@...@delimiters instead: setapp.version=@version@and configureexpandto use@delimiters. - Stale build info after version change: The
build-info.propertiesfile is generated at build time. If you change the version inbuild.gradlebut do not rebuild (gradle clean build), the old version persists in the output. Always clean build for version changes. BuildPropertiesunavailable during tests: Unit tests that do not run the full Spring Boot context may not haveBuildPropertiesavailable. Use@ConditionalOnBean(BuildProperties.class)or provide a default value with@Value("${build.version:unknown}").- Manifest version null in development:
getClass().getPackage().getImplementationVersion()returnsnullwhen running withgradle bootRunor from an IDE because the class is not loaded from a JAR. UseBuildPropertiesinstead for reliable access during development.
Summary
- Use
springBoot { buildInfo() }inbuild.gradleto generatebuild-info.propertiesat build time - Inject
BuildPropertiesto access version, artifact, group, and build time anywhere in the application - Enable Spring Boot Actuator to expose build info via
GET /actuator/infoautomatically - Add custom properties to build info with the
additionalmap inbuildInfo { properties { ... } } - Use Git-based versioning plugins for automatic version derivation from tags
- Prefer
BuildPropertiesover manifest-based approaches for reliability during development

