使用maven的profile功能,我们可以实现多环境配置文件的动态切换,可参考我的上一篇博客。但随着SpringBoot项目越来越火,越来越多人喜欢用SpringBoot的profile功能。但是用SpringBoot的profile功能时,一般我们默认激活的profile肯定是开发环境的profile。当我们打成jar包后,如果在生产环境下运行,就需要在运行这个jar包的命令后面加个命令行参数来指定切换的profile。虽然这样很方便,但是容易忘记加这个参数。
    我们可以通过maven的profile功能和SpringBoot的profile功能结合使用。效果为:当maven打包时通过profile指定配置为test环境的配置,那么我们SpringBoot里面默认激活的就是test环境的配置。  这样我们只需要打包时指定profile后,直接运行jar就可以,不需要在命令行加参数了。这个效果就和我们普通web项目使用maven的profile的效果类似了。

一、思路

    (1)通过maven的profile功能,在打包的时候,通过-P指定maven激活某个pofile,这个profile里面配置了一个参数activatedProperties,不同的profile里面的这个参数的值不同
    (2)SpringBoot的application.properties文件里面spring.profiles.active填的值取上面maven的activatedProperties参数值。
  这样能实现的效果为:
示例一:
maven打包命令为 mvn clean package -P test
那么application.properties里面的spring.profiles.active值就是maven中 id为test的profile的activatedProperties参数值
示例二:
maven打包命令为 mvn clean package -P product
那么application.properties里面的spring.profiles.active值就是maven中 id为product的profile的activatedProperties参数值
 
1
示例一:
2
    maven打包命令为   mvn clean package -P test    
3
    那么application.properties里面的spring.profiles.active值就是maven中 id为test的profile的activatedProperties参数值
4
示例二:
5
    maven打包命令为   mvn clean package -P product
6
    那么application.properties里面的spring.profiles.active值就是maven中 id为product的profile的activatedProperties参数值

二、案例

(1)项目结构介绍
    项目结构如下图所示,是个常见的SpringBoot项目结构,不同环境的propertis文件的后缀不同(见图中红框处)
(2)pom文件中配置maven的profile
    maven的profile的配置见下面代码
    注意:maven的profile中activatedProperties参数值需要和SpringBoot的不同环境Properties文件的后缀一样。
    比如开发环境的Properties的文件名为application-develop.properties,那么maven中develop的profile里面的activatedProperties参数值就应该是develop
    <profiles>
<profile>
<!-- 开发 -->
<id>develop</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>develop</activatedProperties>
</properties>
</profile>
<profile>
<!-- 测试 -->
<id>fuy</id>
<properties>
<activatedProperties>fuy</activatedProperties>
</properties>
</profile>
<profile>
<!-- 生产 -->
<id>production</id>
<properties>
<activatedProperties>production</activatedProperties>
</properties>
</profile>
</profiles>
x
 
1
    <profiles>
2
        <profile>
3
            <!-- 开发 -->
4
            <id>develop</id>
5
            <activation>
6
                <activeByDefault>true</activeByDefault>
7
            </activation>
8
            <properties>
9
                <activatedProperties>develop</activatedProperties>
10
            </properties>
11
        </profile>
12
        <profile>
13
            <!-- 测试 -->
14
            <id>fuy</id>
15
            <properties>
16
                <activatedProperties>fuy</activatedProperties>
17
            </properties>
18
        </profile>
19
        <profile>
20
            <!-- 生产 -->
21
            <id>production</id>
22
            <properties>
23
                <activatedProperties>production</activatedProperties>
24
            </properties>
25
        </profile>
26
    </profiles>
(3)application.properties中的配置
    在application.properties文件中配置SpringBoot默认激活的propertis文件。这时候spring.profiles.active取上面maven的profile里面配置的activatedProperties的值,这个取值要用@符号来取。具体见下面代码
spring.profiles.active=@activatedProperties@
 
1
spring.profiles.active=@activatedProperties@
(4)如何打包
    打包时用 mvn clean package -P profile的id
        如果不加-P参数,那么默认就是<activeByDefault>true</activeByDefault>所在的profile
(5)效果图
        当我们打包命令为mvn clean package -P production 时,解压后的jar包中application.properties配置文件中spring.profiles.active的值自动变成了production
        

三、小结

(1)该方式优点:打包后不需要通过命令行参数来切换不同环境的配置文件,把指定环境的这一步放到了maven打包的命令上
(2)该方式其实是利用了maven的profile功能和SpringBoot的profile相结合使用

