AutoConfig 是一款 maven 插件,主要用於 Maven 項目打包使用。在我們的工作中,會將自己寫的代碼打成 jar 包或者 war 包發布到各種環境上。一般地,不用的環境所使用的數據庫、緩存的配置是不同的。我們完全可以手工修改不用環境的配置,當然這種做法是非常耗費精力的。好在, maven 為我們提供了 profile 機制,但我在工作中還覺得它不夠好,因為它把我的數據庫用戶名和密碼配置在 pom.xml 文件中,pom 文件又被 git 所管理,其實就暴露了數據庫連接的配置。於是我找到了一款非常好用的 maven 插件,實現不同環境使用不同的配置進行打包,同時又不會將 pom.xml 文件納入 git 倉庫管理。
以下所寫的均是基於下面的這篇資料整理的一份操作筆記,供自己查閱和別人參考。
第 13 章 AutoConfig工具使用指南
http://openwebx.org/docs/autoconfig.html
操作步驟:
1、將項目的打包方式設置為 war ;
2、建立打包方式為 war 的文件夾和文件:
3、編寫 auto-config.xml 文件,示例代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <config> <group> <property name="datasource.slave.host" defaultValue="127.0.0.1" description="datasource slave host" /> <property name="datasource.slave.port" defaultValue="3306" description="datasource slave port" /> <property name="datasource.slave.db" defaultValue="read" description="datasource slave db" /> <property name="datasource.slave.username" defaultValue="root" description="datasource slave username" /> <property name="datasource.slave.password" defaultValue="123456" description=" datasource slave password" /> <property name="datasource.slave.maxconn" defaultValue="50" description="datasource slave maxconn" /> <property name="datasource.slave.minconn" defaultValue="25" description="datasource slave minconn" /> </group> <group> <property name="datasource.master.host" defaultValue="127.0.0.1" description="datasource master host" /> <property name="datasource.master.port" defaultValue="3306" description="datasource master port" /> <property name="datasource.master.db" defaultValue="read" description="datasource master db" /> <property name="datasource.master.username" defaultValue="root" description="datasource master username" /> <property name="datasource.master.password" defaultValue="123456" description="datasource master password" /> <property name="datasource.master.maxconn" defaultValue="50" description="datasource master maxconn" /> <property name="datasource.master.minconn" defaultValue="25" description="datasource master minconn" /> </group> <script> <generate template="application.properties.vm" destfile="WEB-INF/classes/application.properties" /> </script> </config>
- 注意:上面的 script 子標簽,將根據 application.properties.vm 模板文件的內容去生成打包好的文件中的 WEB-INF/classes/application.properties 文件。
下面我們編寫 application.properties.vm 模板文件。
4、編寫 application.properties.vm 模板文件
datasource.slave.host=${datasource.slave.host} datasource.slave.port=${datasource.slave.port} datasource.slave.db=${datasource.slave.db} datasource.slave.username=${datasource.slave.username} datasource.slave.password=${datasource.slave.password} datasource.slave.maxconn=${datasource.slave.maxconn} datasource.slave.minconn=${datasource.slave.minconn} datasource.master.host=${datasource.master.host} datasource.master.port=${datasource.master.port} datasource.master.db=${datasource.master.db} datasource.master.username=${datasource.master.username} datasource.master.password=${datasource.master.password} datasource.master.maxconn=${datasource.master.maxconn} datasource.master.minconn=${datasource.master.minconn}
- 注意:這里的 ${datasource.slave.host} 應該和第 3 步的 auto-config.xml 文件中配置的 property 子節點 name 屬性對應。
5、編寫 pom.xml 文件引入 autoconfig 插件
(1)將該插件的 autoconfig 目標綁定到 maven 生命周期的 package 階段
<plugin> <groupId>com.alibaba.citrus.tool</groupId> <artifactId>autoconfig-maven-plugin</artifactId> <version>1.2</version> <configuration> <userProperties>${autoconfig.path}/${autoconfig.file}</userProperties> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>autoconfig</goal> </goals> </execution> </executions> </plugin>
- (2)為了使得配置更加靈活,我們配置屬性 autoconfig.path 和 autoconfig.file
<autoconfig.path>${user.home}/antx-config/${artifactId}/${devModel}</autoconfig.path> <autoconfig.file>antx.properties</autoconfig.file>
其中,${user.home} 是 maven 定義的系統屬性,這里代表操作系統的宿主目錄。
(3)下面定義 profile
<profiles> <!-- 開發環境:本機 --> <profile> <id>dev</id> <properties> <devModel>dev</devModel> <autoconfig.file>antx.properties</autoconfig.file> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <!-- 內測環境:內網 192.168.12.250 --> <profile> <id>beta</id> <properties> <devModel>beta</devModel> <autoconfig.file>antx.properties</autoconfig.file> </properties> </profile> <!-- 集成測試環境:lyced --> <profile> <id>inte</id> <properties> <devModel>inte</devModel> <autoconfig.file>antx.properties</autoconfig.file> </properties> </profile> <!-- 生產環境:17english --> <profile> <id>pro</id> <properties> <devModel>pro</devModel> <autoconfig.file>antx.properties</autoconfig.file> </properties> </profile> </profiles>
以上就基本完成了 autoconfig 的配置。我們執行 maven 的 package 目標,就可以看到 autoconfig 生效了。
如果你使用 IntelliJ IDEA 作為開發工具,你可以使用下面的方式執行 maven 的 package 目標,帶上 profile 。
注意事項:
1、文件夾要自己建立,autoconfig 插件不會幫你創建文件夾;