Eri*_*agt 14 java spring gradle spring-boot
我试图在视图中显示我的Spring Boot应用程序的应用程序版本.我确定我可以访问这个版本信息,我只是不知道如何.
我尝试以下这些信息:https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html,并把这个在我的application.properties:
info.build.version=${version}
Run Code Online (Sandbox Code Playgroud)
然后将它加载@Value("${version.test}")到我的控制器中,但这不起作用,我只会得到如下错误:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in string value "${version}"
Run Code Online (Sandbox Code Playgroud)
有关获取我的应用程序版本,弹簧启动版本等信息的正确方法的任何建议到我的控制器?
Clé*_*ier 31
您还可以将其添加到build.gradle:
springBoot {
buildInfo()
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用BuildPropertiesbean :
@Autowired
private BuildProperties buildProperties;
Run Code Online (Sandbox Code Playgroud)
并获得版本 buildProperties.getVersion()
如参考文档中所述,您需要指示Gradle处理应用程序的资源,以便它将${version}使用项目的版本替换占位符:
processResources {
expand(project.properties)
}
Run Code Online (Sandbox Code Playgroud)
为了安全起见,您可能希望缩小范围,以便仅application.properties处理:
processResources {
filesMatching('application.properties') {
expand(project.properties)
}
}
Run Code Online (Sandbox Code Playgroud)
现在,假设您的财产已命名info.build.version,它将通过@Value以下方式提供:
@Value("${info.build.version}")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12144 次 |
| 最近记录: |