四、参考链接


    

Maven的porfile与SpringBoot的profile结合使用详解的更多相关文章

  1. profile的使用详解

    前言 在开发过程中,我们的项目会存在不同的运行环境,比如开发环境.测试环境.生产环境,而我们的项目在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置, ...

  2. maven之(六)setting.xml的配置文件详解

    setting.xml配置文件 maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.h ...

  3. Maven使用笔记(四)pom.xml配置详解

    pom.xml文件配置详解 --声明规范 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  4. SpringBoot第二篇:配置文件详解一

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10837594.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   Sprin ...

  5. IntelliJ IDEA 2017版 spring-boot加载jsp配置详解(详细图文实例)

    一.创建项目 (File--->New-->Project) 2.项目配置内容 3.选择配置项目的Group包名,Artifact项目名称 4.选择项目类型为web类型 5.创建成功,点击 ...

  6. SpringBoot与Mybatis整合实例详解

    介绍 从Spring Boot项目名称中的Boot可以看出来,SpringBoot的作用在于创建和启动新的基于Spring框架的项目,它的目的是帮助开发人员很容易的创建出独立运行的产品和产品级别的基于 ...

  7. SpringBoot入门及YML文件详解

    SpringBoot 简介 微框架,与 Spring4 一起诞生,基于约定.生来为了简化 spring 的配置 优点 可以快速的上手,整合了一些子项目(开源框架或者第三方开源库) 可以依赖很少的配置快 ...

  8. springboot(四):thymeleaf使用详解

    在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...

  9. SpringBoot (四) :thymeleaf 使用详解

    原文出处: 纯洁的微笑 在上篇文章< springboot(二):web综合开发 >中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymele ...

随机推荐

  1. 显示Linux进程打开了哪些文件? 用lsof命令

    1.lsof abc.txt --> 显示开启文件 abc.txt的进程. 2.lsof -c abc  ---> 显示abc进程现在打开的文件. 3.lsof -c -p 1234 -- ...

  2. (网页)thinkpad 笔记本开机看看坏了没?

    嗯!只是想看一下是不是块砖头. 出现许多细节问题: 1.不用注册联机的乱七八糟的,验证还慢,直接选择脱机的. 2.推荐密码:zaq!2wsx直接进去,以后可以改. 3.有很多选项都可以不勾选,(只勾选 ...

  3. 产品经理说|AIOps 让告警管理变得更智能

    AIOps 人工智能和IT运营支撑 Ops 之间的故事,愈演愈烈,已经成为当今运维圈的热门话题,我打算从2篇文档分享我们在 AIOps 上一些探索和实践.(本篇)为什么事件(告警)处理需要 AIOps ...

  4. 关于正餐智能POS6.0.1.1改版后,点击反结账进入点菜界面后无法进行加菜的FAQ

    适用版本:智能POS正餐V6.0.1.1+适用情况:点击反结账进入点菜界面后无法进行加菜.原因:为让报表统计的数据更准确解决方案:1.点击反结账,输入用户密码,进入点菜界面. 2.点击结算,进入收银台 ...

  5. mssql 一次向表中插入多条数据的方法分享 (转自:http://www.maomao365.com/?p=6058)

    转自:http://www.maomao365.com/?p=6058) <span style="font-size:16px;font-weight:bold;"> ...

  6. python 实现网页 自动登录

    完整代码: 1 from apscheduler.schedulers.blocking import BlockingScheduler 2 from selenium import webdriv ...

  7. Linux 小知识翻译 - 「UTC 和 JST」

    这周解释下「UTC 和 JST」. 地球上的国家之间是存在时差的.然而互联网是遍布全球的网络,跨国境的数据传输是很频繁的.因此,互联网上的服务器就需要一个共通的时间. 实际上,互联网上的服务器,大部分 ...

  8. Django forms 关于select和checkbox设置初始选中值

    Django的forms和models一样很牛逼.他有两种功能,一是生成form表单,还有就是form表单的验证. 这里主要说一下生成form表单时经常用到的需要设置 初始值 / 默认值 的情况. 1 ...

  9. spark program guide

    概述 Spark 应用由driver program 组成,driver program运行用户的主函数,在集群内并行执行各种操作 主要抽象RDD: spark提供RDD,是贯穿整个集群中所有节点的分 ...

  10. js屏蔽/过滤 特殊字符,输入就删除掉,实时删除,

    原文:http://niuxiaoxia870563296.iteye.com/blog/1828189 1.替换方法: <input type="text" class=& ...