博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to set spring boot active profiles with maven profiles
阅读量:5818 次
发布时间:2019-06-18

本文共 3931 字,大约阅读时间需要 13 分钟。

In the previous post you could read about . It’s highly possible that in addition to such setup you would like to load different Spring properties files based on the active Maven profile. In this note you will learn how to accomplish the desired result in a few easy steps.

Step-by-step guide

  1. The first thing you need is two properties files for keeping your configurations. The names of the files should match with the pattern application-{custom_suffix}.properties. Create them in the src/main/resources directory of your Maven project, next to the main application.properties file, which you’re going to use later to activate one of the others and to hold values shared by both profiles.

  2. Then it’s time to modify your pom.xml. You need to define a custom property in each of your Maven profiles and set their values to match with suffixes of corresponding properties files that you want to load with a particular profile. The following sample also marks the first profile to run by default, but it’s not mandatory.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <
    profile
    >
        
    <
    id
    >dev</
    id
    >
        
    <
    properties
    >
            
    <
    activatedProperties
    >dev</
    activatedProperties
    >
        
    </
    properties
    >
        
    <
    activation
    >
            
    <
    activeByDefault
    >true</
    activeByDefault
    >
        
    </
    activation
    >
    </
    profile
    >
    <
    profile
    >
        
    <
    id
    >release</
    id
    >
        
    <
    properties
    >
            
    <
    activatedProperties
    >release</
    activatedProperties
    >
        
    </
    properties
    >
    </
    profile
    >
  3. (这一步不需要)Next, in the build section of the same file, configure filtering for . That will allow you to insert properties defined in the previous step into any file in the resources directory, which is the subsequent step.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <
    build
    >
        
    <
    resources
    >
            
    <
    resource
    >
                
    <
    directory
    >src/main/resources</
    directory
    >
                
    <
    filtering
    >true</
    filtering
    >
            
    </
    resource
    >
        
    </
    resources
    >
        
    </
    build
    >
  4. Finally, add the following line to the application.properties.
    1
    spring.profiles.active=@activatedProperties@

    When the build is run, the Resources Plugin will replace the placeholder with the value of the property defined in the active Maven profile. After starting  your application, the Spring framework will load the appropriate configuration file based on the name of the active Spring profile, which is described by the value of the spring.profiles.active property. Note that  for filtered values and uses @activatedProperties@ instead of ${activatedProperties} notation.

Just build it

The main goal has been achieved and now you are able to load separate properties for different build profiles. If you’re wondering how Spring resolves properties files, you can read more about the topic in . In case of any issues, improvements, or questions, please don’t hesitate to leave a comment.

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

The active profiles to use for a particular application can be specified using the profiles argument. The following configuration enables the foo and bar profiles:

 

...
...
...
org.springframework.boot
spring-boot-maven-plugin
1.5.1.RELEASE
foo
bar
...
...
...
...

The profiles to enable can be specified on the command line as well, make sure to separate them with a comma, that is:

 

mvn spring-boot:run -Drun.profiles=foo,bar

mvn命令中使用 -Dspring.profiles.active参数来指定profile

java -jar命令中,需要使用 --spring.profiles.active来指定profile

http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html

 
其它方式的可参考:
https://my.oschina.net/yybear/blog/113755
http://stackoverflow.com/questions/25420745/how-to-set-spring-active-profiles-with-maven-profiles

https://dzone.com/articles/spring-profiles-or-maven

http://www.jianshu.com/p/948c303b2253

 

转载于:https://www.cnblogs.com/softidea/p/6375806.html

你可能感兴趣的文章
[分享]Ubuntu12.04安装基础教程(图文)
查看>>
WCF
查看>>
django 目录结构修改
查看>>
win8 关闭防火墙
查看>>
Android实例-录音与回放(播放MP3)(XE8+小米2)
查看>>
CSS——(2)与标准流盒模型
查看>>
MYSQL 基本SQL语句
查看>>
C#中的Marshal
查看>>
linux命令:ls
查看>>
Using RequireJS in AngularJS Applications
查看>>
hdu 2444(二分图最大匹配)
查看>>
shell编程笔记六:实现ll命令
查看>>
【SAP HANA】关于SAP HANA中带层次结构的计算视图Cacultation View创建、激活状况下在系统中生成对象的研究...
查看>>
[nodejs] nodejs开发个人博客(五)分配数据
查看>>
《Linux内核修炼之道》 之 高效学习Linux内核
查看>>
Java数据持久层框架 MyBatis之API学习九(SQL语句构建器详解)
查看>>
DevOps 前世今生 | mPaaS 线上直播 CodeHub #1 回顾
查看>>
iOS 解决UITabelView刷新闪动
查看>>
让前端小姐姐愉快地开发表单
查看>>
Web前端JQuery入门实战案例
查看>>