SpringBoot 如何读取pom.xml中的值
如何读取pom.xml中的值
首先,Java代码中是无法直接读取pom.xml中的内容的,需要先把值转到xxx.properties中,再通过程序读取xxx.properties中对应的值。
xxx.properties读取pom.xml
1.xxx.properties中
以pom.xml中的version标签为例。@xx@代表读取pom.xml中的值
这里为什么是用@呢:
由于${}方式会被maven处理。如果你pom继承了spring-boot-starter-parent,Spring Boot已经将maven-resources-plugins默认的${}方式改为了@@方式,如@name@
2.pom.xml中
在rescource加入
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**.*</include>
<include>**/**.*</include>
</includes>
<filtering>true</filtering>
</resource>比如证书文件,不做任何处理的话会抛出异常加入这个标签会后,*.xml、*.properties正常,其他文件的内容可能会发生改变。
DerInputStream.getLength(): lengthTag=111, too big
解决方案是把把不需要过滤的文件单独列出来,从maven-resources-plugin插件中排除
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> <!-- 过滤后缀为pem、pfx的证书文件 --> <nonFilteredFileExtensions> <nonFilteredFileExtension>pem</nonFilteredFileExtension> <nonFilteredFileExtension>pfx</nonFilteredFileExtension> <nonFilteredFileExtension>p12</nonFilteredFileExtension> <nonFilteredFileExtension>eot</nonFilteredFileExtension> <nonFilteredFileExtension>svg</nonFilteredFileExtension> <nonFilteredFileExtension>ttf</nonFilteredFileExtension> <nonFilteredFileExtension>woff</nonFilteredFileExtension> <nonFilteredFileExtension>css</nonFilteredFileExtension> <nonFilteredFileExtension>js</nonFilteredFileExtension> <nonFilteredFileExtension>html</nonFilteredFileExtension> <nonFilteredFileExtension>ico</nonFilteredFileExtension> </nonFilteredFileExtensions> </configuration> </plugin>
读取xxx.properties文件
这个就比较常规了
<modelVersion>4.0.0</modelVersion> <groupId>com.didispace</groupId> <artifactId>Chapter4-2-2</artifactId> <version>1.0.0</version> <packaging>jar</packaging>
@Value("${project.version}")
private String version;
SpringBoot读取pom配置报错
创建父子maven工程时,maven配置文件pom.xml报错;
我的父工程存在多种环境配置,使用${xxx}占位符方式进行引入配置文件版本管理;创建maven子工程时报错,如图报错提示:

报错原因
由于方式会被maven处理。如果你pom继承了spring−boot−starter−parent,SpringBoot已经将maven−resources−plugins默认的‘{}方式会被maven处理。如果你pom继承了spring-boot-starter-parent, Spring Boot已经将maven-resources-plugins默认的`方式会被maven处理。如果你pom继承了spring−boot−starter−parent,SpringBoot已经将maven−resources−plugins默认的‘{xxx}`方式改为了@@方式,如@name@,所以会出现上述报错情况;
解决方法
如果还想继续使用${xxx}占位符方式,只需要在父工程pom.xml文件中加上下面配置即可:
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
如图所示:

更新maven工程,报错问题解决

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
实例讲解Java中random.nextInt()与Math.random()的基础用法
今天小编就为大家分享一篇关于实例讲解Java中random.nextInt()与Math.random()的基础用法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2019-02-02
基于JAVA代码 获取手机基本信息(本机号码,SDK版本,系统版本,手机型号)
本文给大家介绍基于java代码获取手机基本信息,包括获取电话管理对象、获取手机号码、获取手机型号、获取SDK版本、获取系统版本等相关信息,对本文感兴趣的朋友一起学习吧2015-12-12
Java StringBuffer与StringBuilder有什么区别
当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder类,和String类不同的是,StringBuffer和 StringBuilder类的对象能够被多次的修改,并且不产生新的未使用对象,本篇我们来分析分析它们的区别2023-01-01
Mybatis中通用Mapper的InsertList()用法
文章介绍了通用Mapper中的insertList()方法在批量新增时的使用方式,包括自增ID和自定义ID的情况,对于自增ID,使用tk.mybatis.mapper.additional.insert.InsertListMapper包下的insertList()方法;对于自定义ID,需要重写insertList()方法2025-02-02
Java实战之实现一个好用的MybatisPlus代码生成器
这篇文章主要介绍了Java实战之实现一个好用的MybatisPlus代码生成器,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下2021-04-04


最新评论