How to set spring boot active profiles with maven profiles

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

 

时间: 2025-01-21 05:24:45

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

《Spring Boot官方指南》翻译邀请

学技术并发网始终建议同学们阅读原版官方文档,所以并发网每月都会组织大家翻译各种官方技术文档.9月份并发网组织大家翻译<Spring Boot官方指南>,欢迎有兴趣的同学参与. 如何领取  通过评论领取想要翻译的文章,每次领取一章,如Spring Boot Documentation,翻译完后再领取其他章节.领取完成之后,建议在一个星期内翻译完成,如果不能完成翻译,也欢迎你邀请其他同学和你一起完成翻译. 如何提交? 翻译完成之后请登录到并发网提交成待审核状态,会有专门的编辑校对后进行发布.校对完

使用Spring Boot构建RESTful Web服务以访问存储于Aerospike集群

Spring Boot是对Spring快速入门的强大工具.Spring Boot能够帮助你很容易地构建基于Spring的应 用. Aerospike是分布式和可复制的内存数据库,不管使用DRAM还是原生的flash/SSD,Aerospike都进行 了优化. Aerospike具有高可靠性并且遵循ACID.开发人员能够在不停止数据库服务的情况下,很快地将 数据库集群从两个节点扩展到二十个节点. 你所要构建的是什么 本文将会引领你使用Spring Boot创建一个简单的RESTful Web服务.

spring boot应用启动原理分析

spring boot quick start 在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启动的,不需要另外配置一个Web Server. 如果之前没有使用过spring boot可以通过下面的demo来感受下. 下面以这个工程为例,演示如何启动Spring boot项目: git clone git@github.com:hengyunabc/spring-boot-demo.git mvn spring-boot

Spring Cloud连载(3)Spring Boot简介与配置

本站小福利 点我获取阿里云优惠券 原文作者:杨大仙的程序空间 3 Spring Boot简介与配置   3.1 Spring Boot         Spring Cloud基于Spring Boot搭建,本小节将对Spring Boot作一个大致的讲解,读者知道Spring Boot作用即可. 3.1.1 Spring Boot简介         开发一个全新的项目,需要先进行开发环境的搭建,例如要确定技术框架以及版本,还要考虑各个框架之间的版本兼容问题,完成这些繁琐的工作后,还要对新项目

Spring Boot特性

1. SpringApplication SpringApplication 类是启动 Spring Boot 应用的入口类,你可以创建一个包含 main() 方法的类,来运行 SpringApplication.run 这个静态方法: public static void main(String[] args) { SpringApplication.run(MySpringConfiguration.class, args); } 运行该类会有如下输出: . ____ _ __ _ _ /\

一篇文章学会spring boot(包括jms和hessian的集成)

之前在学习spring cloud微服务的时候,由于spring cloud的基础是spring boot,因此曾简单地了解过spring boot,但也只是简单的了解过而已. 而现在,需要把struts2项目改为spring boot,一开始时以为是整个项目重构,不仅限于struts2部分,因此就相对更系统.更细致的学了一下spring boot. 整个过程由易到难,大概分成了这么些模块: 一.创建简单的spring boot web项目 很多时候学一个新的东西,都需要从最简单的地方开始,然后

用 Docker 构建、运行、发布一个 Spring Boot 应用

本文演示了如何用 Docker 构建.运行.发布来一个 Spring Boot 应用. Docker 简介 Docker 是一个 Linux 容器管理工具包,具备"社交"方面,允许用户发布容器的 image (镜像),并使用别人发布的 image.Docker image 是用于运行容器化进程的方案,在本文中,我们将构建一个简单的 Spring Boot 应用程序. 有关 Docker 的详细介绍,可以移步至 <简述 Docker> 前置条件 JDK 1.8+ Maven

spring boot项目中使用jpa的一个未解之谜

公司最近主要的工作就是把之前的一个项目进行几乎全面的重构,之所以说几乎全面,是因为除开业务逻辑外全部换血: 框架由spring+struts2+mybatis改为spring boot+jpa 数据库由sybase+h2改为oracle+redis 子系统之间的交互由activemq改为http 代码具体实现全部重写 对一个运行了若干年的项目进行这样的大动作,路程无疑是漫长而复杂的.在进行了一系列设计文档的编写.评审.修订.再评审之后,终于可以开始码代码了. 本以为码代码终于可以松一口气,没想到

Spring boot 通用配置文件模板

Spring boot 通用配置文件模板   001 # ===================================================================002 # COMMON SPRING BOOT PROPERTIES003 #004 # This sample file is provided as a guideline. Do NOT copy it in its005 # entirety to your own application.