原文地址为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. Kali-tools

    title date tags layout Ubuntu Update Kali-Tools 2018-09-01 Linux post 创建shell文件 vim update-kali-sour ...

  2. getRandomInt getRandomString

    import java.util.concurrent.ThreadLocalRandom; private static final String AB = "ABCDEFGHIJKLMN ...

  3. Linux基础常用命令大全学习

    1.ls命令 就是list的缩写,通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限(包括目录.文件夹.文件权限)查看目录信息等等 常用参数搭配: ls -a 列出目录所有文 ...

  4. 体验vSphere 6之1-安装VMware ESXi 6 RC版(转载)

    体验vSphere 6之1-安装VMware ESXi 6 RC版 在2015年,各个公司都会发布一系列新的产品,例如Microsoft会发布Windows 10,VMware会发布vSphere 6 ...

  5. Mybatis/ibatis基础知识

    Tip:mapper.xml中sql语句不允许出现分号! 1.#和$符号的区别 #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是 ...

  6. Java完成生产者消费者模型

    生产者和消费者模型,是多线程中的典型模型,这里使用Java完成该模型 ServerTest.java 生产者代码 package com.orange.threadmodel; import java ...

  7. Apache2配置腾讯云SSL证书

    首先去腾讯云申请免费的SSL证书,下载下来解压后里面有一个Apache文件夹,里面有三个文件,接下来会用到. 上传证书 将上一步的三个文件上传到/etc/ssl里 启用SSL模块 启用a2enmod ...

  8. POJ 3678 2-SAT

    题意:有n个顶点里面可以放数字1或0,给m个限制,每个限制给出两个顶点编号和两编号内数字运算后的结果 思路:很直接的2-SAT,每个点分为1和0两种情况,按限制要求建边,跑tarjan然后判断点是否在 ...

  9. Drools 7.15.0 docker容器方式部署

    关于drools的相关介绍就不再赘述了,关于drools网上的资料都很少,或者都有些老了,最近折腾了一下,记录下安装部署的过程,希望能节省下大家的时间. 一.快速部署 1.拉取基础镜像,命令如下: d ...

  10. 对Java tutorial-examples中hello2核心代码分析

    1.在hello2中有两个.java源文件分别是GreetingServlet.Java和ResponseServlet.jva文件主要对以下核心代码做主要分析. String username = ...