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上下文根据配置查 ...
随机推荐
- Bata验收互评
小组的名字和链接 优点 缺点,bug报告 最终名次 编程题全队 ①限制用户重复注册同一个邮箱②注册之后可以弹出用户名,不用手动输入③细节考虑到位④面板可拖动,增删改查,还能添加成员 Q1:程序有什么具 ...
- python里实现DSL
以后用到的话可以参考如下链接: http://safehammad.com/downloads/domain-specific-languages-and-python-2011-04-21.pdf ...
- springboot + swagger的实体类属性注解
@Api:用在类上,说明该类的作用 @ApiOperation:用在方法上,说明方法的作用 @ApiImplicitParams:用在方法上包含一组参数说明 @ApiImplicitParam:用在@ ...
- spring cloud学习(四) Fegin 的使用
Feign 的使用 什么是Feign? Feign : Declarative REST clients. Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创 ...
- windows下python安装Numpy、Scipy、matplotlib模块
来源http://blog.csdn.net/Katrina_ALi/article/details/64922107 http://blog.csdn.net/qq_16633405/article ...
- js通过class获取元素
<!doctype html> <html> <head> <meta charset="utf-8"> <meta char ...
- 随机产生div背景颜色变化
使一个DIV在每次刷新后变化背景颜色,很容易想到JS的random()函数:通过每次刷新页面产生使背景rgb随机产生 <!doctype html> <html> <he ...
- 3.strcpy使用注意(3)
void test3(char * str1) { if(str1==NULL) { return; } char string[10]; if(strlen(str1)<=10) { strc ...
- 开发工具之play framework
Play!是一个full-stack(全栈的)Java Web应用框架,包括一个简单的无 状态MVC模型,具有Hibernate的对象持续,一个基于Groovy的模板引擎,以及建立一个现代Web应 ...
- 团队NABCD
NABCD 你的创意解决了用户的什么需求?(N) 每学期开学同学们都有相同的困难:我该选哪几门课?这门课到底是做什么的?有时候上一届的学长学姐会告诉我们他们觉得好的课,但这并不全面.所以我们需要一个平 ...