欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot 如何讀取pom.xml中的值

 更新時間:2022年01月18日 10:34:24   作者:小皮de夢想  
這篇文章主要介紹了SpringBoot 如何讀取pom.xml中的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

如何讀取pom.xml中的值

首先,Java代碼中是無法直接讀取pom.xml中的內(nèi)容的,需要先把值轉(zhuǎn)到xxx.properties中,再通過程序讀取xxx.properties中對應(yīng)的值。

xxx.properties讀取pom.xml

1.xxx.properties中

以pom.xml中的version標(biāo)簽為例。@xx@代表讀取pom.xml中的值

這里為什么是用@呢:

由于${}方式會被maven處理。如果你pom繼承了spring-boot-starter-parent,Spring Boot已經(jīng)將maven-resources-plugins默認(rèn)的${}方式改為了@@方式,如@name@

2.pom.xml中

在rescource加入

<resource>
    <directory>src/main/resources</directory>
    <includes>
       <include>**.*</include>
       <include>**/**.*</include>
    </includes>
    <filtering>true</filtering>
</resource>

比如證書文件,不做任何處理的話會拋出異常加入這個標(biāo)簽會后,*.xml、*.properties正常,其他文件的內(nèi)容可能會發(fā)生改變。

DerInputStream.getLength(): lengthTag=111, too big

解決方案是把把不需要過濾的文件單獨(dú)列出來,從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文件

這個就比較常規(guī)了

<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配置報錯

創(chuàng)建父子maven工程時,maven配置文件pom.xml報錯;

我的父工程存在多種環(huán)境配置,使用${xxx}占位符方式進(jìn)行引入配置文件版本管理;創(chuàng)建maven子工程時報錯,如圖報錯提示:

在這里插入圖片描述

報錯原因

由于方式會被maven處理。如果你pom繼承了spring−boot−starter−parent,SpringBoot已經(jīng)將maven−resources−plugins默認(rèn)的‘{}方式會被maven處理。如果你pom繼承了spring-boot-starter-parent, Spring Boot已經(jīng)將maven-resources-plugins默認(rèn)的`方式會被maven處理。如果你pom繼承了spring−boot−starter−parent,SpringBoot已經(jīng)將maven−resources−plugins默認(rèn)的‘{xxx}`方式改為了@@方式,如@name@,所以會出現(xiàn)上述報錯情況;

解決方法

如果還想繼續(xù)使用${xxx}占位符方式,只需要在父工程pom.xml文件中加上下面配置即可:

        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <encoding>utf-8</encoding>
                        <useDefaultDelimiters>true</useDefaultDelimiters>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

如圖所示:

在這里插入圖片描述

更新maven工程,報錯問題解決

在這里插入圖片描述

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論