use*_*453 2 spring-boot spring-boot-gradle-plugin
以前在Spring Boot 1.x中,我编写了Gradle任务将jar的构建版本复制到应用程序的版本,application.yml并用regex替换给定的属性,例如info.build.version: 0.0.1
迁移到Spring Boot 2.0后,我意识到有一个io.spring.dependency-management插件可以让我定义buildInfo任务:
springBoot {
buildInfo()
}
Run Code Online (Sandbox Code Playgroud)
这在访问/info执行器时有效并成功显示相同的信息。
现在我想使用生成build.version的META-INF/build-info.properties两个用例:
以前,足以访问这样的属性:@Value("${info.build.version:undefined}") String buildVersion
或in logback-spring.xml:
<springProperty scope="context" name="applicationVersion" source="info.build.version"/>
Run Code Online (Sandbox Code Playgroud)
不幸的是,即使我替换info.build.version为build.version(我希望它能正常工作),两个访问器也不再起作用。
我相信将版本包含在logback中仅几步之遥,即通过@Value注释访问属性,因此这是我的问题的核心:
我怎样才能通过访问生成build.version的?META-INF/build-info.properties@Value
我也尝试添加任务
processResources {
filesMatching('build-info.properties') {
expand(project.properties)
}
}
Run Code Online (Sandbox Code Playgroud)
如/sf/answers/2943598871/中所建议,但这似乎没有任何效果。
说到spring-boot,Spring处理的几乎所有信息都可以作为Spring托管bean访问。
对于构建信息,spring-boot有一个bean称为的自动装配公开buildProperties。这通过ProjectInfoAutoConfiguration模块发生。
加上SpEL对@Value注解的Spring Expression语言()支持,您可以获得如下所述的预期结果。
@Value("#{buildProperties.get('version')}") // not 'build.version'
private String myAppBuildVersion;
Run Code Online (Sandbox Code Playgroud)
或者更好的方法是,将buildPropertiesBean直接自动连接到您的组件,以便您可以随意使用它。
@Autowired
private BuildProperties buildProperties;
Run Code Online (Sandbox Code Playgroud)
注意:自动配置会删除
build.前缀。因此,您的SpEL表达式应该version用作键。不build.version。
更新:
首先让您感到困惑的是我的问题,因为这个问题已经超出了如何使用@Value注释的范围。因此,我将保留上述答案。
为了帮助你的logback-spring.xml,进口build-info.properties到logback-spring.xml如下图所示。这允许使用来访问build-info.properties中的每个键logback place-holders。(请勿将其与弹簧座或弹簧表现相混淆)
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<springProperty scope="context" name="appLogTarget" source="app.log.target"
defaultValue="CONSOLE"/>
<property resource="META-INF/build-info.properties" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[${build.version}] %d{ISO8601}" %-5p [%c{3}"] \(%t:%X{}"\) %m%n</Pattern>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
</root>
</xml>
Run Code Online (Sandbox Code Playgroud)
标签的"resource"属性<properties/>将通过classpath resources查找,并找到合适的标签。
| 归档时间: |
|
| 查看次数: |
1830 次 |
| 最近记录: |