Accessing Build Properties at Runtime with Spring Boot
Access build properties at runtime.
Spring boot plugin to provide access to build properties:
- Gradle:
springBoot {
buildInfo()
}
- Maven:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
This causes the build to also run the build-info goal, which generates build metadata (${project.build.outputDirectory}/META-INF/build-info.properties
). The location of this file is configurable with the outputFile parameter. This metadata is available in a spring-wired bean BuildProperties.
Available properties:
- group
- artifactId
- name
- build timestamp
- version
Custom properties:
- Gradle:
springBoot {
buildInfo {
properties {
additional = [
'property.name': 'property value'
]
}
}
}
- Maven:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
<additionalProperties>
<property.name>property.value</property.name>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
Accessing custom properties
buildProperties.getProperty("property.name")
Actuator
Spring actuator allows access to these build properties at the info endpoint.