首页 > 其他分享 >How to set spring boot active profiles with maven profiles

How to set spring boot active profiles with maven profiles

时间:2022-12-19 15:03:56浏览次数:57  
标签:profile ... set spring profiles active properties

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.
​​<​​​​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​​​​>​​

  1. (这一步不需要)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.
​​<​​​​build​​​​>​​
​​<​​​​resources​​​​>​​
​​<​​​​resource​​​​>​​
​​<​​​​directory​​​​>src/main/resources</​​​​directory​​​​>​​
​​<​​​​filtering​​​​>true</​​​​filtering​​​​>​​
​​</​​​​resource​​​​>​​
​​</​​​​resources​​​​>​​
​​…​​
​​</​​​​build​​​​>​​

  1. Finally, add the following line to the application.properties.
​​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

 

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

 

其它方式的可参考:

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

 



标签:profile,...,set,spring,profiles,active,properties
From: https://blog.51cto.com/u_15147537/5952652

相关文章

  • JAG Spring Contest 2012 G PLAY in BASIC 题解
    提交链接其实就是个大模拟。首先对输入的串进行处理,把所有的命令分开,并把连续的停顿合并。为了方便,定义一个时间单位为全音符的\(\frac1{128}\),这样所有命令的持续时间都......
  • Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结
    HowtoconfigureSpringSecuritytoallowSwaggerURLtobeaccessedwithoutauthentication@ConfigurationpublicclassWebSecurityConfigurationextendsWebSecu......
  • 使用Ratpack与Spring Boot构建高性能JVM微服务
    在微服务天堂中Ratpack和SpringBoot是天造地设的一对。它们都是以开发者为中心的运行于JVM之上的web框架,侧重于生产率、效率以及轻量级部署。他们在服务程序的开发中带来了......
  • Spring MVC Integration,Spring Security
    ​​ http://docs.spring.io/spring-security/site/docs/4.2.0.RELEASE/reference/htmlsingle/#authorize-requests​​37.5 SpringMVCandCSRFIntegration37.5.1 Aut......
  • JAG Spring Contest 2012 G PLAY in BASIC 题解
    提交链接其实就是个大模拟。首先对输入的串进行处理,把所有的命令分开,并把连续的停顿合并。为了方便,定义一个时间单位为全音符的\(\frac1{128}\),这样所有命令的持续时间都......
  • docker-compose入门以及部署SpringBoot+Vue+Redis+Mysql(前后端分离项目)以若依前后端
    场景若依前后端分离版手把手教你本地搭建环境并运行项目:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662上面在搭建起来前后端分离版的项目后。......
  • Springboot整合Apache Dubbo
    ​​Springboot​​​整合​​ApacheDubbo​​一、通过​​Docker​​​安装​​Zookeeper​​​​docker-compose​​​的​​yaml​​文件version:'3'services:zoo......
  • spring事务
    spring事务Spring事务概述事务是一个不可分割的逻辑工作单元,具备ACID特性,实际工作中可借助Spring进行事务管理,Spring提供了两种事务管理方式,编程式事务和声明式事务,本......
  • Spring Cloud构建微服务架构(二)服务消费者
     NetflixRibbonisanInterProcessCommunication(IPC)cloudlibrary.Ribbonprimarilyprovidesclient-sideloadbalancingalgorithms.Apartfromtheclient-si......
  • Spring CredHub 教程学习
    SpringCredHub提供客户端支持,用于从CloudFoundry​平台中运行的CredHub服务器存储,检索和删除凭据。CredHub提供了一个HTTPAPI,用于安全地存储,生成,检索和删除各种类型的凭......