【问题标题】:how to capture Build Info using Gradle and Spring Boot如何使用 Gradle 和 Spring Boot 捕获构建信息
【发布时间】:2018-04-27 05:45:53
【问题描述】:

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

我找不到任何关于如何配置的文档/示例

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

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

在我的 build.gradle 文件中,我将有 version 条目,那么如何使用 Spring Boot 和 Gradle 将其放入 Java 主类中。

build.gradle

version=0.0.1-SNAPSHOT

我已经尝试添加

build.gradle

apply plugin: 'org.springframework.boot'

springBoot {    
    buildInfo() 
}

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")     
    }

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

【问题讨论】:

    标签: java spring-boot gradle build.gradle


    【解决方案1】:

    在 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";
    }
    }
    

    别忘了生成 application-build.properties

    springBoot {
        buildInfo()
    }
    

    【讨论】:

    • 在 gradle 构建中调用 buildInfo 后,利用内置的 Spring Boot 'info' Actuator (/actuator/info) 可能更容易,而不是滚动你自己的控制器方法
    • @runderworld 对于部署在独立 tomcat 中的 springboot war 文件如何做到这一点?
    【解决方案2】:

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

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

    在 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>
    

    更新

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

    gradle.properties

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

    【讨论】:

    • 感谢您的信息!你知道 springBoot buildInfo additionalProperties 方法记录在哪里吗?奇怪的是,这里显示的类似但不同的方法不起作用:docs.spring.io/spring-boot/docs/2.0.3.RELEASE/gradle-plugin/…
    • 谢谢老兄。我不知道 IDE intellij IDEA 中有这个奇怪的东西。当我在 docker 容器中运行时它工作:)
    【解决方案3】:

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

    所以要获取信息,请使用context.getBean(BuildProperties.class).getVersion();

    【讨论】:

    • 谢谢吸血鬼。我已经按照您的建议更改了代码,但现在出现异常,说明:组件需要类型为“org.springframework.boot.info.BuildProperties”的 bean,但无法找到。 - 'ProjectInfoAutoConfiguration' 中的 Bean 方法 'buildProperties' 未加载,因为 @ConditionalOnResource 没有找到资源 '${spring.info.build.location:classpath:META-INF/build-info.properties}' 我没有运行 web- application 这是一个独立的 Springboot 应用程序。
    • 我将您的回复标记为答复,因为它对我有很大帮助。如果您同意我的答案和示例代码,那么您能否也将其标记为答案,因为我无法自己标记
    • 我不确定它为什么没有加载,据我所知,这也应该适用于独立的 spring-boot 应用程序。也许您的类路径中没有该文件?除此之外,只有提问者可以将答案标记为答案。并且只有一个答案可以标记为答案。而且您应该能够将自己的答案标记为答案,它可能只需要一些时间或声誉就可以了。除此之外,请阅读并遵守stackoverflow.com/help/someone-answers。有一点是总是对有用的答案进行投票,即使您另外将它们标记为答案。
    • 找不到 build-info.properties 的一种可能性是使用 bootRun 运行项目。我不知道让 bootRun 和 buildinfo 都工作的推荐方法。
    【解决方案4】:

    在您的 Gradle 脚本中添加以下内容。它将版本正确地插入到 jar 清单中,如下所示:

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

    您的代码将能够从该 jar 清单文件中获取版本:

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

    更多详情请参考以下链接:https://github.com/akhikhl/wuff/wiki/Manifest-attributes-in-build.gradle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 2020-04-13
      • 2017-07-29
      • 2019-11-26
      • 2018-04-06
      相关资源
      最近更新 更多