我们在进行项目的多环境配置时,有很多种方式供我们选择,比如 SpringBoot 自带的 application-dev.yml、maven 的 profile 等。这里介绍的就是如何利用 profile 进行多环境配置。

首先,在 pom.xml 中添加你需要的 profile 配置:

    <profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>publish</id>
<properties>
<env>publish</env>
</properties>
</profile>
<!-- 本地环境 -->
<profile>
<id>local</id>
<properties>
<env>local</env>
</properties>
<!--默认启用-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>

profiles 里面配置了多个 profile 文件,即 dev、publish、local 环境,<env> 标签是为了切换环境,激活不同的环境需要。<activeByDefault> 设置为 true 表明:该 profile 是默认激活状态。

接下来,我们要将 <resource> 的 <filtering> 标签设置为 true,表示启用参数化值来替换标记,而参数化值源于 filter 标签中的 properties 文件。以下是原文解释:

 Whether resources are filtered to replace tokens with parameterised values or not.
The values are taken from the <code>properties</code> element and from the properties in the files listed
in the <code>filters</code> element.
 <build>
<!-- 指定使用filter -->
<filters>
<filter>src/main/resources/profiles/${env}/env.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<excludes>
<exclude>profiles/**</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
</build>

<filter> 中引用了 <profile> 的 env 属性,表示读取哪个环境变量的值,不同的 properties 文件中配置了不同环境的值:

前面说到 <filtering>true</filtering> 表示启用参数化值来替换标记,这是什么意思呢?我们来看看 application.yml 中要怎么表示?

server:
port: 8080
tomcat:
max-threads: 800
uri-encoding: UTF-8 spring:
redis:
host: ${spring.redis.host}
timeout: ${spring.redis.timeout}
pool:
max-idle: ${spring.redis.pool.max-idle}
max-active: ${spring.redis.pool.max-active}
password: ${spring.redis.password}
database: ${spring.redis.database}

那么问题来了,maven 怎么认识到 ${*} 这个符号是标记呢?这个标记符号是在 <plugin> 中的 <delimiter> 设置的,其中 <nonFilteredFileExtension> 表示不需要替换的文件扩展名。

        <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<delimiters>
<delimiter>${*}</delimiter>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<nonFilteredFileExtension>store</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>

因此,整个流程应该是这样进行的:执行 maven compile 命令, <resource> 读取 <filter> 中 properties 的属性值,然后替换 src/main/resources 下中的标记 — 诸如 ${spring.redis.host} 这些。

最后,只剩下一个问题了,怎么切换环境呢?如果你开发的工具是 IDEA,直接在旁边窗口切换即可:



如果使用命令行编译,加上 -P 选择 profile 即可,如下:

clean -U package -P dev -DskipTests=true -f pom.xml

maven 利用 profile 进行多环境配置的更多相关文章

  1. 使用maven的profile构建不同环境配置

    基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...

  2. Maven之profile实现多环境配置动态切换

            一般的软件项目,在开发.测试及生产等环境下配置文件中参数是不同的.传统的做法是在项目部署的时候,手动修改或者替换这个配置文件.这样太麻烦了,我们可以用Maven的profile来解决这 ...

  3. (十一)Maven之profile实现多环境配置动态切换

    原文链接:https://www.cnblogs.com/zeng1994/p/a442108012ffd6a97b22c63055b48fe9.html 一.多环境配置文件的放置  将不同环境下的配 ...

  4. maven学习(下)利用Profile构建不同环境的部署包

    接上回继续,项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre) ...

  5. 通过maven profile 打包指定环境配置

    背景 最近换了个新公司接手了一个老项目,然后比较坑的是这个公司的项目都没有没有做多环境打包配置,每次发布一个环境都要手动的去修改配置文件.今天正好有空就来配置下. 解决这个问题的方式有很多,我这里挑选 ...

  6. maven profile实现多环境配置

    每次项目部署上线都需要手动去修改配置文件(比如数据库配置,或者一个自定义的配置)然后才能打包,很麻烦,网上找到 maven profile可以完成这个工作,记录如下: 环境:eclipse + spr ...

  7. 使用maven profile实现多环境配置相关打包

    项目开发需要有多个环境,一般为开发,测试,预发,正式4个环境,通过maven可以实现按不同环境进行打包部署,命令为: mvn package -P dev 在eclipse中可以右击选项run con ...

  8. maven学习利用Profile构建不同环境的部署包

    项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre).正式生产环 ...

  9. Maven 教程(18)— 利用 Profile 构建不同环境的部署包

    原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79776257 接上回继续,项目开发好以后,通常要在多个环境部署,象我们公司多达5 ...

随机推荐

  1. 2019-10-30,Hack The Box 获取邀请码

    一.快速获取邀请码方法 1,使用curl请求下面的地址curl -X POST https://www.hackthebox.eu/api/invite/generate 2,在返回结果的code部分 ...

  2. linux网络配置(ifcfg)

    将linux主机接入到网络需要配置哪些配置项? IP/NETMASK:本地通信. 路由(网管):跨网络通信. DNS服务器地址:基于主机名通信. DNS服务器有三种:主/备用DNS服务器/第三备份dn ...

  3. 【Android - 控件】之MD - CardView的使用

    CardView是Android 5.0新特性——Material Design中的一个布局控件,可以通过属性设置显示一个圆角的类似卡片的视图. 1.CardView的属性: app:cardCorn ...

  4. Django4模型(操作数据库)

    模型入门同步数据库的两个指令创建模型注意事项1.外键ForeignKey 模型入门 同步数据库的两个指令 python manage.py makemigrations python manage.p ...

  5. sed 使用介绍

    第6周第4次课(4月26日) 课程内容: 9.4/9.5 sed 9.4/9.5 sed 操作实例如下 sed和grep比较起来,sed也可以实现grep的功能,但是没有颜色显示,sed强项是替换一些 ...

  6. SpringBoot和SpringCloud的版本对应关系

    1.详细的SpringBoot和SpringCloud对应的关系: Spring官方对应关系 2.springCloud与各组件的版本对应关系 官方文档

  7. JSON.parse() 报错和一些解决方法

    js 报错 Unexpected end of JSON input,Unexpected token u in JSON at position 0 JSON 通常用于与服务端交换数据. 在接收服务 ...

  8. 关于SQL Server 中日期格式化若干问题

    select CONVERT(varchar, getdate(), 120 )2004-09-12 11:06:08 select replace(replace(replace(CONVERT(v ...

  9. Aria2GUI for macOS - 百度网盘高速下载

    目录 一. aria2gui 1.1 下载地址:aria2gui 1.2 安装 1.2.1 方式一:手动安装 1.2.2 方式二:Homebrew安装 二. YAAW for Chrome 2.1 下 ...

  10. Node.js 中 __dirname 和 ./ 的区别

    概要 __dirname 总是指向被执行 js 文件的绝对路径 在 /d1/d2/myscript.js 文件中写了 __dirname, 它的值就是 /d1/d2 . ./ 会返回你执行 node ...