扩展评论/简短回答:
你必须看看target/classes
...not at src/main/resources
!;)
...文件在src/main/resources
保持未被过滤/触及。
哦,好棒。抱歉,我可以在 target/classes 文件夹中找到属性文件。但如何从我的应用程序中读取此属性文件?
请另请参阅此处:-在基于 servlet 的应用程序中,配置资源文件应该放在哪里以及如何读取? https://stackoverflow.com/q/2161054/592355
..使用“标准”java:
// assuming src/main/resources/build.properties
Properties buildProps = new Properties();
buildProps.load(
//this is fail safe for most situations (test/web container/...), but *any classloader* could do it.
Thread.currentThread().getContextClassLoader()
.getResourceAsStream("build.properties")
);
String buildDate = buildProps.getProperty("build.date");
..与(例如)弹簧:
@PropertySource("classpath:/build.properties")
...
@Value("${build.date}")
String buildDate;
But,自从你标记了java-ee /questions/tagged/java-ee(,您应该有一种非常具体且“复杂”的方法来执行此操作(将属性加载到应用程序中)),所以我们应该问! :) (看:http://www.adam-bien.com/roller/abien/entry/injecting_properties_into_java_ee http://www.adam-bien.com/roller/abien/entry/injecting_properties_into_java_ee)
谢谢,现在可以了,但是构建时间戳看起来不像这样的格式: yyyy-MM-dd HH:mm 我的格式如下: 1549362759203你有什么主意吗?
嗯,到目前为止,没有任何线索......对我来说,它按预期工作,生成:
build.date=2019-02-05 10:55
..以及(已编辑)Properties.load()
code.
(也许你“覆盖”timestamp
属性......这听起来不像是一个非常好的(个人)属性名称(因为any1/thing(可以)引用“时间戳”,更好的是像com.my.company.myVerySpecialTimestamp
!?;)
以及:看看target/classes/build.properties
告诉你出了什么问题:
- https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html
- ... https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html