Spring Boot Profile 与Maven Profile 集成实践

Spring Boot Profile 与Maven Profile 集成实践

文章目录
  1. 1. 摘要
  2. 2. 集成的意义
  3. 3. Maven Filter
    1. 3.1. Filtering
    2. 3.2. Spring Boot Resource 插件的默认配置
  4. 4. Spring Boot Profile
    1. 4.1. Spring Maven 默认配置
  5. 5. 集成
    1. 5.1. 工程结构
      1. 5.1.1. 演示实例的默认工程结构为:
      2. 5.1.2. jdbc.properties
      3. 5.1.3. application.properties
      4. 5.1.4. application-${profile}.properties
    2. 5.2. pom 中定义Profile 参数
    3. 5.3. 覆盖默认Resource 插件配置
    4. 5.4. Filter 的使用
    5. 5.5. 采用Maven 激活Spring Profile
    6. 5.6. 打包部署
      1. 5.6.1. 运行Maven 指令打包
      2. 5.6.2. 结果查看
    7. 5.7. 源代码
    8. 5.8. 总结
  6. 6. 好书推荐
  7. 7. 参考

摘要

在现代的项目开发中多人协作、多环境部署已经是必不可少的软件开发方式,笔者目前正在开发的一个基于Spring Boot 项目环境就有四套之多,包括(本地、测试、演练、生产)。尤其是现代的大型项目开发,构建复杂、参与人数众多等因素,使得高效的构建工具必不可少。而Maven 正是这样的一款优秀的Java工程构建工具。
本文主要介绍Maven Profile 与Spring Boot Profile 集成使用的方式。一句话概括就是两种类型的Profile 能够实现的功能既有不同也有交叉,本文的目的就是要充分的使用各自的优势实现项目的合理构建。

集成的意义

Maven 作为构建工具,其侧重的粒度更大一些,偏重与项目的整体环境配置、项目的依赖管理以及各种环境下的配置文件管理。当然Maven 也能够实现属性的配置。
Spring Boot Profile 作为Spring 提供的功能,由Spring 最核心的IoC 功能可知,其更加侧重于Spring Beans 的多环境配置以及属性字段的配置。
因而,集成的意义就在采用Maven 来实现多环境下的配置文件管理,依赖管理等,而Spring Boot Profile 来实现属性,以及Spring Boot 自动装配的Bean 的属性的配置。

Maven Filter

Filtering

Maven Filter 是Maven Resource 插件提供的利用环境变量、pom文件定义的属性以及指定的*.properties配置文件里的属性来替换其他文件中的占位符,如${jdbc.driverClassName}。在本集成实例中就会分别读取不同Profile 下的数据库连接配置文件。替换application.properties 中的占位符。

Spring Boot Resource 插件的默认配置

在构建基于Spring Boot 项目时,就会继承自spring-boot-starter-parent,查看该文件会得到如下内容:

       
       
1
2
3
4
5
6
7
8
9
10
11
       
       
<plugin>
<groupId>org.apache.maven.plugins </groupId>
<artifactId>maven-resources-plugin </artifactId>
<version>2.6 </version>
<configuration>
<delimiters>
<delimiter>${resource.delimiter} </delimiter>
</delimiters>
<useDefaultDelimiters>false </useDefaultDelimiters>
</configuration>
</plugin>

${resource.delimiter}的值在该文件的开始位置已经将默认的$ 修改为@,定义如下:

       
       
1
2
       
       
<resource.delimiter>@ </resource.delimiter>
<!-- delimiter that doesn't clash with Spring ${} placeholders -->

Spring Boot Profile

Spring Maven 默认配置

同理,在spring-boot-starter-parent 文件中,Spring Boot 对Resource 进行了配置,包括需要被打包进jar&war 的application* 文件:

       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
       
       
<build>
<!-- Turn on filtering by default for application properties -->
<resources>
<resource>
<directory>${basedir}/src/main/resources </directory>
<filtering>true </filtering>
<includes>
<include>**/application*.yml </include>
<include>**/application*.yaml </include>
<include>**/application*.properties </include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources </directory>
<excludes>
<exclude>**/application*.yml </exclude>
<exclude>**/application*.yaml </exclude>
<exclude>**/application*.properties </exclude>
</excludes>
</resource>
</resources>
</build>

从配置文件中可以看到Spring Boot 对配置文件的默认打包设定。

集成

本次实现Spring Boot Profile 与Maven Profile 的集成主要有如下目的:

  • 实现不同环境下(profile)将不同目录下的jdbc.properties 文件打包到classpath 下
  • 采用jdbc.properties 文件中配置的数据库连接信息替换application.properties 文件中的通配符@*@
  • 实现根据选中的Maven Profile 激活Spring Boot Profile的属性spring.profiles.active

工程结构

演示实例的默认工程结构为:

Spring Boot Maven Profile 集成项目结构Spring Boot Maven Profile 集成项目结构

jdbc.properties

       
       
1
2
3
4
       
       
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=release

application.properties

application.properties 文件中需要被替换通配符的数据库连接信息:

       
       
