实际上,我更感兴趣的是与这个类的jar相关的MANIFEST.MF文件中的“实现版本”信息。我需要提供类似默认清单servlet的东西,在那里我也将有由buildnumber-maven-plugin提供的供应链管理提交版本。有什么简单的方法可以注入主应用程序类吗?
发布于 2015-09-03 03:55:43
您是否只是简单地尝试将MANIFEST.MF定义为属性源,然后只是自动连接值?
@SpringBootApplication
@PropertySource("META-INF/MANIFEST.MF")
public class Application implements CommandLineRunner {
@Value("${Spring-Boot-Version:notfound}")
String springBootVersion;
@Value("${Implementation-Version:notfound}")
String implementationVersion;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("springBootVersion is " + springBootVersion);
System.out.println("implementationVersion is " + implementationVersion);
}
}这将打印以下内容:
springBootVersion is 1.2.5.RELEASE
implementationVersion is 0.1.0MANIFEST.MF已经是一种yaml格式,boot也能理解它。
发布于 2015-09-03 01:30:06
你可以使用这个插件来创建属性文件,你可以简单地在spring中读取它作为任何其他的属性文件。
有"buildNumberPropertiesFileLocation“选项,您可以在其中指定属性文件的位置。只需将其放在src/main/resources/version.properties中,并将其作为spring应用程序中的常规属性源进行读取。您还可以指定属性名称。
只需查看文档中的可用选项:buildnumber-maven-plugin docs
可以使用属性占位符读取属性
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>version.properties</value>
</property>
</bean>或者使用注释:
@PropertySource({ "classpath:version.properties" })
@Configuration
class SomeConfigClass {}然后,您可以简单地向服务类/控制器注入属性
@Value( "${project.version}" )
private String projectVersion;我检查过Spring启动代码,我认为主应用程序类只用于日志记录,所以如果你想在运行时读取它,你必须以某种方式将它注入到应用程序上下文中,或者定义在运行时将被读取的系统属性。
https://stackoverflow.com/questions/32358051
复制相似问题