pom通过profile设置打包运行环境

1.简介

spring boot项目在开发过程中,会涉及到开发、测试、线上的部署,而不同的部署环境需要加载不同的配置文件,此时可以使用profile对打包运行环境进行配置。

2.使用方式

在pom.xml中配置profile属性,先来看一个完整的配置样例:

    <!--分别设置开发,本地,生产环境-->
    <profiles>
        <!-- 本地环境 -->
        <profile>
            <!--定义id与maven打包时候的参数对应-->
            <id>local</id>
            <activation>
                <!--默认激活,true:激活,false:不激活-->
                <activeByDefault>true</activeByDefault>
            </activation>
            <!--配置变量,在property或者yml中使用@xxx@进行引用-->
            <properties>
                <!--配置变量名及变量值,变量名可以任意定义-->
                <environment>local</environment>
            </properties>
        </profile>
        <!-- 用户体验环境 -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <environment>dev</environment>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <environment>prod</environment>
            </properties>
        </profile>
    </profiles>

可以在profiles里面配置多个profile,每个profile有一个唯一的id值,使用activeByDefault配置默认激活的profile。在maven打包的时候,执行的打包命令:mvn clean package -P dev中的-P后的参数就是指定选中哪个profile,此参数对应于profile中的id值。

配置好profile后,在idea工具中,刷新maven,会出现profile的选项,选择哪一个,idea打包的时候激活的就是对应的配置。
在这里插入图片描述
我们可以根据激活的profile,获取到profile里面配置的属性,例如此处就是使用激活的profile获取到spring.profiles.active的值,当激活的profile为local时,spring boot激活的配置文件为application-local.yml。
在这里插入图片描述

3.打包方式

maven中打包,我们可以使用命令:

###激活的profile是dev
mvn clean package -P  dev

在使用jenkins发布系统时,我们可以使用命令:

##-Pdev即指定激活的profile为dev,--pl只打包xxx目录下的aaa,有可能xxx目录下存在多个项目,am:also-make表示同时处理选定模块所依赖的模块
clean package -Dmaven.test.skip=true -Pdev --pl xxx/aaa -am

在maven构建的时候,我们可以使用resources属性配置打包需要的文件以及排除的文件,在pom.xml中配置:

  <build>
        <resources>
            <resource>
                <!-- 指定配置文件所在的resource目录 -->
                <directory>src/main/resources</directory>
                <!--打包包含的文件-->
                <includes>
                    <include>application.yml</include>
                    <include>application-${environment}.yml</include>
                    <include>logback-xxx.xml</include>
                    <include>mchange-xxx.properties</include>
                </includes>
                <!--打包需要排除的文件-->
                <excludes>
                    <exclude>test/*</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值