SpringBoot 项目各个环境配置说明dev、uat、prod

2 篇文章 0 订阅
2 篇文章 0 订阅

String boot 项 配置文件application.yml,但是为了一个项目可以在开发环境,测试环境,生产环境执行,那么就要配置不通的配置文件,如:application-dev.yml、application-uat.yml、application-prod.yml

环境发布时,需要打包项目,无论jar包 还是war包,可以指定打包环境,即指定特定配置文件

1、application.yml 中配置如下,其中 ‘@spring.profiles.active@’ 就是指定打包配置文件的引用。

@@ 是说明引用pom.xml 配置文件中的配置,网上有些没有单引号,我配置时没有单引号时,yml文件报错,记得单引号一定要,否则可能会报错,如下会说明:

spring.profiles.active 为pom.xml 文件中的配置

spring:
  application:
    name: xxx-xxx
  profiles:
    active: '@spring.profiles.active@'

2、pom.xml 配置说明

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
            <activation>

               <!--默认使用dev的配置,一般用于本地启动时-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <spring.profiles.active>uat</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <spring.profiles.active>pro</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

同时为了Springboot打包时可以获取到pom.xml 中的配置,pom.xml 中配置 <resources>标签

<build>
      <finalName>gp-lotso-web</finalName>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <configuration>
                  <includeSystemScope>true</includeSystemScope>
              </configuration>
          </plugin>
      </plugins>
      <resources>
          <resource>
              <directory>src/main/resources</directory>
              <filtering>true</filtering>
          </resource>
      </resources>
      
  </build>

3、打包

mvn clean package -P uat  

uat 就是指定打包使用的配置文件,打包完成后的jar包或war包,

配置文件application.yml 中的 环境标签变为如下,即可启动项目了

spring:
  application:
    name: xxx-xxx
  profiles:
    active: 'uat'

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值