spring 使用 maven profile
先看看 maven 定义 profile 的写法
<!-- profiles -->
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>dev</id>
<properties>
<profile.env>dev</profile.env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profile.env>test</profile.env>
</properties>
</profile>
<profile>
<id>product</id>
<properties>
<profile.env>product</profile.env>
</properties>
</profile>
</profiles>
然后在 spring.yml 里面这个么写

但是 spring 在运行的时候 已经和maven 没有关系了 这时候 ${ 变量名的写法 } 已经不生效了
备注: 这里 ${ 变量名 } 是maven 引用 变量的 写法。
比如:

这里的 ${serverA.version}_${profile.env} 分别是引用自 前面配置文件 中定义的 maven 变量 ${serverA.version} 来自 <properties> ${profile.env} 来自 profile

怎么才能让 spring一识别 maven的 表达式呢?我们知道 spring运行在maven 打包以后,所以 不能可能spring运行的时候还能读取到 maven的 变量,所以只能是在maven打包的时候替换文件。 这就是核心思想了。加上下面的代码就可以打包的时候替换了spring.yml 中 maven的 变量了 。
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
这个 代码 加在 <build> 里面就可以了
<!-- 指定jdk 的版本 -->
<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
上面 多了一个 修改 jdk 版本的 配置,我的另一篇 博客里面写了 修改 jdk 版本的 maven的 2 中配置方式。
如果需要零时切换 profile 可以在 jar 后面 更上 --spring.profiles.active=dev
java -jar aaa.jar --spring.profiles.active=dev
在 eclipse 中 run 右键 运行配置中指定

spring 使用 maven profile的更多相关文章
- [Spring Boot 系列] 集成maven和Spring boot的profile功能
由于项目的需要, 今天给spirng boot项目添加了profile功能.再网上搜索了一圈,也没有找到满意的参考资料,其实配置并不难,就是没有一个one stop(一站式)讲解的地方,所以有了写这篇 ...
- 集成maven和Spring boot的profile功能
思路:maven支持profile功能,当使用maven profile打包时,可以打包指定目录和指定文件,且可以修改文件中的变量.spring boot也支持profile功能,只要在applica ...
- 集成maven和Spring boot的profile 专题
maven中配置profile节点: <project> .... <profiles> <profile> <!-- 生产环境 --> <id& ...
- 004-集成maven和Spring boot的profile功能打包
参考地址:https://blog.csdn.net/lihe2008125/article/details/50443491 一.主要目标 1.通过mvn在命令行中打包时,可以指定相应的profil ...
- [Spring Boot 系列] 集成maven和Spring boot的profile 专题
maven中配置profile节点: <project> .... <profiles> <profile> <!-- 生产环境 --> <id& ...
- 集成maven和Spring boot的profile
如果在配置中勾选了多套配置,则以pom.xml文件中 profiles中 配置 最后一个配置为准. maven中配置profile节点: <project> .... <profi ...
- Spring Boot Maven Plugin(二):run目标
简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...
- maven profile实现多环境配置
每次项目部署上线都需要手动去修改配置文件(比如数据库配置,或者一个自定义的配置)然后才能打包,很麻烦,网上找到 maven profile可以完成这个工作,记录如下: 环境:eclipse + spr ...
- Spring 快速开始 Profile 和 Bean
和maven profile类似,Spring bean definition profile 有两个组件:声明和激活. [栗子:开发测试环境使用HyperSQL 生产环境使用JNDI上下文根据配置查 ...
随机推荐
- HDU-5050 Divided Land (二进制求GCD)
题目大意:将两个二进制数的GCD用二进制数表示出来. 题目分析:这道题可以用java中的大数类AC. 代码如下: import java.io*; import java.math.BigIntege ...
- ORACLE11G内存管理参数
今天,对ORACLE11G的几个内存参数看了一下,记录如下,大家可以参考: 1.首先,在ORACLE11G的INIT.ORA里,有“__”开头的参数,也就是以两个下划线开头的参数,这种参数应该是系统自 ...
- zk客户端的ClientCnxn类
ClientCnxn是客户端的类:该类管理zk客户端的socket io,维持一个可用服务器的列表. //ClientCnxn类 private final LinkedList<Packet& ...
- 【VUE】@click加上v-bind绑定切换类名及动画事件
好长的名字... 效果是 点击元素,通过改变类名的方式让其改变颜色+移动动画效果,这里用的是v-bind和@click 废话不说 show me the code! <div id=" ...
- 【转载】Android Bug分析系列:第三方平台安装app启动后,home键回到桌面后点击app启动时会再次启动入口类bug的原因剖析
前言 前些天,测试MM发现了一个比较奇怪的bug. 具体表现是: 1.将app包通过电脑QQ传送到手机QQ上面,点击安装,安装后选择打开app (此间的应用逻辑应该是要触发 [闪屏页Activity] ...
- 51nod1537
题解: 预处理每一个要变换几次,然后改成每一个要改变的次数-上一个要改变的次数 然后对于区间[l,r]修改,就是l++,r+1++ dp即可(据说可以o(n)) 代码: #include<bit ...
- python列表[]中括号
names = ['jack', 'rose', 'tom', 'jerry', 'jerry'] print(names) print(names[0]) names[0] = 'adam' # 改 ...
- 使用shake.js让你博客支持摇一摇
大家好,又到了随机文章的时间,请使用手机打开演示站点,然后像摇妹子一样摇晃手机,你会发现非常牛逼的事情,炫酷吧.该功能已经集成在Oconnor1.8中.本文主要讲解这货的原理. 首先需要下载shake ...
- hdu 1159 Common Subsequence (最长公共子序列 +代码)
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
- hibernate中3个重要的类 Configuration SessionFactory Session
配置类Configuration 主要负责管理hibernate的配置信息以及启动hibernate,在hibernate运行时,配置文件取读底层的配置信息,基本包括数据库驱动,url.usernam ...