背景介绍
因为项目框架的原因,开发环境的配置放置在src/main/resources目录,测试环境的配置单独放置在conf目录里面。整体目录大致如下:
conf // 该目录放置除开发环境外的配置
src/main/resources // 该目录放置开发环境的配置
src/main/java // java代码
优化想法
为了避免多处修改,只把不同环境中需要修改的配置放置到测试环境,其它配置还是通过src/main/resources目录去获取。配置文件一共有10个左右,在conf/local/resources中只放置5个需要根据环境进行修改的配置文件。
具体做法
- 在pom.xml中添加profiles
<profiles>
<profile>
<!-- 开发环境的配置 -->
<id>dev</id>
<properties>
<package.environment>src/main/resources</package.environment>
</properties>
<activation>
<!-- 设置当前为默认值 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境的配置 -->
<id>test</id>
<properties>
<package.environment>conf/local/resources</package.environment>
</properties>
</profile>
</profiles>
- 添加resources标签
<resources>
<!-- 不需要修改的配置文件,都从开发环境直接获取,尽可能避免多处修改 -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>api.server.properties</exclude>
<exclude>application.properties</exclude>
<exclude>dids-client-config.properties</exclude>
<exclude>jdbc.properties</exclude>
<exclude>third.url.properties</exclude>
</excludes>
</resource>
<!-- 需要修改的配置文件,根据不同开发环境从不同的地方获取,尽可能避免多处修改 -->
<resource>
<directory>${package.environment}</directory>
<filtering>true</filtering>
<includes>
<include>api.server.properties</include>
<