Maven管理SpringBoot Profile详解

1. Spring Profile

Spring可使用Profile绝对程序在不同环境下执行情况,包含配置、加载Bean、依赖等。

Spring的Profile一般项目包含:dev(开发), test(单元测试), qa(集成测试), prod(生产环境)。由spring.profiles.active属性绝定启用的profile。

SpringBoot的配置文件默认为 application.properties(或yaml,此外仅心properties配置为说明)。不同Profile下的配置文件由application-{profile}.properties管理,同时独立的 Profile配置文件会覆盖默认文件下的属性。

2. Maven Profile

Maven同样也有Profile设置,可在构建过程中针对不同的Profile环境执行不同的操作,包含配置、依赖、行为等。

Maven的Profile由 pom.xml 的<Profiles>标签管理。每个Profile中可设置:id(唯一标识), properties(配置属性), activation(自动触发的逻辑条件), dependencies(依赖)等。

此文章不对Spring和Maven的Profile作过多说明,详细情况请自行查阅。

3. Maven 管理 Spring Profile

由于构建是基于Maven(或Gradle,此处仅以Maven说明)。所以使用Maven管理Spring构建时的Profile是非常方便的。

Maven管理Spring Profile分五步,以下详细介绍。

3.1 去掉默认的 Tomcat依赖

在SpringBoot MVC项目中,默认以内嵌Tomcat运行,如果需要特殊的设置或者使用Undertow,需要去掉默认的Tomcat依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>

如果你同时使用了 MyBatis,需要去掉tomcat-jdbc依赖:

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-jdbc</artifactId>
    </exclusion>
  </exclusions>
</dependency>

3.2 Maven Profile设置

在项目(如果有模块为具体模块)的pom.xml下设置:

<!-- Maven控制Spring Profile -->
<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <profileActive>dev</profileActive>
    </properties>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
      </dependency>
      <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jdbc</artifactId>
      </dependency>
    </dependencies>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <profileActive>prod</profileActive>
    </properties>
  </profile>
</profiles>

从上面的配置可以看出,Maven的Profile配置了两个:dev和prod,并且在dev中使用了内嵌Tomcat,而 prod 中没有,所以这两个Profile打包的文件dev可以直接运行(Plugin使用了SpringBoot Plugin),而prod并不能直接运行(或部署在外部Tomcat下,并不推荐这样,后面会说明)。

properties中的profileActive是我们申明的属性,此处对应Spring的Profile值。

3.3 Maven资源过滤

SpringBoot的 Profile选择需要在 application.properties中配置,如果定死在文件,那么每次打包都需要手动修改,很麻烦,而且容易出错。

Maven的资源过滤功能可以实现在构建时修改以“@xxx@”表示的属性。资源过滤需要在pom.xml的<build>标签下配置 resources:

<!-- profile对资源的操作 -->
<resources>
  <resource>
    <directory>src/main/resources</directory>
    <excludes>
      <exclude>application*.properties</exclude>
    </excludes>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <!-- 是否替换@xx@表示的maven properties属性值 -->
    <filtering>true</filtering>
    <includes>
      <include>application.properties</include>
      <include>application-${profileActive}.properties</include>
    </includes>
  </resource>
</resources>

上面的第一个resource去掉了src/main/resources下的所有application.properties文件,“”是通配符,表示此处有任何内容(没有也可以)都匹配。

第二个resource添加了application.properties默认配置文件和由profileActive属性决定的相应profile配置文件。并且filtering为true表示,会将文件内容的“@xx@”替换为相应的变量(如文件中的@profileActive@会替换为profileActive属性值)。

3.4 Spring配置Profile

在application.properties默认配置文件中配置:

spring.profiles.active = @profileActive@

@profileActive@表示该属性值会在maven构建时被替换掉。

3.5 构建

构建命令:

mvn clean package -Pdev

上面的命令会根据Maven Profile的 dev构建环境包,如果需要prod包,则把-P的参数替换成prod即可。

为了方便我会在每个项目下生成一个build.sh文件,内容如下:

#!/bin/bash
profileActive=prod
if [ -n "$1" ]; then
  profileActive=$1
fi
mvn clean package -Dmaven.test.skip=true -P$profileActive

该脚本接收一个参数,即打包对应的Profile。默认情况下如果不带参数,会打包prod环境包。

需要注意的是,该命令跳过了测试。

4. 总结

完成了上面的五步,即可使项目根据你的构建参数的不同,打包出不同环境下运行的包。

第1步去掉了SpringBoot内嵌的tomcat和tomcat-jdbc。使得我们可以决定在什么情况下使用何种容器运行我们的项目。
第2步配置了Maven构建Porfile,使得构建可根据我们的指令分发不同的包。
第3步配置了Maven资源过滤,不仅使得不同Profile下的资源文件互不可见,且替换了资源文件中以“@xx@”表示的属性值。
第4步使Spring的Profile由Maven决策,这样,我们就不用每次打包都修改Spring的Profile配置了。
第5步展示了如何执行不同Profile下的构建命令,并且使用了一个Shell脚本方便我们执行构建和跳过测试(多数时候我们在构建项目时先测试,并不需要在构建时测试,测试和构建的解耦使得我们更专注。但同时,如果你忘记了前置测试,也可能会引发未察觉的测试问题)。

以上就是本文关于Maven管理SpringBoot Profile详解的全部内容,希望对大家有所帮助。欢迎参阅:maven学习-初窥门径使用maven运行Java Main的三种方法解析等,有什么问题可以留言,欢迎大家交流讨论。

若文章对您有帮助,帮忙点个赞!

0
-3
发布时间 2017-10-31 15:04:43
0 条回复(回复会通过微信通知作者)
点击加载更多评论
登录 后再进行评论
(微信扫码即可登录,无需注册)