Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to capture Build Info using Gradle and Spring Boot

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.

build.gradle

version=0.0.1-SNAPSHOT

I've tried adding

build.gradle

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.

like image 360
robbie70 Avatar asked Nov 29 '22 22:11

robbie70


1 Answers

Spring Boot auto-configures a BuildProperties bean with the information generated by buildInfo().

So to get the information use context.getBean(BuildProperties.class).getVersion();.

like image 83
Vampire Avatar answered Dec 29 '22 04:12

Vampire