SpringBoot 多环境打包配置

2023-11-20 14:28:32 浏览数 (1)

实际开发中,应用程序通常需要部署到不同的运行环境中,例如 开发环境测试环境生产环境等。

不同的环境可能需要不同的环境配置,针对这种情况,不可能手动变更配置文件来适应不同的开发环境,通常需要对项目进行多环境配置。

多环境配置

application.yml 文件中添加如下配置:

代码语言:javascript复制
spring:
  profiles:
    active: @package.environment@  # 指定环境

创建多环境配置文件

文件命名格式:application-xxx.yml

resources 目录下创建 开发环境测试环境生产环境 配置文件:

application-dev.yml 开发环境

代码语言:javascript复制
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/demo_dev?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true
    username: root
    password: root  

application-test.yml 测试环境

代码语言:javascript复制
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/demo_test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true
    username: root
    password: root  

application-prod.yml 生产环境

代码语言:javascript复制
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/demo_prod?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true
    username: root
    password: root  

pom.xml 配置

添加 profile

代码语言:javascript复制
<profiles>
    <!-- 开发环境 -->
    <profile>
        <id>dev</id>
        <properties>
            <package.environment>dev</package.environment>
        </properties>
        <activation>
            <!-- 默认激活这个配置 -->
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <!--测试环境-->
    <profile>
        <id>test</id>
        <properties>
            <package.environment>test</package.environment>
        </properties>
    </profile>

    <!--生产环境-->
    <profile>
        <id>prod</id>
        <properties>
            <package.environment>prod</package.environment>
        </properties>
    </profile>
</profiles>

默认激活开发环境配置

添加 resource

代码语言:javascript复制
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>application.yml</include>
                <include>application-${package.environment}.yml</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>

    ...
</build>

启动项目

IDEA 右边 Maven 菜单的 Profiles 中看到默认选中的是 dev 环境。

同样启动项目日志打印的当前环境:The following 1 profile is active: "dev"

代码语言:javascript复制
Connected to the target VM, address: '127.0.0.1:51986', transport: 'socket'

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.7.17)

2023-11-17 10:03:46.455  INFO 10788 --- [           main] c.ufan.shopping.UfanShoppingApplication  : Starting UfanShoppingApplication using Java 1.8.0_251 on DESKTOP-BBRENPM with PID 10788 (E:workspaceufan-cloudufan-modulesufan-shoppingtargetclasses started by Administrator in E:workspaceufan-cloud)
2023-11-17 10:03:46.456  INFO 10788 --- [           main] c.ufan.shopping.UfanShoppingApplication  : The following 1 profile is active: "dev"

打包

IDEA 工具打包

IDEA 右边 Maven 菜单的 Profiles 勾选要打包的环境,双击 Lifecycle -> package 进行打包。

命令打包

IDEATerminal 命令窗口输入命令:

代码语言:javascript复制
# 开发环境
mvn clean package -P dev

# 测试环境
mvn clean package -P test

# 生产环境
mvn clean package -P prod

0 人点赞