iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot如何读取pom.xml中的值
  • 808
分享到

SpringBoot如何读取pom.xml中的值

2024-04-02 19:04:59 808人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录如何读取pom.xml中的值xxx.properties读取pom.xml读取xxx.properties文件SpringBoot读取pom配置报错报错原因解决方法如何读取pom

如何读取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工程,报错问题解决

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: SpringBoot如何读取pom.xml中的值

本文链接: https://www.lsjlt.com/news/162826.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作