how to capture Build Info using Gradle and Spring Boot

孤街醉人 提交于 2019-12-01 11:08:20

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

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

I managed to get it working now - using the help pointer that Vampire gave below and some other sources. The key was adding the actuator class to the project dependency. Note: Intellj doesn't seem to recognise buildInfo() in the springBoot tag - but it does run ok - so don't be put off.

build.gradle

buildscript {
   ext {
      springBootVersion = '1.5.6.RELEASE'
      gradleVersion = '3.3'
   }

   repositories {
      mavenLocal()
      maven { url "http://cft-nexus.ldn.xxxxxxxxx.com:8081/nexus/content/groups/public/" }
                }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
                }
            }

        apply plugin: 'application'
        apply plugin: 'java'
        apply plugin: 'eclipse'
        apply plugin: 'idea'
        apply plugin: 'org.springframework.boot'

    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = 1.8

    springBoot{
        buildInfo {
            additionalProperties = [
                    'foo': 'bar'
            ]
        }
    }

  compile "org.springframework.boot:spring-boot-starter-actuator"

MyExampleApplication

@Slf4j
@EnableIntegration
@EnableLoaderApplication
@SpringBootApplication
@EnableDiscoveryClient
public class MyExampleApplication implements CommandLineRunner {

    private static final String SYSTEM_NAME_INFO = "My Example Application";
    private static final String VERSION="0.0.1";

    @Autowired
    private ApplicationContext context;

    public static void main(String[] args) {
        SpringApplication.run(MyExampleApplication.class, args);
    }

    @Override
    public void run(String[] args) throws Exception{
        BuildProperties buildProperties = context.getBean(BuildProperties.class);
        displayInfo(buildProperties);
    }

    private static void displayInfo(BuildProperties buildProperties) {
        log.info("build version is <" + buildProperties.getVersion() + ">");
    log.info("value for custom key 'foo' is <" + buildProperties.get("foo") + ">");
    }

}

Screenshot of Console output when running the Application in Intellj

pasting the output as well incase the image doesn't display

> 2017-11-14 14:35:47.330  INFO 22448 --- [           main]
> o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase
> 2147483647 2017-11-14 14:35:47.448  INFO 22448 --- [           main]
> s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s):
> 8780 (http) 2017-11-14 14:35:47.451  INFO 22448 --- [           main]
> c.u.o.metrics.MyExampleApplication         : build version is
> <0.0.1-SNAPSHOT> 2017-11-14 14:35:47.451  INFO 22448 --- [          
> main] c.u.o.myexample.MyExampleApplication         : value for custom key
> 'foo' is <bar>

UPDATE

After reviewing this with my colleague we decided to move the some of the build properties, e.g. version (above) out of the build.gradle file and into gradle.properties file. This gives us a cleaner separation for build details and properties. When you run Gradle build it automatically pulls these values in and they are available in the BuildProperties bean in the Java main class as shown in the example above.

gradle.properties

group=com.xxx.examplesource
version=0.0.1-SNAPSHOT 
gradleVersion=3.3
akshaya pandey

add following to your Gradle script.It inserts the version into the jar manifest correctly, as shown here:

version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version
    }
}

Your code will be able to pick up the version from that jar manifest file:

public class BuildVersion {
    public static String getBuildVersion(){
        return BuildVersion.class.getPackage().getImplementationVersion();
    }
}

Refer the link below for more details: https://github.com/akhikhl/wuff/wiki/Manifest-attributes-in-build.gradle

Easy way to get version number in Spring boot

@Controller
@RequestMapping("/api")
public class WebController {

private final BuildProperties buildProperties;

public WebController(BuildProperties properties) {
    buildProperties = properties;
}

@GetMapping("/test")
public String index() {

    System.out.println(buildProperties.getVersion());
    System.out.println(buildProperties.getArtifact());
    System.out.println(buildProperties.getGroup());
    System.out.println(buildProperties.getName());
    System.out.println(buildProperties.getTime());

    return "index";
}
}

And dont forget generate application-build.properties

springBoot {
    buildInfo()
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!