maven的資源過濾 原

maven的過濾資源需要結合maven的2個定義才能實現,分別是:

  • profile
  • resources

下面分開來做介紹。

profile

profile可以讓我們定義一系列的配置信息,然後指定其激活條件。這樣我們就可以定義多個profile,然後每個profile對應不同的激活條件和配置信息,從而達到不同環境使用不同配置信息的效果。需要掌握profile的定義以及激活條件。後面結合resources會介紹。

resources

resources是指定maven編譯資源文件指定到何處的,例如maven的標準資源目錄結構是src/main/resources(這個在超級pom中定義到了),maven進行編譯時候就會將resources中的資源文件放到web的WEB-INF/classes下.具體如何和資源目錄有關係,後面結合的時候後講到。 
超級pom中定義的resources:

<resources>
     <resource>
            <directory>${project.basedir}/src/main/resources</directory>
    </resource>
</resources>
<testResources>
      <testResource>
           <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
</testResources>

 

maven標準目錄filter

很多互聯網項目中,測試環境和線上環境都是分離的,那麼爲了方便開發測試,maven項目可以在編譯時選取不同的配置文件,如何設置呢,看看以下例子?。

例子如下: 
我在java/src/resources目錄中定義了jdbc.properties文件內容如下:

#dataSource configure
#jdbc.connection.url=jdbc:mysql://localhost:3306/test
#jdbc.connection.username=root
#jdbc.connection.password=123456


jdbc.connection.url=${jdbc.url}
jdbc.connection.username=${jdbc.username}
jdbc.connection.password=${jdbc.password}
  • 通過maven編譯後再WEB-INF/classes中得到的jdbc.properties文件內容如下:
#dataSource configure
#jdbc.connection.url=jdbc:mysql://localhost:3306/test
#jdbc.connection.username=root
#jdbc.connection.password=123456


jdbc.connection.url=abcd
jdbc.connection.username=cccc
jdbc.connection.password=dddd

具體是怎麼做到的呢?屬性在使用${}的方式獲取,屬性值肯定得在pom中定義,這個在項目pom.xml中的定義方式如下:

<profiles>
        <!-- 開發/測試環境,默認激活 -->
        <profile>
            <id>test</id>
            <properties>
                <jdbc.url>abcd</jdbc.url>
                <jdbc.username>cccc</jdbc.username>
                <jdbc.password>dddd</jdbc.password>
            </properties>
            <activation>
                <!--默認啓用的是dev環境配置 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <!-- 生產環境 -->
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>

爲了能讓profiles中的內容能讓resources中的文件使用到,還需要上面說到的resources插件,定義信息如下:

<build>
        <finalName>idea-maven-introduce</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

filtering設置爲true很關鍵,不然引用不到profiles中的內容。但是這樣做,就算設置好了嗎,如何切換不同的屬性的呢,還是沒能體現到啊

profiles的激活方式:

  1. 默認激活方式 
    根據上面的例子,定義了一個
<activation>
        <!--默認啓用的是dev環境配置 -->
         <activeByDefault>true</activeByDefault>
</activation>

這個是默認的激活方式,意思就是你什麼都不做,直接使用標準的package打包時候會將該test定義屬性能讓resources下面的文件獲取到。

使用標準的maven目錄file進行管理profiles

src/main/java/filters中的文件如下: 
aaa.properties

jdbc.url=aaajdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=aaatestuser
jdbc.password=aaa123456

bbb.properties

jdbc.url=bbbjdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=bbbtestuser
jdbc.password=bbb123456

file管理配置文件例子1:

pom.xml文件的內容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lgy</groupId>
    <artifactId>idea-maven-introduce</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>idea-maven-introduce Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>



    <build>
        <finalName>idea-maven-introduce</finalName>

        <filters> <!-- 指定使用的 filter -->
            <filter>src/main/filters/aaa.properties</filter>
        </filters>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

此時去掉了profiles,直接用filters指定要使用的filter,此時,resources中要用到的值都會從aaa.properties.

結合profiles的激活機制能更好的使用filers目錄中的內容,pom.xml中的內容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lgy</groupId>
    <artifactId>idea-maven-introduce</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>idea-maven-introduce Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <profiles>
        <!-- 開發/測試環境,默認激活 -->
        <profile>
            <id>test</id>
            <properties>
                <dev.name>aaa</dev.name>
            </properties>

            <activation>
                <!--默認啓用的是dev環境配置 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <!-- 生產環境 -->
        <profile>
            <id>product</id>
            <properties>
                <dev.name>bbb</dev.name>
            </properties>
        </profile>
    </profiles>

    <build>
        <finalName>idea-maven-introduce</finalName>

        <filters> <!-- 指定使用的 filter -->
            <filter>src/main/filters/${dev.name}.properties</filter>
        </filters>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

此時默認的激活方式就是profiles中id爲test,filters就會去尋找aaa.peroperties中的對應的屬性值給resources中的資源文件進行使用!

總結

有關知識點的內容講解有如下: 
- maven profiles標籤的使用 
- resources 資源標籤的使用 
- filters 標籤的使用

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章