Maven項目中resources配置總結
背景
通常Maven項目的文件目錄結構如下:
# Maven項目的標準目錄結構 src main java #源文件 resources #資源文件 filters #資源過濾文件 config #配置文件 scripts #腳本文件 webapp #web應用文件 test java #測試源文件 resources #測試資源文件 filters #測試資源過濾文件 it #集成測試 assembly #assembly descriptors site #Site target generated-sources classes generated-test-sources test-classes xxx.jar pom.xml LICENSE.txt NOTICE.txt README.txt
其中src/main/resources
和src/test/resources
是資源文件目錄。本文將詳細介紹資源文件相關的配置。
第一部分 基本配置介紹
我們在使用Maven組件來構建項目的時候,通常將配置文件放在資源文件目錄下。針對這個目錄,在pom.xml
文件進行了定義,我們首先看一個案例:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <exclude>application.properties</exclude> </excludes> </resource> </resources> </build>
- 標簽
<directory>
指定資源文件目錄; - 標簽
<include>
指定資源文件目錄中,哪些文件被打包。 - 標簽
<excludes>
指定資源文件目錄中,哪些文件不被打包。
特別的,標簽<filtering>
是一個bool
值,默認值為false
。在maven資源文件中,支持使用變量placeholder
,例如資源文件:
# application.properties application.user=${username} application.password=${password}
文件中使用${keyword}
占位符來標識變量。這時候可以在pom.xml
文件中定義變量的取值:
<properties> <username>mysql</username> <password>password123</password> </properties>
如果需要對配置文件中變量進行替換實際值,就需要開啟<filtering>
,該值設置為true
。
第二部分 具體配置和注意事項
2.1 案例說明
根據(jù)上面的介紹,最開始例子中有兩段resource
的配置描述,分別的含義為:
- 第一個配置的含義是:在配置文件目錄
src/main/resources
過濾掉其他文件,只保留application.properties
文件。并且開啟filtering
變量替換屬性。 - 第二個配置的含義是:在配置文件目錄
src/main/resources
過濾掉application.properties
文件,其他文件均保留。并且關閉filtering
變量替換屬性。
需要特別注意的是,這里兩個<resources>
都是對資源目錄<src/main/resources>
的配置定義,一個是保留application.properties
,一個是去除application.properties
。這樣兩個配置會不會沖突?實際上兩個配置是兼容。最后是取兩個配置分別過濾的文件集合的并集。
可以看一下例子,資源目錄src/main/resources
里面有三個文件:
application.yml application.properties application.xml
編譯后,target/classes
路徑中三個配置文件都是有的。第一配置文件過濾后文件集合為{application.properties}
,第二個配置過濾后的集合為{application.yml,application.xml}
,最后取并集就得到了最后編譯結果。
2.2 正則過濾
在對資源目錄中文件進行過濾時,還支持正則表達式。例如:
<include>**/*.xml</include>
這個表達式表示包含了資源目錄下面所有xml
文件(以及子目錄下面)。
2.3 變量占位符
這里主要指的是<filtering>
的功能。例如下面的xml
文件定義了一個研發(fā)<profile>
。
<profiles> <profile> <id>dev</id> <properties> <resource.delimiter>${}</resource.delimiter> <username>mysql</username> <password>password123</password> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> </profiles>
配置中定義的username
和password
兩個變量的值。使用package -P dev
編譯后,配置文件中占位符變量被替換:
application.user=mysql application.password=password123
需要注意的是這里增加了<resource.delimiter>
標簽配置,定義了占位符的格式。有些時候其他依賴包的pom
文件也會指定占位符的格式,就會造成格式不統(tǒng)一。例如:spring boot把默認的占位符號${}改成了@var@。所以建議進行配置,否則容易環(huán)境"污染"。
2.4 關于一個錯誤觀點的說明
有很多關于這個主題的文章(例如CSND
)中,認為同一個<resource>
中,若是<include>
和<exclude>
都存在的話,那就發(fā)生沖突了,這時會以<exclude>
為準。
關于這個論點,筆者實際做了實驗,同一個<resource>
中,同時配置了<include>
和<exclude>
。
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.properties</include> </includes> <excludes> <exclude>application.properties</exclude> </excludes> </resource> </resources> </build>
編譯結果,配置文件沒有打包進入target/classes
。說明這個論點是有問題的。說明在同一個resource
中兩種配置是取交集的。
2.5 子目錄
資源目錄也是支持子目錄的。即可以在資源目錄下面創(chuàng)建子目錄,在打包過程中會保留子目錄結構。例如:
resources -test --app.xml -application.yml -application.properties -application.xml
在項目編譯后,如果子目錄中資源文件被保留,那么子目錄的結構也是保留的。例如:
target -classes --test ---app.xml -application.yml -application.properties -application.xml
第二部分 讀取resources資源
例如我們的配置文件properties
類型的配置文件,可以使用下面的語句進行讀?。?/p>
方法1,從編譯后的整個classes目錄下去找;
InputStream is = this.getClass().getResourceAsStream("/" +application.properties);
方法2,ClassLoader
從整個classes目錄找;
InputStream is = this.getClass().getClassLoader().getResourceAsStream(application.properties);
讀取使用Java
的工具包java.util.Properties
:
import java.util.Properties; Properties properties = new Properties(); InputStream is = this.getClass().getClassLoader().getResourceAsStream(application.properties); properties.load(is) //獲取配置文件中name的配置值 System.out.println(properties.get(getProperty("name")))
其他類型的配置文件讀取讀者可以執(zhí)行查找資料。
參考文獻及資料
1、Maven Resources Plugin,鏈接:https://maven.apache.org/components/plugins-archives/maven-resources-plugin-2.6/
2、Maven資源過濾的配置,鏈接:http://c.biancheng.net/view/5285.html
到此這篇關于Maven項目中resources配置總結的文章就介紹到這了,更多相關Maven resources配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決@Autowired注入空指針問題(利用Bean的生命周期)
這篇文章主要介紹了解決@Autowired注入空指針問題(利用Bean的生命周期),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot使用MyBatis-Plus解決Invalid?bound?statement異常
這篇文章主要介紹了SpringBoot使用MyBatis-Plus解決Invalid?bound?statement異常,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09Java數(shù)據(jù)結構之LinkedList的用法詳解
鏈表(Linked?list)是一種常見的基礎數(shù)據(jù)結構,是一種線性表。Java的LinkedList(鏈表)?類似于?ArrayList,是一種常用的數(shù)據(jù)容器,本文就來簡單講講它的使用吧2023-05-05