先看看 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的更多相关文章

  1. [Spring Boot 系列] 集成maven和Spring boot的profile功能

    由于项目的需要, 今天给spirng boot项目添加了profile功能.再网上搜索了一圈,也没有找到满意的参考资料,其实配置并不难,就是没有一个one stop(一站式)讲解的地方,所以有了写这篇 ...

  2. 集成maven和Spring boot的profile功能

    思路:maven支持profile功能,当使用maven profile打包时,可以打包指定目录和指定文件,且可以修改文件中的变量.spring boot也支持profile功能,只要在applica ...

  3. 集成maven和Spring boot的profile 专题

    maven中配置profile节点: <project> .... <profiles> <profile> <!-- 生产环境 --> <id& ...

  4. 004-集成maven和Spring boot的profile功能打包

    参考地址:https://blog.csdn.net/lihe2008125/article/details/50443491 一.主要目标 1.通过mvn在命令行中打包时,可以指定相应的profil ...

  5. [Spring Boot 系列] 集成maven和Spring boot的profile 专题

    maven中配置profile节点: <project> .... <profiles> <profile> <!-- 生产环境 --> <id& ...

  6. 集成maven和Spring boot的profile

    如果在配置中勾选了多套配置,则以pom.xml文件中 profiles中  配置 最后一个配置为准. maven中配置profile节点: <project> .... <profi ...

  7. Spring Boot Maven Plugin(二):run目标

    简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...

  8. maven profile实现多环境配置

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

  9. Spring 快速开始 Profile 和 Bean

    和maven profile类似,Spring bean definition profile 有两个组件:声明和激活. [栗子:开发测试环境使用HyperSQL 生产环境使用JNDI上下文根据配置查 ...

随机推荐

  1. throw new OAException执行了,却没有正常抛出异常!

    try { if ("E".equals(returnStatus)) { throw new OAException(returnMessage, OAException.ERR ...

  2. Oracle OAF 应用构建基础之实现控制器 (转)

    原文地址: Oracle OAF 应用构建基础之实现控制器 设计一个OA Controller 如OA Framework Page解析中所描述的,OA Controller定义了web beans的 ...

  3. @ConditionalOnProperty 详解

    @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @Documented @Con ...

  4. 42. Trapping Rain Water *HARD*

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. Logger.getLogger()和LogFactory.getLog()的区别

    第一.Logger.getLogger()和LogFactory.getLog()的区别 1.Logger.getLogger()是使用log4j的方式记录日志: 2.LogFactory.getLo ...

  6. UVALive 5107 dfs暴力搜索

    题目链接:A hard Aoshu Problem DES:给三个字符串,包含的字符是A-E范围内的.长度都不超过8.每个字符可以而且只可以匹配一个数字.两个字符不能匹配相同的数字.前两个式子之间可以 ...

  7. cas AuthenticationFilter

    AuthenticationFilter *** 这个类的作用:判断是否已经登录,如果没有登录则根据配置的信息来决定将跳转到什么地方 *** casServerLoginUrl:定义cas 服务器的登 ...

  8. 基于资源名的MVC权限控制

    在程序复杂程度不断上升的过程中,无可避免需要触碰到权限控制,而权限控制又与业务逻辑紧紧相关,市场上出现了大量的权限控制产品,而程序的开发,讲究去繁化简的抽象,在我的开发过程中,逐渐发现程序的权限控制核 ...

  9. windows : Jmeter自动化测试-eclipse+maven+jmeter

    前提 在window上已经安装maven并且在eclipse中配置好了maven,如果没有配置,参考文章   windows上安装maven及eclipse中配置maven 一.创建一个包含jmete ...

  10. WiFi Pineapple的Karma攻击与原理探究

    WiFi钓鱼是一种被炒烂了的安全威胁,你几乎每天都能从电视.网络媒体中看到因误连了恶意WiFi导致大笔资金失窃的案例,并且很多媒体语不惊人死不休的报道风格总是能把当事人的受害过程删减修饰到灵异的程度. ...