maven 利用 profile 进行多环境配置
我们在进行项目的多环境配置时,有很多种方式供我们选择,比如 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 进行多环境配置的更多相关文章
- 使用maven的profile构建不同环境配置
基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...
- Maven之profile实现多环境配置动态切换
一般的软件项目,在开发.测试及生产等环境下配置文件中参数是不同的.传统的做法是在项目部署的时候,手动修改或者替换这个配置文件.这样太麻烦了,我们可以用Maven的profile来解决这 ...
- (十一)Maven之profile实现多环境配置动态切换
原文链接:https://www.cnblogs.com/zeng1994/p/a442108012ffd6a97b22c63055b48fe9.html 一.多环境配置文件的放置 将不同环境下的配 ...
- maven学习(下)利用Profile构建不同环境的部署包
接上回继续,项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre) ...
- 通过maven profile 打包指定环境配置
背景 最近换了个新公司接手了一个老项目,然后比较坑的是这个公司的项目都没有没有做多环境打包配置,每次发布一个环境都要手动的去修改配置文件.今天正好有空就来配置下. 解决这个问题的方式有很多,我这里挑选 ...
- maven profile实现多环境配置
每次项目部署上线都需要手动去修改配置文件(比如数据库配置,或者一个自定义的配置)然后才能打包,很麻烦,网上找到 maven profile可以完成这个工作,记录如下: 环境:eclipse + spr ...
- 使用maven profile实现多环境配置相关打包
项目开发需要有多个环境,一般为开发,测试,预发,正式4个环境,通过maven可以实现按不同环境进行打包部署,命令为: mvn package -P dev 在eclipse中可以右击选项run con ...
- maven学习利用Profile构建不同环境的部署包
项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre).正式生产环 ...
- Maven 教程(18)— 利用 Profile 构建不同环境的部署包
原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79776257 接上回继续,项目开发好以后,通常要在多个环境部署,象我们公司多达5 ...
随机推荐
- Asis CTF 2016 b00ks理解
---恢复内容开始--- 最近在学习堆的off by one,其中遇到这道题,萌新的我弄了大半天才搞懂,网上的很多wp都不是特别详细,都得自己好好调试. 首先,这题目是一个常见的图书馆管理系统,虽然我 ...
- nginx实现前后台分离部署
2.1 前后台分离部署 (一) 组网图 (二) 简要说明: 如标题所示,至于为什么要前后台分离部署,个人理解的原因有三 (一) 便于部署 前台代码由ngin ...
- android 活动监听是否点击某个view
前述(写给做过web前端的人) 在web H5,如果如果判断当前是否点击某个元素,一般会这样写. window.addEventListener("touchstart",func ...
- 【翻译】.NET Core3.1发布
.NET Core3.1发布 我们很高兴宣布.NET Core 3.1的发布.实际上,这只是对我们两个多月前发布的.NET Core 3.0的一小部分修复和完善.最重要的是.NET Core 3.1是 ...
- sortColors
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 浅谈css样式之list-type
在我们的工作学习中,大多数人使用列表标签的时候总一般的选择是把list-type设置成none.不过可能很多人对于这个属性的细节并没有很深的了解.甚至会把list-type和list-type-sty ...
- 暗灰色的圆形按钮.html
宝宝 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title& ...
- 你知道,HTTPS用的是对称加密还是非对称加密?
1.引言 随着互联网安全意识的普遍提高,对安全要求稍高的应用中,HTTPS的使用是很常见的,甚至在1年前,苹果公司就将使用HTTPS作为APP上架苹果应用市场的先决条件之一(详见<苹果即将强制实 ...
- KVM http网络加载镜像报错(mount: wrong fs type, bad option, bad superblock on /dev/loop0)
curl: (23) Failed writing body (7818 != 16384)loop: module loadeddracut-initqueue[579]: mount: wrong ...
- 配置React Native的开发环境
本文转载自:http://mp.weixin.qq.com/s?__biz=MzIxNjEzNjUzOQ==&mid=402020148&idx=2&sn=ccad14a919 ...