1. 程式人生 > >maven根據profile讀取指定環境的配置檔案

maven根據profile讀取指定環境的配置檔案

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

<profiles>

<!--default開發分支-->

        <profile>

            <id>local</id>

            <activation>

<activeByDefault>true</activeByDefault>

            </activation>

            <build>

                <filters>

                    <

filter>src/main/resources/local/deploy.properties</filter>

                </filters>

                <resources>

                    <resource>

                        <directory>src/main/resources/common/</directory>

                        <filtering>true</filtering

>

                    </resource>

                    <resource>

                        <directory>src/main/resources/local/</directory>

                        <filtering>true</filtering>

                    </resource>

                </resources>

            </build>

        </profile>

        <profile>

            <id>test</id>

            <activation>

<activeByDefault>false</activeByDefault>

            </activation>

            <build>

                <filters>

                    <filter>src/main/resources/test/deploy.properties</filter>

                </filters>

                <resources>

                    <resource>

                        <directory>src/main/resources/common/</directory>

                        <filtering>true</filtering>

                    </resource>

                    <resource>

                        <directory>src/main/resources/test/</directory>

                        <filtering>true</filtering>

                    </resource>

                </resources>

            </build>

        </profile>

</profiles>

- 先指定 src/main/resources下所有檔案及資料夾為資原始檔 在不同的profile下資原始檔不同

<build>

<resources>

            <resource>

                <directory>src/main/resources/</directory>

                <filtering>true</filtering>

                <includes>

                    <include>deploy/*</include>

                </includes>

                <targetPath>${project.build.directory}</targetPath>

            </resource>

</resources>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

            </plugin>

        </plugins>

</build>

deploy資料夾下這些檔案中的${key}會被替換掉為真正的值


#!/bin/bash

base="${deploy.base}"

name="${project.name}"

java_home="${deploy.java.home}"

bin_home="${deploy.bin.home}"

conf_home="${deploy.conf.home}/resin"

deploy="${deploy.base}"

webapp="${deploy}/${project.name}"

servers=(${deploy.。。.server})

resin="${deploy.resin.home}"

而${deploy.base} ..這些是在deploy.properites中定義


詳解

我們分析下<resource>下面的屬性

<directory>: 配置那個目錄下的檔案通過${key}會被替換成屬性值,resource目錄下,我們一般放jdbc連線,或配置檔案

<includes>:指定那個目錄下那個檔案

<filtering>:這個配置的意思是過濾上面指定屬性檔案中的佔位符,佔位符是${變數名稱}這樣的形式,maven會自動讀取配置檔案,然後解析其中的佔位符,使用上面pom檔案中定義的屬性進行替換

<exclueds>:在resource目錄下,有很多檔案,但用些檔案不希望替換,則可以通過<excluede>指定

<filters>:這裡的filters與<profile>的filter意思一樣,都是指屬性檔案地址,這個如果上面定義<profile>的時候指定了,這裡就不需要了,但有些開發習慣是在<profile>不定義,然後在<build>裡指定。


在dispather-servlet下

<context:component-scanbase-package="com.。。。.webapp"/>

<beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<propertyname="locations">

            <list>

                <value>classpath:dubbo.properties</value>

            </list>

        </property>

    </bean>

只有簡單配置這些,在使用maven命令的時候 mvn clean package -PprofileId ,就可以根據不同環境打不同的包了