Jenkins 为开发环境、生产环境等不同环境构建war

我们在开发过程中,同一套代码,开发环境、生产环境会使用不同的配置文件。如果使用同一个配置文件,在部署到不同环境的时候,需要修改配置文件,然后commit,再用Jenkins构建。
下面我讲解下如何通过定义变量,实现Jenkins构建的时候,动态加载配置文件。
1. 我们在resources/config创建dev和product目录,分别添加config.properties配置文件。
在resources/config/dev/config.properties定义env.target=dev,在resources/config/product/config.properties定义env.target=product
2. 我们在spring.xml配置加载config.properties

<context:property-placeholder location="classpath*:config/${env.target}/config.properties" ignore-resource-not-found="true" />

3.我们在pom.xml添加profiles配置

<profiles>
    <!--开发环境-->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <filters>
                <filter>${project.basedir}/src/main/resources/config/dev/config.properties</filter>
            </filters>
        </build>
    </profile>

    <!--生产环境-->
    <profile>
        <id>product</id>
        <build>
            <filters>
                <filter>${project.basedir}/src/main/resources/config/product/config.properties</filter>
            </filters>
        </build>
    </profile>      
</profiles>

4.我们在开发环境的Jenkins配置Build参数clean install -Pdev -Dmaven.test.skip=true
Build参数
我们在正式环境的Jenkins配置Build参数clean install -Pproduct -Dmaven.test.skip=true
Build参数
这样我们在Jenkins上为不同环境构建时,Jenkins便会把引用变量配置项声明为其对应环境配置项。
eg:我们在一个xml引用db.url配置项,xml代码如下:

<property name="jdbcUrl" value="${db.url}" />

当我们在Jenkins使用clean install -Pdev -Dmaven.test.skip=true构建时,Jenkins构建项目的target目录对应的xml文件,db.url便会成为dev开发环境的数据库地址。

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值