I am trying to get access to build info values such as version
in my Java main application using Spring Boot and Gradle.
I can't find any documentation / examples of how to configure the
build.gradle
application.yml
(if required)Java main class
could someone please help with a small code example for the above files.
In my build.gradle
file I will have the version
entry, so how to get this into Java main class using Spring Boot and Gradle.
version=0.0.1-SNAPSHOT
I've tried adding
apply plugin: 'org.springframework.boot'
springBoot {
buildInfo()
}
but the buildInfo()
isn't recognised as a keyword in Intellij
In my Java main class I have the following:
public class MyExampleApplication implements CommandLineRunner {
@Autowired
private ApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(MyExampleApplication.class, args);
}
@Override
public void run(String[] args) throws Exception{
Environment env = (Environment) context.getBean("environment");
displayInfo(env);
}
private static void displayInfo(Environment env) {
log.info("build version is <" + env.getProperty("version")
}
But when I run this - the output from env.getProperty("version")
is showing as null
.
Spring Boot auto-configures a BuildProperties
bean with the information generated by buildInfo()
.
So to get the information use context.getBean(BuildProperties.class).getVersion();
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With