首先,Java代码中是无法直接读取pom.xml中的内容的,需要先把值转到xxx.properties中,再通过程序读取xxx.properties中对应的值。
一、xxx.properties读取pom.xml
1.xxx.properties中
以pom.xml中的version标签为例。@xx@代表读取pom.xml中的值
project.version=@project.version@
这里为什么是用@呢:
由于${}方式会被maven处理。如果你pom继承了spring-boot-starter-parent,Spring Boot已经将maven-resources-plugins默认的${}方式改为了@@方式,如@name@
2.pom.xml中
在rescource加入
src/main/resources
**.*
**/**.*
true
比如证书文件,不做任何处理的话会抛出异常加入这个标签会后,*.xml、*.properties正常,其他文件的内容可能会发生改变。
DerInputStream.getLength(): lengthTag=111, too big
解决方案是把把不需要过滤的文件单独列出来,从maven-resources-plugin插件中排除
org.apache.maven.plugins
maven-resources-plugin
UTF-8
pem
pfx
p12
eot
svg
ttf
woff
css
js
html
ico
二、读取xxx.properties文件
这个就比较常规了
@Value("${project.version}")
private String version;
最后输出到页面
版权声明:本文为weixin_42525189原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。