1
2
3
4
5
       
       
# config datasource

application-${profile}.properties

每一个application-${profile}.properties 文件中都定义了一个属性:

       
       
1
2
3
4
5
6
7
8
       
       
# config dev properties
self.hello=hello world dev
# config release properties
self.hello=hello world release
# config test properties
self.hello=hello world test

该属性将会被在HomeController.java 中读取,并作为返回参数,对打包结果进行测试:

       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
       
       
@RestController
public class HomeController {
private final static Logger LOG = LoggerFactory.getLogger(HomeController.class);
@Value( "${self.hello}")
private String hello;
@GetMapping(value = "/")
public String home() {
LOG.info( "hi, {}.", hello);
return hello;
}
}

pom 中定义Profile 参数

       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
       
       
<profiles>
<profile>
<id>test </id>
<properties>
<activatedProperties>test </activatedProperties>
</properties>
<activation>
<activeByDefault>true </activeByDefault>
</activation>
</profile>
<profile>
<id>prod </id>
<properties>
<activatedProperties>prod </activatedProperties>
</properties>
</profile>
<profile>
<id>release </id>
<properties>
<activatedProperties>release </activatedProperties>
</properties>
</profile>
</profiles>

本实例中我们在pom.xml 定义了testprodrelease 三个Profile, test为默认激活。

覆盖默认Resource 插件配置

通过maven resource 插件实现对不同Profile 下的文件的拷贝:

       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
       
       
<resources>
<!-- when use filter to replace the @@, do not need copy the jdbc.properties to jar, we can remove the below code -->
<resource>
<directory>${basedir}/src/main/resources/${activatedProperties}-conf </directory>
</resource>
<resource>
<directory>${basedir}/src/main/resources </directory>
<filtering>true </filtering>
<includes>
<include>**/application.yml </include>
<include>**/application.yaml </include>
<include>**/application.properties </include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources </directory>
<filtering>true </filtering>
<includes>
<include>**/application-${activatedProperties}.yml </include>
<include>**/application-${activatedProperties}.yaml </include>
<include>**/application-${activatedProperties}.properties </include>
</includes>
</resource>
<resource>
<directory>src/main/resources </directory>
<filtering>true </filtering>
<excludes>
<exclude>**/*.properties </exclude>
</excludes>
</resource>
</resources>

Filter 的使用

使用Filter 替换掉通配符:

       
       
1
2
3
4
       
       
<filters>
<filter>src/main/resources/application-${activatedProperties}.properties </filter>
<filter>${basedir}/src/main/resources/${activatedProperties}-conf/jdbc.properties </filter>
</filters>

采用Maven 激活Spring Profile

在上文中我们为Maven 定义了三种不同的activatedProperties,在application.properties 中如下配置:

       
       
1
2
       
       
# use maven profile properties to config spring boot profile
spring.profiles.active=@activatedProperties@

打包部署

运行Maven 指令打包

       
       
1
       
       
mvn clean package -Dmaven.test.skip= true -P ${activatedProperties}

此处指定Profile 为release

       
       
1
       
       
mvn package -Dmaven.test.skip=true -Prelease

结果查看

  • 打包后的jar 中的application.properties
    打包后的`jar` 中的`application.properties`打包后的`jar` 中的`application.properties`

  • 浏览器查看
    浏览器查看浏览器查看

  • Java 代码查看

             
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
             
             
    @SpringBootApplication
    public class BootMvnProfilesApplication {
    private final static Logger LOG = LoggerFactory.getLogger(BootMvnProfilesApplication.class);
    public static void main(String[] args) {
    ConfigurableApplicationContext applicationContext =
    SpringApplication.run(BootMvnProfilesApplication.class, args);
    String[] profiles = applicationContext.getEnvironment().getActiveProfiles();
    LOG.info( "active profiles are {}.", Arrays.toString(profiles));
    }
    }

源代码

本工程源代码可以从github 获取。源代码

总结

本文详细地介绍了Maven Profile 与Spring Boot Profile 进行集成的步骤。同时,对集成的原理进行了深入的介绍,包括Spring Boot 各个插件的默认配置等信息。相信通过阅读本文,并结合源代码,读者一定可以对其熟练的掌握。希望本文可以对读者在工程构件上有所裨益。

好书推荐

参考

  1. Spring Boot Maven Plugin
  2. Spring Boot Doc Maven Plugin
  3. How to set spring boot active profiles with maven profiles
  4. maven profile
  5. 使用 Maven Profile 和 Filtering

本文章采用知识共享署名 2.5 中国大陆许可协议进行许可。
欢迎转载,但转载请注明来自张兆玉,并保持转载后文章内容的完整。本人保留所有版权相关权利。
本文链接:tramp.cincout.cn/2017/07/04/tool-2017-07-05-integrate-maven-profile-with-spring-boot-profile/

原文链接:http://tramp.cincout.cn/2017/07/04/tool-2017-07-05-integrate-maven-profile-with-spring-boot-profile/

猜你喜欢

转载自blog.csdn.net/qq_32786873/article/details/78870200