spring boot + jenkins 多环境配置

本文详细介绍了如何在Spring Boot项目中通过pom.xml的profiles配置实现不同环境的切换,以及如何在Jenkins中配合使用,以便于自动化部署。重点讲解了激活默认环境的机制和application.properties中动态获取环境变量的方法。

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

一、首先在项目中的配置
在这里插入图片描述
二、pom.xml配置

<profiles>
   <profile>
        <id>alpha</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <profiles.active>alpha</profiles.active>
        </properties>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>rc</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <profiles.active>rc</profiles.active>
        </properties>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>release</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <profiles.active>release</profiles.active>
        </properties>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

三、application.properties中配置

spring.profiles.active = @profiles.active@

注意这里的值 @profiles.active@ ,该值为pom.xml中配置的值
在这里插入图片描述
环境的判断是根据<activeByDefault>true</activeByDefault> 标签获取,如果该标签为 true ,在表示为当前环境,并且在 idea 工具中能够显示当前环境
在这里插入图片描述
四、Jenkins 配置
只需在环境构建的时候指定当前环境

clean install -Dmaven.test.skip=true -Prc

在这里插入图片描述
五、测试
在这里插入图片描述
有关 Jenkins 的搭建可以参考
https://blog.csdn.net/weixin_45267345/article/details/116158220

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值