Maven項目中resources配置總結

目錄

  • 背景
  • 第一部分 基本配置介紹
  • 第二部分 具體配置和注意事項
  • 第三部分 讀取resources資源
  • 參考文獻及資料

背景

一般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/resourcessrc/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>,該值設置爲truespring

第二部分 具體配置和注意事項

2.1 案例說明

根據上面的介紹,最開始例子中有兩段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},最後取並集就獲得了最後編譯結果。

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>

配置中定義的usernamepassword兩個變量的值。使用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")))

其餘類型的配置文件讀取讀者能夠執行查找資料。

參考文獻及資料

一、Maven Resources Plugin,連接:https://maven.apache.org/components/plugins-archives/maven-resources-plugin-2.6/

二、Maven資源過濾的配置,連接:http://c.biancheng.net/view/5285.html

更多關注公衆號:

相關文章
相關標籤/搜索