In the previous post you could read about separate Spring Boot builds for a local development machine and public environments. 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 the Resources Plugin. 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 Spring Boot 1.3 replaced the default Resources Plugin syntax 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 the official documentation. 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:

<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
<configuration>
<profiles>
<profile>foo</profile>
<profile>bar</profile>
</profiles>
</configuration>
...
</plugin>
...
</plugins>
...
</build>
...
</project>

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

How to set spring boot active profiles with maven profiles的更多相关文章

  1. Spring boot项目分环境Maven打包,动态配置文件,动态配置项目

    Spring boot Maven 项目打包 使用Maven 实现多环境 test dev prod 打包 项目的结构 在下图中可用看出,我们打包时各个环境需要分开,采用 application-环境 ...

  2. Spring Boot 多模块与 Maven 私有仓库

    前言 系统复杂了,抽离单一职责的模块几乎是必须的:若需维护多个项目,抽离公用包上传私有仓库管理也几乎是必须的.其优点无需赘述,以下将记录操作过程. 1. 多模块拆分 在.NET 中由于其统一性,实现上 ...

  3. spring boot 项目打包到maven仓库供其它模块使用

    在对spring boot项目进行打包发布的时候发现其它spring boot项目服务真正引用使用该spring boot包中的类 需对打包插件做如下修改: <build> <plu ...

  4. 一、Spring Boot系列:通过Maven创建第一个项目

    1.打开idea选择创建工程 2.创建maven工程,同时选择jdk1.8 注意:不需要勾选其他选项 3.填写项目名称 4.创建好maven项目后,在pom.xml文件中导入Spring Boot需要 ...

  5. idea spring boot docker 多项目 maven 编译

    1,重复的model [INFO] Scanning for projects... [ERROR] [ERROR] Some problems were encountered while proc ...

  6. [DEBUG] spring boot在eclipse中用maven打包成jar访问templates报500错误

    更新:打war包的话只要把html文件放在resources/templates下即可,根本不需要放外面. 配置application.yml和templates放外面这种做法,打war包确实不行. ...

  7. Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip

    spring-boot-assembly 在spring boot项目中使用maven profiles和maven assembly插件根据不同环境打包成tar.gz或者zip 将spring bo ...

  8. 一文掌握 Spring Boot Profiles

    Spring Boot Profiles 简介 Profile 的概念其实很早在 Spring Framework 就有了,在 Spring Framework 3.1 版本引入了注解 @Profil ...

  9. 源码解读 Spring Boot Profiles

    前言 上文<一文掌握 Spring Boot Profiles> 是对 Spring Boot Profiles 的介绍和使用,因此本文将从源码角度探究 Spring Boot Profi ...

随机推荐

  1. Netflix公布个性化和推荐系统架构

    Netflix的推荐和个性化功能向来精准,前不久,他们公布了自己在这方面的系统架构. 3月27日,Netflix的工程师Xavier Amatrain和Justin Basilico在官方博客发布文章 ...

  2. DB 查询分析器 6.03 在Windows 8 上安装与运行演示

           DB 查询分析器 6.03 在Windows 8 上安装与运行演示 马根峰                ( 广东联合电子服务股份有限公司, 广州 510300) 摘要          ...

  3. Gradle 1.12用户指南翻译——第四十章. ANTLR 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  4. mybatis源码之StatementHandler

    /** * @author Clinton Begin */ public interface StatementHandler { Statement prepare(Connection conn ...

  5. STM32之使用库函数驱动LED灯

    一.熟悉GPIO结构体 以下这个结构体是我从官方手册中获取的: typedef struct { u16 GPIO_Pin; GPIOSpeed_TypeDef GPIO_Speed; GPIOMod ...

  6. 初探linux子系统集之led子系统(二)

    巴西世界杯,德国7比1东道主,那个惨不忍睹啊,早上起来看新闻,第一眼看到7:1还以为点球也能踢成这样,后来想想,点球对多嘛6比1啊,接着就是各种新闻铺天盖地的来了.其实失败并没有什么,人生若是能够成功 ...

  7. LambdaToSql 发布 兰姆达转换sql

    文档目录索引 查询.函数.分组.排序.分页 添加 Insert into 编辑 Update set 删除 Delete 生成实体 内置常用工具类库  文档完善中... 事务处理 Join 连接查询 ...

  8. 《转》iOS 平台 Cocos2d-x 项目接入新浪微博 SDK 的坑

    最近在做一个 iOS 的 cocos2d-x 项目接入新浪微博 SDK 的时候被“坑”了,最后终于顺利的解决了.发现网上也有不少人遇到一样的问题,但是能找到的数量有限的解决办法写得都不详细,很难让人理 ...

  9. java虚拟机的类加载机制

    引言 我们写的代码是放在.java文件中,经过编译器编译后,转成.class文件.Class文件是一串二进制流,它可以被各平台的虚拟机所接受,实现跨平台.      虚拟机将描述类的数据从class文 ...

  10. WebLogic域配置策略

    WebLogic域配置策略--手动和模板选项,第一部分 域含有BEA WebLogic Server实例的配置信息.它包含有关服务器.集群和机器的配置信息.域还含有关于资源,例如Java数据库连接(J ...