在开发过程中,我们的项目会存在不同的开发环境,比如开发环境、生产环境、测试环境,而我们的项目在不同的环境中有些配置也是不一样的,比如数据源配置、日志文件配置等,假如我们每次将软件部署到不同的环境时,都需要对相应的配置文件进行修改,来来回回修改,很容易出现少改的地方,而且浪费我们的劳动力。项目用的maven的profile来区别不同的环境配置,我也正好来学习一下。
数据准备:准备3个数据库、数据库名为test_db、表名都为student,只是数据不同。
在本次演示中,使用IDEA结合maven的profile来实现对数据库的切换以及应用端口的切换。工程结构如图:
<!--配置不同的profile,对应不同的生产环境-->
<profiles>
<profile>
<!--开发-->
<id>dev</id>
<activation>
<!--默认开发环境-->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
</profile>
<profile>
<!--生产-->
<id>pro</id>
<properties>
<activatedProperties>pro</activatedProperties>
</properties>
</profile>
<profile>
<!--生产-->
<id>test</id>
<properties>
<activatedProperties>test</activatedProperties>
</properties>
</profile>
</profiles>
spring.profiles.active=@activatedProperties@ //这里名称与profile中的标签名一致
打开浏览器访问:localhost/dev-app/all?base=1
启动程序,查看控制台日志如下:
打开浏览器访问:http://localhost/test-app/all?base=1,连接192.168.229.134数据,学生名应该为张三
浏览器访问:http://localhost/pro-app/all?base=1
IDEA结合Maven的profile构建不同开发环境(SpringBoot)
原文:https://www.cnblogs.com/qingmuchuanqi48/p/11939397.html