目錄
- 背景
- 第一部分 基本配置介紹
- 第二部分 具體配置和注意事項
- 第三部分 讀取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 案例說明
根據上面的介紹,最開始例子中有兩段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文件定義了一個研發<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文件也會指定占位符的格式,就會造成格式不統一。例如:spring boot把默認的占位符號${}改成了@var@。所以建議進行配置,否則容易環境"污染"。
2.4 關於一個錯誤觀點的說明
有很多關於這個主題的文章(例如CSND)中,認為同一個<resource>中,若是<include>和<exclude>都存在的話,那就發生沖突了,這時會以<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 子目錄
資源目錄也是支持子目錄的。即可以在資源目錄下面創建子目錄,在打包過程中會保留子目錄結構。例如:
resources
-test
--app.xml
-application.yml
-application.properties
-application.xml
在項目編譯后,如果子目錄中資源文件被保留,那么子目錄的結構也是保留的。例如:
target
-classes
--test
---app.xml
-application.yml
-application.properties
-application.xml
第二部分 讀取resources資源
例如我們的配置文件properties類型的配置文件,可以使用下面的語句進行讀取:
- 方法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")))
其他類型的配置文件讀取讀者可以執行查找資料。
參考文獻及資料
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
更多關注公眾號:

