【问题标题】:Getting the Gradle.build version into Spring Boot将 Gradle.build 版本导入 Spring Boot
【发布时间】:2016-04-30 00:26:33
【问题描述】:

我正在尝试在视图中显示我的 Spring Boot 应用程序的应用程序版本。我确定我可以访问此版本信息,但我不知道如何。

我尝试关注此信息:https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html,并将其放入我的application.properties

info.build.version=${version}

然后在我的控制器中加载它@Value("${version.test}"),但这不起作用,我只会收到如下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in string value "${version}"

关于将我的应用程序版本、spring boot 版本等信息输入控制器的正确方法有什么建议吗?

【问题讨论】:

  • 您想从您的 Spring Boot 应用程序中以编程方式访问 gradle 构建脚本变量(例如版本)?
  • @Eric 你终于做到了吗?

标签: java spring gradle spring-boot


【解决方案1】:

你也可以在build.gradle添加这个:

springBoot {    
    buildInfo() 
}

然后,你可以使用BuildProperties bean:

@Autowired
private BuildProperties buildProperties;

并通过buildProperties.getVersion()获取版本

【讨论】:

【解决方案2】:

作为described in the reference documentation,您需要指示Gradle 处理您的应用程序资源,以便它将${version} 占位符替换为项目的版本:

processResources {
    expand(project.properties)
}

为了安全起见,您可能希望缩小范围,以便只处理 application.properties

processResources {
    filesMatching('application.properties') {
        expand(project.properties)
    }
}

现在,假设您的资源被命名为 info.build.version,它将通过@Value 提供:

@Value("${info.build.version}")

【讨论】:

  • 嗨,安迪,感谢您的回复。我已经尝试过了,但是每当我从 IDE 运行 Spring Boot 应用程序时,我都会收到错误消息:无法解析字符串值“${version}”中的占位符“版本”。我将此添加到我的 application.properties:info.build.version=${version},并将其添加到我的控制器:@Value("${info.build.version}") private String applicationVersion;
  • @ErikPragt 根据您使用的 IDE 以及您导入 Gradle 项目的方式,Gradle 可能没有运行,因此它没有机会处理您的 application.properties 文件。它可以在命令行上运行吗(例如./gradlew bootRun)?
  • 是的,我当然有点傻,我从我的 IDE 运行它,而不是从 Gradle。还有一种方法可以让它始终有效吗?比如Spring Boot的ResourceBanner.java就做了类似的事情......
  • @AndyWilkinson:你能帮我写这篇文章吗? stackoverflow.com/questions/38752485/…
  • 这会破坏集成测试。
【解决方案3】:

我通过在 application.yml 中添加以下内容解决了这个问题:

${version?:unknown}

它也可以在 cli:gradle bootRun 和 IntelliJ 中工作,您不必在 IntelliJ 中启动之前调用 Gradle 任务 processResources 或使用 spring 配置文件。

这适用于 Gradle 版本:4.6 以及 Spring Boot 版本:2.0.1.RELEASE。 希望对您有所帮助;)

【讨论】:

    【解决方案4】:

    我是这样解决的: 在application.properties 中定义您的info.build.version

    info.build.version=whatever
    

    在你的组件中使用它

    @Value("${info.build.version}")
    private String version;
    

    现在将您的版本信息添加到您的 build.gradle 文件中,如下所示:

    version = '0.0.2-SNAPSHOT'
    

    然后添加一个方法来用正则表达式替换您的 application.properties 以更新您的版本信息:

    def updateApplicationProperties() {
        def configFile = new File('src/main/resources/application.properties')
        println "updating version to '${version}' in ${configFile}"
        String configContent = configFile.getText('UTF-8')
        configContent = configContent.replaceAll(/info\.build\.version=.*/, "info.build.version=${version}")
        configFile.write(configContent, 'UTF-8')
    }
    

    最后,确保在触发buildbootRun时调用该方法:

    allprojects {
        updateVersion()
    }
    

    就是这样。如果您让 Gradle 编译您的应用程序以及从 IDE 运行 Spring Boot 应用程序,则此解决方案有效。该值不会更新,但不会引发异常,并且一旦您运行 Gradle,它将再次更新。

    我希望这对其他人有所帮助,并为我解决了问题。我找不到更合适的解决方案,所以我自己编写了脚本。

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 2020-09-12
      • 1970-01-01
      • 2018-03-10
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多