Getting ArtifactId and Version in Spring Boot Starter
The other answer is completely correct. Just for others finding this question in case you are using Gradle instead of Maven:
Generating build info is as simple as adding this to your build.gradle
file:
plugins {
id 'org.springframework.boot' version '<your-boot-version>.RELEASE'
}
// ...
springBoot {
buildInfo()
}
And if you want to pass custom properties:
springBoot {
buildInfo {
properties {
additional = [
'property.name': 'property value',
'other.property': 'different.value'
]
}
}
}
Then the usage in Java code is the same using BuildProperties
. You can find more info about the plugin in this guide.
After a lot of effort, I found a surprisingly simple answer. This is how spring-boot-actuator gets the information.
The Spring Boot Maven plugin comes equipped with a build-info goal. As long as this goal is triggered in the main project Spring has a BuildProperties class you can wire in for the information.
<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>
You can access the properties in your starter like:
@Autowired
BuildProperties buildProperties;
...
buildProperties.getArtifact();
buildProperties.getVersion();
You can even specify additional properties from the plugin. See the plugin documentation for more details: https://docs.spring.io/spring-boot/docs/current/maven-plugin/build-info-mojo.html
Unfortunately I never quite got to fully understand why I could not access the correct manifest, but this should help anyone else trying to solve this problem.