自动加载配置文件
描述
开发环境修改配置文件 auto-config.xml application.properties.vm 文件,代码合并至测试环境
服务部署时,执行./builde.sh ;部署过程中,提示是否更新 antx.properteis ,选择是,系统自动将本次 auto-config.xml 中新增内容同步到 antx.properties 中
原理
-
auto-config.xml 与 antx.properteis
- 若 auto-config.xml 有新增 key-value,则会同步到 antx.properties 中
- 若 auto-config.xml 中更改 value 值 ,不会同步到 antx.properties 中
- 若 antx.properties 中更改 value 值,不会被 auto-config.xml 中原有信息覆盖
-
application.properties.vm 中添加占位符与name值的对应关系
- 项目运行时,配置文件加载占位符的位置,通过 application.properties.vm 找到对应name值,name值与 auto-config.xml 对应
-
部署服务器中既有 auto-config.xml 又有 antx.propertis 为何要增加 antx.propertis ?
- 项目通过 git 管理,.gitignore 中配置 antx,properties ; antx.properties 不需要添加到 git 版本管理中 ;本地开发环境目录地址 {home}/antx.properties 有此文件
- maven 实现自动化配置工具,项目打包时不对 auto-config.xml 进行打包
实现
- 自动化配置工具
- pom.xml
<plugin>
<groupId>com.alibaba.citrus.tool</groupId>
<artifactId>autoconfig-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>autoconfig</goal>
</goals>
</execution>
</executions>
</plugin>