how to capture Build Info using Gradle and Spring

2020-03-27 06:02发布

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.

4条回答
我想做一个坏孩纸
2楼-- · 2020-03-27 06:28

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

查看更多
走好不送
3楼-- · 2020-03-27 06:32

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 Console Output when running the application is shown below

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
查看更多
放荡不羁爱自由
4楼-- · 2020-03-27 06:34

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()
}
查看更多
干净又极端
5楼-- · 2020-03-27 06:47

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

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

查看更多
登录 后发表回答