1. 程式人生 > >maven 多資料庫環境配置

maven 多資料庫環境配置

在使用maven構建的web專案裡,不管採用的是什麼orm框架,資料庫寫死了必然不是最靈活的方式。所以通過maven 的buid方式可以動態的分配資料庫資訊比如在jdbc.properties中,可以這樣寫:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=${maven.jdbc.url}
jdbc.user=${maven.jdbc.user}
jdbc.password=${maven.jdbc.pwd}
當然,這個jdbc.properties是在springmvc中或者其他orm配置檔案中對映進去的。就如在spring-dao中 那麼maven是如何處理呢,如下配置了三個處理環境:其內容格式應該與上面的${maven.jdbc.url}等格式對應好:
123maven.jdbc.url=jdbc:mysql://******/gdtc_dbomaven.jdbc.user=g***c
maven.jdbc.pwd=8****o
、最後在pom.xml檔案中配置好build時候的處理方式:複製程式碼
    <profiles>
        <profile>
            <!-- 本地開發環境 -->
            <id>dev</id>
            <properties>
                <deploy.env>dev</deploy.env>
            </properties>
         
        </
profile> <profile> <!-- 62 --> <id>pro62</id> <properties> <deploy.env>pro62</deploy.env> </properties> </profile> <profile> <!-- 71 -->
<id>pro71</id> <properties> <deploy.env>pro71</deploy.env> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> </profiles> <build> <filters> <filter>src/main/resources/filters/${deploy.env}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <finalName>gdtc2016</finalName> </build>
複製程式碼其中 activation元素是預設在build時候選擇的版本當然,我們也可以再專案中右鍵->run as -> maven build ->在Grols中輸入->clean install -Pdev 或者 clean install -Ppro6等來根據需要打包不同的環境版本