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.

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

 

相关文章
|
2月前
|
IDE Java Maven
Spring Boot之如何解决Maven依赖冲突Maven Helper 安装使用
Spring Boot之如何解决Maven依赖冲突Maven Helper 安装使用
50 2
|
2月前
|
Java Maven
SpringBoot用maven打出的jar只有几kb解决
SpringBoot用maven打出的jar只有几kb解决
148 0
|
2月前
|
Java Maven
SpringBoot项目的用maven插件打包报Test错误
SpringBoot项目的用maven插件打包报Test错误
|
4天前
|
IDE Java 数据库连接
如何使用Spring Boot Profiles进行环境配置管理
如何使用Spring Boot Profiles进行环境配置管理
|
4天前
|
XML Java 数据格式
SpringBoot Profiles特性
SpringBoot Profiles特性
|
2月前
|
Java Maven Spring
maven打包插件maven-jar-plugin与spring-boot-maven-plugin
该内容介绍了两个Maven打包插件:`spring-boot-maven-plugin`和`maven-jar-plugin`。`spring-boot-maven-plugin`是Spring Boot项目的默认打包工具,它会包含项目类文件、资源和依赖的jar,但不会解编译依赖。而`maven-jar-plugin`则用于创建普通JAR包,不包含依赖。文中还展示了两个插件打包后的效果差异,并强调了持续练习以掌握这些技能的重要性。
39 0
|
2月前
|
Java Maven Windows
小唐开始学 Spring Boot——(1)IDEA 2021.3.2和Maven的安装配置
小唐开始学 Spring Boot——(1)IDEA 2021.3.2和Maven的安装配置
|
2月前
|
Java Maven Spring
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)2
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)
84 2
|
2月前
|
Java Maven 开发工具
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)1
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)
44 2
|
2月前
|
Java Maven Kotlin
[AIGC] 请你写一遍博客介绍 “使用idea+kotinlin+springboot+maven 结合开发一个简单的接口“,输出markdown格式,用中文回答,请尽可能详细
[AIGC] 请你写一遍博客介绍 “使用idea+kotinlin+springboot+maven 结合开发一个简单的接口“,输出markdown格式,用中文回答,请尽可能详细
194 0