一般Maven項目的文件目錄結構以下:html
# 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
是資源文件目錄。本文將詳細介紹資源文件相關的配置。java
咱們在使用Maven組件來構建項目的時候,一般將配置文件放在資源文件目錄下。針對這個目錄,在pom.xml
文件進行了定義,咱們首先看一個案例:mysql
<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
,例如資源文件:web
# application.properties application.user=${username} application.password=${password}
文件中使用${keyword}
佔位符來標識變量。這時候能夠在pom.xml
文件中定義變量的取值:正則表達式
<properties> <username>mysql</username> <password>password123</password> </properties>
若是須要對配置文件中變量進行替換實際值,就須要開啓<filtering>
,該值設置爲true
。spring
根據上面的介紹,最開始例子中有兩段resource
的配置描述,分別的含義爲:sql
第一個配置的含義是:在配置文件目錄src/main/resources
過濾掉其餘文件,只保留application.properties
文件。而且開啓filtering
變量替換屬性。shell
第二個配置的含義是:在配置文件目錄src/main/resources
過濾掉application.properties
文件,其餘文件均保留。而且關閉filtering
變量替換屬性。apache
須要特別注意的是,這裏兩個<resources>
都是對資源目錄<src/main/resources>
的配置定義,一個是保留application.properties
,一個是去除application.properties
。這樣兩個配置會不會衝突?實際上兩個配置是兼容。最後是取兩個配置分別過濾的文件集合的並集。app
能夠看一下例子,資源目錄src/main/resources
裏面有三個文件:
application.yml application.properties application.xml
編譯後,target/classes
路徑中三個配置文件都是有的。第一配置文件過濾後文件集合爲{application.properties}
,第二個配置過濾後的集合爲{application.yml,application.xml}
,最後取並集就獲得了最後編譯結果。
在對資源目錄中文件進行過濾時,還支持正則表達式。例如:
<include>**/*.xml</include>
這個表達式表示包含了資源目錄下面全部xml
文件(以及子目錄下面)。
這裏主要指的是<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@。因此建議進行配置,不然容易環境"污染"。
有不少關於這個主題的文章(例如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
中兩種配置是取交集的。
資源目錄也是支持子目錄的。便可以在資源目錄下面建立子目錄,在打包過程當中會保留子目錄結構。例如:
resources -test --app.xml -application.yml -application.properties -application.xml
在項目編譯後,若是子目錄中資源文件被保留,那麼子目錄的結構也是保留的。例如:
target -classes --test ---app.xml -application.yml -application.properties -application.xml
例如咱們的配置文件properties
類型的配置文件,可使用下面的語句進行讀取:
InputStream is = this.getClass().getResourceAsStream("/" +application.properties);
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")))
其餘類型的配置文件讀取讀者能夠執行查找資料。
一、Maven Resources Plugin,連接:https://maven.apache.org/components/plugins-archives/maven-resources-plugin-2.6/
二、Maven資源過濾的配置,連接:http://c.biancheng.net/view/5285.html
更多關注公衆號: