用maven profile实现环境配置切换
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                用maven profile实现环境配置切换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                前言
互聯網后端服務通常會部署多個環境:開發環境、測試環境、預發布環境和生產環境。不同的環境通常有各自的環境配置,例如mysql服務器的地址、用戶名密碼,zookeeper的ip和端口等等。為了使打出的jar包能獲取不同環境的配置,業界最常用的做法是使用maven的profile來區分不同環境。
代碼示例
1. 在resources文件夾中為每個環境創建一個文件夾
└--resources└--dev└--test└--online2. 修改pom.xml,增加如下內容
<profiles><profile><id>dev</id><properties><env>src/main/resources/dev</env></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>test</id><properties><env>src/main/resources/test</env></properties></profile><profile><id>online</id><properties><env>src/main/resources/online</env></properties></profile></profiles><build><resources><resource><directory>src/main/resources</directory><excludes><exclude>dev/**</exclude><exclude>test/**</exclude><exclude>online/**</exclude></excludes></resource><resource><directory>${env}</directory></resource></resources> </build>3. 打包時增加-P參數指定profile
mvn clean package -Pdev mvn clean package -Ptest mvn clean package -Ponline指定的profile對應的文夾中的配置文件將被放入jar包中。在啟動JVM之后這些文件會在classpath下,可以被應用程序訪問。
完整代碼
https://github.com/gzllol/spr...
總結
以上是生活随笔為你收集整理的用maven profile实现环境配置切换的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Nuc972使用NandFlash时,u
- 下一篇: TCPDUMP/LIBPCAP 3-PC
