如何使用Gradle和Spring Boot捕获构建信息

rob*_*e70 2 java gradle build.gradle spring-boot

我试图version使用Spring Boot和Gradle 访问构建信息值,例如在Java主应用程序中。

我找不到任何文档/有关如何配置

  • build.gradle
  • application.yml (如果需要的话)
  • Java main class

有人可以帮忙提供上述文件的小代码示例。

在我的build.gradle文件中,将包含version条目,因此如何使用Spring Boot和Gradle 将其输入Java主类。

build.gradle

version=0.0.1-SNAPSHOT
Run Code Online (Sandbox Code Playgroud)

我尝试添加

build.gradle

apply plugin: 'org.springframework.boot'

springBoot {    
    buildInfo() 
}
Run Code Online (Sandbox Code Playgroud)

buildInfo()在Intellij中无法将其识别为关键字

在我的Java主类中,我具有以下内容:

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")     
    }
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此命令时-的输出env.getProperty("version")显示为null

rob*_*e70 6

我现在设法让它工作 - 使用吸血鬼在下面提供的帮助指针和其他一些来源。关键是将执行器类添加到项目依赖项中。注意:Intellj 似乎无法识别 springBoot 标记中的 buildInfo() - 但它确实运行正常 - 所以不要推迟。

构建.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"
Run Code Online (Sandbox Code Playgroud)

我的示例应用程序

@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") + ">");
    }

}
Run Code Online (Sandbox Code Playgroud)

在 Intellj 中运行应用程序时控制台输出的屏幕截图 运行应用程序时的控制台输出如下所示

粘贴输出以及柜面图像不显示

> 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>
Run Code Online (Sandbox Code Playgroud)

更新

在与我的同事一起审查后,我们决定将一些构建属性(例如version(以上)从build.gradle文件中移到gradle.properties文件中。这使我们可以更清晰地分离构建细节和属性。当您运行 Gradle build 时,它会自动提取这些值,并且它们在 Java 主类的 BuildProperties bean 中可用,如上例所示。

gradle.properties

group=com.xxx.examplesource
version=0.0.1-SNAPSHOT 
gradleVersion=3.3
Run Code Online (Sandbox Code Playgroud)


Vam*_*ire 5

Spring Boot BuildProperties使用生成的信息自动配置Bean buildInfo()

因此要获取信息使用context.getBean(BuildProperties.class).getVersion();