一般的环境分为开发环境、生产环境和测试环境,频繁的修改配置文件过于繁琐,如果开发人员比较多的话还容易混淆,所以一般都会实行多环境配置
下面以db的配置文件为例:
db.properties
db.springrain.driver=${db.springrain.driver}
db.springrain.url=${db.springrain.url}
db.springrain.username=${db.springrain.username}
db.springrain.password=${db.springrain.password}
然后在添加配置文件
这三个文件都类似,只是数据库链接和密码不同,模拟多个环境
db.springrain.driver=com.mysql.jdbc.Driver
db.springrain.url=jdbc:mysql://localhost:3306/agent
db.springrain.username=root
db.springrain.password=123456
pom文件中的build标签
<filters>
<filter>${basedir}/src/main/filter/${env}/config-${env}.properties</filter>
</filters>
build中的resources标签
<resource>
<directory>${basedir}/src/main/filter</directory>
<excludes>
<exclude>/dev/*</exclude>
<exclude>/test/*</exclude>
<exclude>/pro/*</exclude>
</excludes>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
最后在pom的project标签中指定
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<!-- 设置默认激活这个配置 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<!--发布环境-->
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
在打包的时候需要指定哪个环境打包
clean 清理本地class文件
package -Pdev 使用开发环境打包
-DskipTests 跳过测试代码
打完包可以看一下是否成功
参考地址:https://blog.csdn.net/vbirdbest/article/details/78744786