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
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过使用 Maven 的 profiles 和构建参数来实现多个 pom 文件分别打包测试环境和生产环境。 首先,你可以创建两个独立的 pom 文件,分别用于测试环境和生产环境打包。在这两个 pom 文件中,你可以定义不同的构建配置,如依赖、插件和资源文件等。 接下来,你可以为每个 pom 文件创建一个对应的 profile。在每个 profile 中,你可以指定特定的构建参数,如环境变量、属性值等。这将帮助 Maven 根据不同的 profile 来选择正确的 pom 文件进行构建。 下面是一个示例: 首先,在根目录下创建两个 pom 文件,分别为 pom-test.xml 和 pom-prod.xml。这两个文件可以拥有不同的依赖和插件配置。 然后,在根目录的 pom.xml 文件中添加以下内容: ```xml <profiles> <profile> <id>test</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <!-- 配置 test profile 的构建插件 --> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <!-- 配置 prod profile 的构建插件 --> </plugins> </build> </profile> </profiles> ``` 在以上示例中,test profile设置为默认激活的 profile,这意味着如果没有指定 profile,则会使用 test profile 进行构建。你可以根据需要修改这个设置。 最后,你可以使用以下命令来选择不同的 profile 进行构建: ```shell mvn package -P test # 使用 test profile 进行构建 mvn package -P prod # 使用 prod profile 进行构建 ``` 使用上述命令的时候,Maven 将会根据指定的 profile 来选择对应的 pom 文件进行构建,并且应用相应的配置。 希望这个例子能对你有所帮助!如果你还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值