Maven的porfile与SpringBoot的profile结合使用案例详解

作者:zeng1994 时间:2023-11-14 00:07:59 

使用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)项目结构介绍 项目结构如下图所示,是个常见的SpringBoot项目结构,不同环境的propertis文件的后缀不同(见图中红框处)

Maven的porfile与SpringBoot的profile结合使用案例详解

(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>

(3)application.properties中的配置

    在application.properties文件中配置SpringBoot默认激活的propertis文件。这时候spring.profiles.active取上面maven的profile里面配置的activatedProperties的值,这个取值要用@符号来取。具体见下面代码


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

Maven的porfile与SpringBoot的profile结合使用案例详解

三、小结

(1)该方式优点:打包后不需要通过命令行参数来切换不同环境的配置文件,把指定环境的这一步放到了maven打包的命令上

(2)该方式其实是利用了maven的profile功能和SpringBoot的profile相结合使用

四、参考链接

(1)https://www.cnblogs.com/zeng1994/p/a442108012ffd6a97b22c63055b48fe9.html

(2)http://dolszewski.com/spring/spring-boot-properties-per-maven-profile/

来源:https://www.cnblogs.com/zeng1994/p/06917ed3b98677fa7a1b0f74de16c3be.html

标签:Maven,porfile,SpringBoot,使用
0
投稿

猜你喜欢

  • Mybatis配置返回为修改影响条数方式

    2021-10-26 12:05:05
  • Java链表(Linked List)基本原理与实现方法入门示例

    2021-10-12 05:49:14
  • C#实现扫描枪扫描二维码并打印(实例代码)

    2023-02-26 18:02:56
  • idea企业开发之新建各类型项目的详细教程

    2023-02-28 05:27:11
  • Java 将PPT幻灯片转为HTML文件的实现思路

    2022-12-12 12:00:35
  • Java动态添加view的方法

    2023-06-11 04:20:38
  • 最小树形图模板朱刘算法分享

    2023-11-07 07:04:38
  • Android RadioGroup多行显示效果 解决单选问题

    2023-05-28 18:23:55
  • Mybatis批量插入index out of range错误的解决(较偏的错误)

    2022-06-11 01:11:51
  • Java判断线程池线程是否执行完毕

    2023-10-18 12:09:29
  • C#使用Chart绘制曲线

    2023-03-12 19:08:56
  • C# WinForm调用Shell_NotifyIcon的示例代码

    2021-07-17 16:04:41
  • 通过Java查看程序资源占用情况

    2023-11-10 02:01:19
  • Android学习教程之圆形Menu菜单制作方法(1)

    2022-05-23 03:08:50
  • java 类加载与自定义类加载器详解

    2022-05-21 04:31:31
  • Android编程实现google消息通知功能示例

    2023-02-02 20:00:27
  • Android开发Jetpack组件DataBinding用例详解

    2021-10-08 02:17:17
  • java简单列出文件夹下所有文件的方法

    2022-12-23 19:25:05
  • Windows7下的Java运行环境搭建过程图解

    2022-03-14 19:40:59
  • C#实现XML文件与DataTable、Dataset互转

    2021-06-07 04:00:27
  • asp之家 软件编程 m.aspxhome.com