(1)经过maven的profile功能,在打包的时候,经过-P指定maven激活某个pofile,这个profile里面配置了一个参数activatedProperties,不一样的profile里面的这个参数的值不一样 web
(2)SpringBoot的application.properties文件里面spring.profiles.active填的值取上面maven的activatedProperties参数值。spring
这样能实现的效果为:app
项目结构以下图所示,是个常见的SpringBoot项目结构,不一样环境的propertis文件的后缀不一样(见图中红框处)maven
maven的profile的配置见下面代码spa
注意:maven的profile中activatedProperties参数值须要和SpringBoot的不一样环境Properties文件的后缀同样。命令行
好比开发环境的Properties的文件名为application-dev.properties,那么maven中dev的profile里面的activatedProperties参数值就应该是dev3d
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <activatedProperties>dev</activatedProperties> </properties> </profile> <profile> <id>test</id> <activation> <os> <family>mac</family> </os> </activation> <properties> <activatedProperties>test</activatedProperties> </properties> </profile> <profile> <id>production</id> <properties> <activatedProperties>production</activatedProperties> </properties> </profile> </profiles>
在application.properties文件中配置SpringBoot默认激活的propertis文件。这时候spring.profiles.active取上面maven的profile里面配置的activatedProperties
的值,这个取值要用@符号来取。具体见下面代码code
spring.profiles.active\=@activatedProperties@
打包时用 mvn clean package -P profile的idblog
若是不加-P参数,那么默认就是<activeByDefault>true</activeByDefault>所在的profile开发
当咱们打包命令为mvn clean package -P production 时,解压后的jar包中application.properties配置文件中spring.profiles.active的值自动变成了production
(1)该方式优势:打包后不须要经过命令行参数来切换不一样环境的配置文件,把指定环境的这一步放到了maven打包的命令上
(2)该方式实际上是利用了maven的profile功能和SpringBoot的profile相结合使用