Maven的pom.xml中resources標簽的用法
pom.xml中resources標簽的用法
我們先看一下spring-boot-starter-parent-2.4.1.pom文件中的resources標簽,可以直接在Maven倉庫中找當然也可以在idea中查看

spring-boot-starter-parent-2.4.1.pom文件中resources標簽內容如下:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
</resources>
</build>先做一下實驗,看一下結果,我們再來給出這個標簽的含義是什么
先在maven工程的resources目錄下面創(chuàng)建2個配置文件

再修改pom文件,修改內容如下
<!--在properties標簽中添加變量test.name--> <properties> <test.name>測試內容test01</test.name> </properties> <!--在build標簽中添加resources標簽--> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>a.properties</include> </includes> </resource> </resources> </build>
再在a.properties文件中引用pom文件中定義的變量值
a.name=${test.name}b.properties文件中的內容如下
b.name=${test.name}
打包完成后在target目錄下的classes類路徑下的內容如下

<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>a.properties</include> </includes> </resource> </resources> </build>
從這里可以得出結論:上面這段配置的含義是如果filtering標簽的值設置成true,默認值是false,則在工程進行編譯打包的時候只會將工程的src/main/resources目錄下的includes標簽中的資源文件打包進去,并且會進行預編譯(就是就將資源文件中引用pom文件中的properties標簽中的變量,轉化其對應的值),除了includes標簽中的資源文件的其它資源文件都不會被打包進去。
再修改pom文件的resources標簽
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>a.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <!--filtering標簽的值默認是false,在這里可寫可不寫--> <filtering>false</filtering> <excludes> <exclude>a.properties</exclude> </excludes> </resource> </resources> </build>
進行編譯打包,結果如下

<resource>
<directory>src/main/resources</directory>
<!--filtering標簽的值默認是false,在這里可寫可不寫-->
<filtering>false</filtering>
<excludes>
<exclude>a.properties</exclude>
</excludes>
</resource>由此可知這段配置的含義是:在項目進行打包的時候會將src/main/resources目錄下除了a.properties資源文件的其它資源文件打包進去,并且這些打包進去的資源文件的內容不會進行預編譯,之前是什么樣子,打完包之后還是什么樣子。
總結
如果項目打包的時候,想對某個資源文件中的內容進行預編譯再打包進去,而對其他的資源文件不進行預編譯處理打包到項目中,則可以按照如下配置進行處理
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>需要進行預編譯的資源文件名稱如:a.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <!--filtering標簽的值默認是false,在這里可寫可不寫--> <filtering>false</filtering> <excludes> <exclude>上面includes標簽中的資源文件名稱如:a.properties</exclude> </excludes> </resource> </resources>
resources標簽結合mybatis使用
如果想要將mapper.xml文件和mapper接口同一個包中,并且在項目打包的時候需要作為資源文件也要打包進去,比如項目的結構如下

可以在pom文件中進行如下配置
<build>
<resources>
<!--將src/main/java目錄下的所有xml文件都作為項目的資源文件,
項目打包的時候也會打包進去
-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--將項目的src/main/resources目錄下的所有文件都作為項目的資源文件
項目打包的時候也會打包進去
-->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>到此這篇關于Maven的pom.xml中resources標簽的用法的文章就介紹到這了,更多相關Maven pom.xml resources標簽內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springcloud-eureka與gateway簡易搭建過程
文章介紹了如何搭建Eureka服務注冊中心和Spring Cloud Gateway網關,對于Eureka,首先創(chuàng)建eureka-server項目,配置依賴和屬性,啟動后通過控制臺查看服務注冊狀態(tài),對于Spring Cloud Gateway,創(chuàng)建gateway項目,配置依賴和屬性,啟動后測試路由轉發(fā)功能,感興趣的朋友一起看看吧2025-12-12
淺談synchronized方法對非synchronized方法的影響
下面小編就為大家?guī)硪黄獪\談synchronized方法對非synchronized方法的影響。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
95%的Java程序員人都用不好Synchronized詳解
這篇文章主要為大家介紹了95%的Java程序員人都用不好Synchronized詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
Java之Springcloud Gateway內置路由案例講解
這篇文章主要介紹了Java之Springcloud Gateway內置路由案例講解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08

