在开发中总是存在多个环境的切换,在 SpringBoot 工程可通过 application-*.properties 的方式实现多环境配置,但是打包时会把所有的.properties 配置文件都打到包里。这样对启动项目存在极大的干扰。通过下面的方式可以实现在打包的时候只打上当前环境的配置文件。
在/src/resources
下面共有四个配置文件,其中application-test.properties 、application-dev.properties、application-pro.properties 为测试、开发和生产的配置文件,根据实际打包进不同的文件,application.properties 为打包环境选择文件,配置内容为:
#surroundings 参数是指定打包环境
spring.profiles.active=@surroundings@
为设置的环境变量
(1)配置有哪些环境,打包会获取指定打包环境
<profiles><profile><id>dev</id><properties><!-- 本地开发环境 --><surroundings>dev</surroundings></properties><activation><!-- 默认的,不加参数时执行这个profile --><activeByDefault>true</activeByDefault></activation></profile><profile><id>test</id><properties><!-- 测试环境 --><surroundings>test</surroundings></properties></profile><profile><id>pro</id><properties><!-- 生产环境 --><surroundings>pro</surroundings></properties></profile></profiles>
(2)设置编译配置
<resources><resource><filtering>true</filtering><directory>src/main/resources</directory><excludes><!-- 存在的环境配置,如果不写那么就会把没写配置也打包进去 --><exclude>application-dev.properties</exclude><exclude>application-test.properties</exclude><exclude>application-pro.properties</exclude></excludes></resource><resource><filtering>true</filtering><directory>src/main/resources</directory><includes><!-- 根据打包命令选择配置后缀 --><include>application-${surroundings}.properties</include></includes></resource></resources>
(1)开发环境:clean install -Pdev
(2)测试环境:clean install -Ptest
(3)生产环境:clean install -Ppro
创建运行任务:
输入打包命令:
打包结果:
在开发中总是存在多个环境的切换,在 SpringBoot 工程可通过 配置进行多环境配置。通过配置可以打包对应配置文件里面的配置文本。
其实跟上面的流程是一样的,但是需要修改 上面 3(2)设置编译配置 内容就好了,如下:
<resources><resource><filtering>true</filtering><directory>src/main/resources</directory><excludes><!-- 修改部分 --><exclude>dev</exclude><exclude>test</exclude><exclude>pro</exclude></excludes></resource><resource><!-- 修改部分 --><directory>src/main/resources/${surroundings}</directory></resource></resources>
(1)开发环境:clean install -Pdev
(2)测试环境:clean install -Ptest
(3)生产环境:clean install -Ppro
创建运行任务:
输入打包命令:
打包结果:
对应application-*.yml 这种类型,同样适用,只需要把application-*.properties 修改为 application-*.yml 就可以了。
本文发布于:2024-02-04 06:47:46,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170701409053308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |