SpringBoot+Maven使用profile打包不同环境的配置文件

前言

在开发的时候一般会有三个环境:开发dev、测试qas、生产prd,三个环境的配置不一样,手动修改容易出错,并且配置多的时候手动修改产生的工作量巨大,所以需要根据不同的环境用不同的配置文件
SpringBoot天然支持多环境配置:
application.yml存放公共的配置
application-dev.yml、application-qas.yml、application.prd.yml分布存放三个环境的配置
application.yml 中配置spring.profiles.active=prd(或者dev、qas)指定使用的配置文件
这种方法不在本次讨论范围内,下面开始进入正题

SpringBoot版本:2.0.4.RELEASE
开发工具:IDEA
JDK:1.8

关键文件目录及配置:

关键文件目录及配置

占位符

<properties>
  <resource.delimiter>#</resource.delimiter>
</properties>

L2 表示被替换的配置占位符需要以#开头和结尾,你也可以用其他的(如果使用默认${} ,实测不会生效,据说:SpringBoot 用的也是${} 会有冲突)

例如:application.yml

#port#将被替换为具体的值

server: 
  port: #port#

profile配置

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <package.environment>dev</package.environment>
    </properties>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
  </profile>
  <profile>
    <id>qas</id>
    <properties>
      <package.environment>qas</package.environment>
    </properties>
  </profile>
  <profile>
    <id>prd</id>
    <properties>
      <package.environment>prd</package.environment>
    </properties>
  </profile>
</profiles>

profiles中三组profile 分别定义了dev开发、qas测试、prd生产 三个环境的标志

L5:在dev中定义<activeByDefault>true</activeByDefault>表示默认激活的环境为dev

替换文件目录

<build>
  <finalName>${project.artifactId}-${project.version}</finalName>
  
  <filters>
    <filter>env/${package.environment}/filter.properties</filter>
  </filters>
  
  <resources>
    
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <excludes>
				<exclude>application-exclude.yml</exclude>
			</excludes>
    </resource>
    
    <!--src/main/java目录及所有子目录下的properties,xml类型文件不替换-->
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.properties</include>
        <include>**/*.xml</include>
      </includes>
      <filtering>false</filtering>
    </resource>
    
  </resources>
  
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

编译时指定profile
编译指定profile

实际效果

实际效果

project.build关键配置:

  • build.filters.filter
  • resources.resource

build.filters.filter:表示存实际值的配置文件 例如env/dev/filter.properties

${package.environment} 对应当前使用的profile的id对应的 profile.properties

resources.resource:一组resource对应一组规则

filtering:true\false 替换或不替换

directory:指定目录

Includes/excludes :配置过滤规则外的文件

resources.resource.excludes.定义了需要排除的配置文件,实际测试效果是:该文件不会编译到target对应的目录中(并非预想的只是不替换值
源文件编译结果

build.filters.filter可以放到profiles下面,原build.filters可以省略,用哪种方式就萝卜白菜了,例如:

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <profiles.active>dev</profiles.active>
    </properties>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <build>
      <filters>
        <filter>env/dev/filter.properties</filter>
      </filters>
    </build>
  </profile>
  <profile>
    <id>qas</id>
    <properties>
      <profiles.active>qas</profiles.active>
    </properties>
    <build>
      <filters>
        <filter>env/qas/filter.properties</filter>
      </filters>
    </build>
  </profile>
</profiles>

Maven根据Profile读取不同配置环境配置文件

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值