原文地址为https://www.cnblogs.com/haixiang/p/12451703.html,转载请注明出处!

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>

Springboot与Maven多环境配置文件夹解决方案的更多相关文章

  1. 通过Maven更换环境配置文件

    大致思路:配置文件有三套:main/resources.devmain/resources.prodmain/resources.test公共部分放到main/resource下使用mvn clean ...

  2. 使用IDEA开发SpringBoot不加载application.yml配置文件的解决方案

    1.如果启动项目不加载application.yml配置文件,那么请确认下是否应用了Resources为项目资源文件夹 2.如果项目起初是可以正常使用的,突然不知道改了什么,然后进行启动项目的时候不加 ...

  3. 用maven按环境打包SpringBoot的不同配置文件

    利用maven按环境打包SpringBoot的不同配置文件 application-dev.properties对应开发环境 application-test.properties对应测试环境 app ...

  4. springboot多环境配置文件

    一.关于springboot的配置文件 springboot的配置文件主要有两种:properties文件和yml文件,我们只要选择一种使用就可以了.我们通过properties文件介绍一下配置的方式 ...

  5. 使用maven profile指定配置文件打包适用多环境

    新建maven项目,   在pom.xml中添加 profile节点信息如下: <profiles> <profile> <!-- 开发环境 --> <id& ...

  6. spring-boot多环境配置文件

    spring-boot多环境配置文件 目录 配置 多环境配置文件名称要遵循格式 application-{profile}.yml application.yml spring: profiles: ...

  7. SpringBoot(二): SpringBoot属性配置文件 SpringBoot多环境配置文件 SpringBoot自定义配置文件

    1.属性配置文件 一共分为两种,一种是键值对的properties属性配置文件,一种是yaml格式的配置文件 properties配置: 2.多环境配置文件 当我们的项目中有多套配置文件 比如开发的配 ...

  8. springboot结合maven打包发布

    本篇分享如何使用maven便利我们打springboot的发布包:我这里使用的是idea开发工具,首先创建了多个module的项目结构,如图: 要对多个module的项目做打包,一般情况都是在父级po ...

  9. Maven的环境搭建及新建web项目

    第一次接触maven,做一个简单的记录 一.下载maven及环境变量的配置 下载地址 http://maven.apache.org/download.cgi 配置其环境变量  MAVEN_HOME= ...

随机推荐

  1. 金山wps的面试经历

    故事从两个月前开始说起吧. 前段时间突然想跳槽,原因也没啥,就是想折腾下,看看外面的世界?有一部分原因是想离家近一些稳定下来,博主上份工作坐标厦门,风景好的简直随便拍照就是大片. 不废话了,机缘巧合, ...

  2. Eclipse 快捷键(mac)

    Option + Command + X: 运行Command + O:显示大纲Command + 1:快速修复Command + D:删除当前行Command + Option + ↓:复制当前行到 ...

  3. Arcpy处理修改shapefile FeatureClass 线要素坐标

    需求:在开发的webgis系统中需要将道路矢量数据与谷歌地图瓦片叠加,谷歌地图瓦片在国家测绘局的要求是进行了偏移处理的,人称“火星坐标系GCJ_02”,道路数据是WGS-84坐标系下的经纬度坐标,现在 ...

  4. [LC] 1099. Two Sum Less Than K

    Given an array A of integers and integer K, return the maximum S such that there exists i < j wit ...

  5. 吴裕雄--天生自然PYTHON学习笔记:python自动登录网站

    打开 www. 5 l eta . com 网站,如果己经通过某用户名进行了登录,那么先退出登录 . 登录该网站 的步骤一般如下 : ( 1 )单击右上角的“登录”按钮. ( 2 )先输入账号. ( ...

  6. centos jdk

    yum list java* yum install xxx -y java -version /* 可省略 */ vi /etc/profile export JAVA_HOME=/usr/lib/ ...

  7. 使用Cron表达式创建定时任务

    CronTriggerCronTrigger功能非常强大,是基于日历的作业调度,而SimpleTrigger是精准指定间隔,所以相比SimpleTrigger,CroTrigger更加常用.CroTr ...

  8. cs231n spring 2017 lecture7 Training Neural Networks II

    1. 优化: 1.1 随机梯度下降法(Stochasitc Gradient Decent, SGD)的问题: 1)对于condition number(Hessian矩阵最大和最小的奇异值的比值)很 ...

  9. caffe之那些依赖的库

    1. Boost库 Boost是一个可移植的,提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一.Boost强调对跨平台的支持,编译与平台无关.Caffe采用C++为主要开发语言 ...

  10. 一个自动递增生成目录和文件的cop文件类

    package com.hudong.util.orther; import java.io.File; import java.io.FileInputStream; import java.io. ...