springboot多环境配置文件使用 结合idea打包 使用超方便

本文介绍了如何在SpringBoot项目中设置多环境配置文件,包括dev、test和prod环境,并通过Maven进行构建。详细步骤涵盖了修改application.yml及各个环境的yml文件,更新pom.xml配置,以及在IDEA中指定不同环境启动和打包时使用的配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于maven搭建

properties yml格式的配置文件均适用

1:修改总的配置文件 和 开发环境、测试环境、生产环境的配置文件   dev test prod

修改application.yml   注: @activatedProperties@与pom文件中标签对应

#生效配置文件
spring:
  profiles:
    active: @activatedProperties@

修改application-dev.yml

# 指定端口和后端服务URL
server:
  port: 9010
  servlet:
    context-path: /group_purchasing_server

# 配置连接的数据
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/dev?tinyInt1isBit=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

 

修改application-test.yml(同application-dev.yml配置)

修改application-prod.yml(同application-dev.yml配置)

2:修改pom.xml文件配置

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!-- 环境标识,需要与配置文件的名称相对应 -->
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
        </profile>

        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
            </properties>
        </profile>
    </profiles>

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>
                        -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
                    </jvmArguments>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

3:在idea指定开发环境启动和测试环境、生产环境打包时生效的配置文件

      指定开发环境生效(默认为开发环境),正常启动即可。

使用idea打包时,指定打包时生效的配置文件,指定apply后,利用工具打包即可。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值