每次項目部署上線都需要手動去修改配置文件(比如數據庫配置,或者一個自定義的配置)然后才能打包,很麻煩,網上找到 maven profile可以完成這個工作,記錄如下:
環境:eclipse + spring mvc + maven
1、直接看圖,把數據庫的配置單獨拿出來放在了resources_env目錄下,三個不同環境參數不同,
2,在pom文件中添加配置 這個引用自:http://www.cnblogs.com/raphael5200/p/6677549.html,感謝
<profiles> <profile> <!-- 開發環境 --> <id>dev</id> <properties> <env>dev</env> </properties> <activation> <!-- 默認激活該profile節點--> <activeByDefault>true</activeByDefault> </activation> <build> <resources> <resource> <directory>src/main/resources_env/dev</directory> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> </profile> <profile> <!-- 測試環境 --> <id>qa</id> <properties> <env>qa</env> </properties> <build> <resources> <resource> <directory>src/main/resources_env/qa</directory> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> </profile> <profile> <!-- 生產環境 --> <id>online</id> <properties> <env>online</env> </properties> <build> <resources> <resource> <directory>src/main/resources_env/online</directory> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> </profile> </profiles>
說明:這個resources里面的路徑對應上面文件路徑,resources里面所有的配置加上各自環境的配置,
在引用jdbc.pro的地方如下:在datasource.xml中,
還有 新增的 evn那個包下面的所有文件都需要設置為資源文件,這個不必說 直接看圖
3,maven設置要使用的環境:
項目右鍵-->maven-->Select Maven profiles ,選擇一個環境,修改最好清理一下項目才生效,我之前沒清理,發現沒起作用。
4、然后運行項目就是你選擇的環境了,或者直接導出war包,
其他的多環境配置同。
參考:http://www.cnblogs.com/raphael5200/p/6677549.html 感謝。