Springboot与Maven多环境配置的解决方案
作者:海向 发布时间:2023-11-29 08:53:58
目录
Profile用法
resources
filters
多环境配置解决方案
Profile用法
我们在application.yml中为jdbc.username赋予一个值,这个值为一个变量
jdbc:
username: ${jdbc.username}
Maven中的profiles可以设置多个环境,当我们选择a环境后,<jdbc.username>内的值将替换上述配置文件中的变量
</profiles>
<profile>
<id>a</id>
<properties>
<jdbc.username>root</jdbc.username>
</properties>
<!-- 默认使用此环境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
我们查看编译后的application.yml文件,果然变量已经被赋值。我们猜想是否可以利用Profile的这一特性设置开发、测试、生产环境,选择不同环境时使用不同变量,配合Resources和Filter来指定打包内容以及替换变量。
jdbc:
username: root
resources
用来操作编译文件
filters
过滤器,设置过滤器的资源将会对同名变量进行赋值(被赋值的资源文件需要设置filtering为true)
多环境配置解决方案
网上大多数都是分为application-dev.xml、application-test.xml、application-prod.xml三个文件,可是我们在真实项目开发中,将会用到很多各式各样的文件(例如log4j的配置文件),它们在不同环境中应该也是不同的配置,不能在测试和生产环境使用同一个配置文件。所以我们将分为三个文件夹分别代表开发环境、测试环境、生产环境,他们里面的配置文件种类一致但是内容不一样。选择完当前环境后,打的jar包只包含当前环境文件夹下的配置文件。
├─main
│ ├─java
│ │ └─......
│ └─resources
│ ├─dev
│ │ └─config
│ │ │ └─mq.yml
│ │ │ └─redis.yml
│ │ └─application-dev.yml
│ ├─prod
│ │ └─config
│ │ │ └─mq.yml
│ │ │ └─redis.yml
│ │ └─application-prod.yml
│ └─test
│ │ └─config
│ │ │ └─mq.yml
│ │ │ └─redis.yml
│ │ └─application-test.yml
│ └─application.yml
│ └─a.xml
└─test
└─java
└─......
dev下的config下的mq.yml
mq: mq-dev
dev下的config下的redis.yml
redis: redis-dev
dev下的application-dev.yml
profiles.active:
dev
port: dev-port
application.yml
spring:
profiles:
active: ${profiles.active}
port: ${port}
查看编译后的结果
其中application.yml中变量已经被替换为
spring:
profiles:
active: dev
port: dev-port
完整的pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<!--使用默认的变量分割符即${}-->
<configuration>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
<!-- 测试文件的编译路径设置 -->
<testResources>
<testResource>
<!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包-->
<directory>src/main/resources</directory>
<includes>
<include>application.yml</include>
</includes>
<filtering>true</filtering>
</testResource>
<testResource>
<!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包-->
<directory>src/main/resources/${profiles.active}</directory>
<includes>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</testResource>
</testResources>
<resources>
<resource>
<!--打包该目录下的 application.yml -->
<directory>src/main/resources</directory>
<includes>
<include>application.yml</include>
</includes>
<!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 -->
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<!-- ${profiles.active}由profile提供 -->
<directory>src/main/resources/${profiles.active}</directory>
<includes>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<!-- 定义 filter,即该资源中的值将会用来替换同名属性(设置 filtering 为 true 的资源中的属性)-->
<filters>
<filter>
src/main/resources/${profiles.active}/application-${profiles.active}.yml
</filter>
</filters>
</build>
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles>
来源:https://www.cnblogs.com/haixiang/p/12451703.html


猜你喜欢
- 首先我将贴出几种实现圆角边框的dmeo程序效果图:方式一:使用shape元素填充背景,设置圆角/带弧度的角1、首先在 \res\drawab
- 效果图如下:类注释:方法注释:idea不会默认帮我们设置,所以需要手动设置。1:IDEA中在创建类时会自动给添加注释打开idea,操作Fil
- 随着互联网公司的微服务越来越多,分布式事务已经成为了我们的经常使用的。所以我们来一步一步的实现基于RocketMQ的分布式事务。接下来,我们
- 在.NET4.0中,我可以借助System.Speech组件让电脑来识别我们的声音。以上,当我说"name",显示&qu
- 一、问题分析入门案例的内容已经做完了,在入门案例中我们创建过一个SpringMvcConfig的配置类,再回想前面咱们学习Spring的时候
- MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对
- 本文通过介绍了Asp.Net中MVC缓存的种类,以及他们之间的区别等内容,让学习者能够深入的了解MVC缓存的原理机制,以下是具体内容:缓存是
- 一、简介 1、地图 地图展示:普通地图(2D,3D)、卫星图和实时交通图。 地图操作:可通过接口或手势控制来实
- 本文实例讲述了C#实现的xml操作类,分享给大家供大家参考,具体如下:using System;using System.Data;usin
- 普通的excel列表,easyexcel读取是没有什么问题的。但是,如果有合并单元格,那么它读取的时候,能获取数
- RPC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样。RP
- 需要装一个插件:File - Settings- Plugins - 搜索gson 安装GsonFromat;如下两张图安装完成后 ,新建一
- 本篇文章是直接下载最新的APK安装的方法,并不是增量下载该APk。想要实现一个android应用,自动更新下载APK软件的方法,我采取的是以
- System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。一.优点1。支持自
- 在搭建Spring Cloud Eureka环境前先要了解整个架构的组成,常用的基础模式如下图:服务提供者:将springboot服务编写好
- 目录一、简述二、内容一、简述利用C# TcpClient在局域网内传输文件,可是文件发送到对面的时候却要重新命名文件的。那可不可以连着文件名
- mybatis foreach嵌套if标签代码实现:Mapper.java文件List<Map<String, Object&g
- 在Android开发中,有时我们需要对SQLite数据库进行增,删,查,找等操作,现在就来简单介绍一下,以下为详细代码。 &nbs
- 本文实例为大家分享了Unity实现全屏截图、Unity实现QQ截图,供大家参考,具体内容如下全屏截图:要实现的是点击鼠标左键,就实现截图,并
- Android中的翻转动画效果的实现,首先看一下运行效果如上图所示. Android中并没有提供直接做3D翻转的动画,所以关于3D翻转的动画