springBoot 配置文件或者指定文件配置进行多环境打包

阅读: 评论:0

springBoot 配置文件或者指定文件配置进行多环境打包

springBoot 配置文件或者指定文件配置进行多环境打包

一、多环境打包配置文件

1、描述

在开发中总是存在多个环境的切换,在 SpringBoot 工程可通过 application-*.properties 的方式实现多环境配置,但是打包时会把所有的.properties 配置文件都打到包里。这样对启动项目存在极大的干扰。通过下面的方式可以实现在打包的时候只打上当前环境的配置文件。

2、建立文件

/src/resources下面共有四个配置文件,其中application-test.properties 、application-dev.properties、application-pro.properties 为测试、开发和生产的配置文件,根据实际打包进不同的文件,application.properties 为打包环境选择文件,配置内容为:

#surroundings 参数是指定打包环境
spring.profiles.active=@surroundings@

为设置的环境变量

3、修改l 文件

(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>

4、执行如下打包命令

(1)开发环境:clean install -Pdev

(2)测试环境:clean install -Ptest

(3)生产环境:clean install -Ppro

创建运行任务:

输入打包命令:

打包结果:

 

二、多环境打包指定文件里面的配置

1、描述

在开发中总是存在多个环境的切换,在 SpringBoot 工程可通过 配置进行多环境配置。通过配置可以打包对应配置文件里面的配置文本。

2、建立文件

3、配置

其实跟上面的流程是一样的,但是需要修改 上面 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>

4、执行如下打包命令(同样执行命令)

(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 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23