最近项目中使用了 autoConfig 工具;
// pom文件中定义多个环境
<profiles>
<profile>
<id>test</id>
<properties>
<autoconfig.properties>antx_test.properties</autoconfig.properties>
</properties>
</profile>
<profile>
<id>pre</id>
<properties>
<autoconfig.properties>antx_pre.properties</autoconfig.properties>
</properties>
</profile>
<profile>
<id>prd</id>
<properties>
<autoconfig.properties>antx_prd.properties</autoconfig.properties>
</properties>
</profile>
</profiles>
// pom 中使用插件
<plugin>
<groupId>com.alibaba.citrus.tool</groupId>
<artifactId>autoconfig-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<userProperties>src/main/resources/properties/${autoconfig.properties}</userProperties>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>autoconfig</goal>
</goals>
</execution>
</executions>
</plugin>
使用 mvn package -P test 就可以打 test 环境的包 ;
打包时,会利用各自环境指定的配置文件去替换 autoConfig.xml 中 script 标签对应配置文件中的占位符;
antoConfig 文件中的一部分如下:
<script>
<generate template="application.properties.vm" destfile="WEB-INF/classes/application.properties" />
</script>
application.propertities 文件中如下:
datasource.slave.host=${datasource.slave.host}
datasource.slave.port=${datasource.slave.port}
datasource.slave.db=${datasource.slave.db}
参考: