将 Gradle.build 版本导入 Spring Boot

2022-09-01 12:47:32

我正在尝试在视图中显示我的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}"

关于以何种正确方式将我的应用程序版本,弹簧启动版本等信息传输到我的控制器中的任何建议?


答案 1

您也可以在以下位置添加此内容:build.gradle

springBoot {    
    buildInfo() 
}

然后,您可以使用bean:BuildProperties

@Autowired
private BuildProperties buildProperties;

并获取版本buildProperties.getVersion()


答案 2

参考文档中所述,您需要指示 Gradle 处理应用程序的资源,以便它将占位符替换为项目的版本:${version}

processResources {
    expand(project.properties)
}

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

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

现在,假设您的属性被命名为 ,它将通过以下方式提供:info.build.version@Value

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

推荐