1. 程式人生 > >利用maven-resource-plugin外掛進行打包時不同環境下的配置檔案切換

利用maven-resource-plugin外掛進行打包時不同環境下的配置檔案切換

基本概念說明(resources、filter和profile): 
1.profiles定義了各個環境的變數id 
2.filters中定義了變數配置檔案的地址,其中地址中的環境變數就是上面profile中定義的值 
3.resources中是定義哪些目錄下的檔案會被配置檔案中定義的變數替換,一般我們會把專案的配置檔案放在src/main/resources下,像db,bean等,裡面用到的變數在打包時就會根據filter中的變數配置替換成固定值 
          在我們平常的java開發中,會經常使用到很多配製檔案(xxx.properties,xxx.xml),而當我們在本地開發(dev),測試環境測試(test),線上生產使用(product)時,需要不停的去修改這些配製檔案,次數一多,相當麻煩。現在,利用maven的filter和profile功能,我們可實現在編譯階段簡單的指定一個引數就能切換配製,提高效率,還不容易出錯,詳解如下。 
 

原理:
    利用filter實現對資原始檔(resouces)過濾 
maven filter可利用指定的xxx.properties中對應的key=value對資原始檔中的${key}進行替換,最終把你的資原始檔中的username=${key}替換成username=value 
    利用profile來切換環境 
maven profile可使用作業系統資訊,jdk資訊,檔案是否存在,屬性值等作為依據,來啟用相應的profile,也可在編譯階段,通過mvn命令加引數 -PprofileId 來手工啟用使用對應的profile 
結合filter和profile,我們就可以方便的在不同環境下使用不同的配製 

打包替換檔案配置:

如圖配置

1:指生產環境下的配置

2:指開發環境下的配置

在pom.xml下的配置如下:

  <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!--配置資料來源切換-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                                          <!-- 覆蓋原有檔案 -->
                             <overwrite>true</overwrite>
                            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                            <!-- 也可以用下面這樣的方式(指定相對url的方式指定outputDirectory) <outputDirectory>target/classes</outputDirectory> -->
                            <!-- 待處理的資源定義 -->
                            <resources>
                                <resource>
                                    <!-- 指定resources外掛處理哪個目錄下的資原始檔 -->
                                    <directory>src/main/resources/${package.environment}</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                        <inherited></inherited>
                    </execution>
                </executions>
            </plugin>
        </plugins>
<profile>
            <id>oracle-47</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <package.environment>dev</package.environment>
                  <jdbc.url><![CDATA[jdbc:oracle:thin:@10.xxx.xxx.47:1521:orcl]]></jdbc.url>
                 <jdbc.username>xxx</jdbc.username>
                 <jdbc.password>xxx</jdbc.password>
                <jdbc.driver.name>oracle.jdbc.driver.OracleDriver</jdbc.driver.name>
                <druid.initialSize>1</druid.initialSize>
                <druid.minIdle>1</druid.minIdle>
                <druid.maxActive>20</druid.maxActive>
                <druid.maxWait>6000</druid.maxWait>
                <druid.timeBetweenEvictionRunsMillis>6000</druid.timeBetweenEvictionRunsMillis>
                <druid.minEvictableIdleTimeMillis>300000</druid.minEvictableIdleTimeMillis>
                <druid.maxPoolPreparedStatementPerConnectionSize>20</druid.maxPoolPreparedStatementPerConnectionSize>

                <mybatis.dialect>oracle</mybatis.dialect>
                <mybatis.mapperLocations>classpath*:/mappings/oracle/**/*Mapper.xml
                </mybatis.mapperLocations>
            </properties>
        </profile>

<profile>
            <id>prd</id>
            <properties>
                <package.environment>prd</package.environment>
            </properties>
        </profile>

打包:

在使用maven打包時,測試環境的打包

mvn -insall 

正式環境打包

mvn -install -Pprd 進行打包時會將帶替換檔案替換掉指定目錄下的檔案

打包排除檔案配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <warSourceExcludes>WEB-INF/xxx.jsp</warSourceExcludes>
    </configuration>
</plugin>