maven-maven使用-P参数打包不同环境

maven-maven使用-P参数打包不同环境

一般的,开发环境有dev, test 和 pro,他们的配置多有不同,那么就可以使用 maven -P这个参数进行多环境打包

clean install -Dmaven.test.skip=true -P pro,就可以切换成生产环境,和 jenkins 配合简直不要太爽!!

举个例子

以 boot 项目来说,现有目录结构:

/src
    /main
        /java
        /resources
            /static
            /templates
            application.yml
            application-dev.yml
            application-pro.yml
            application-test.yml

application.yml

server:
  port: 8080
spring:
  profiles:
     # @spring.profiles.active@ 变量将会随着参数的传入被替换
    active: @spring.profiles.active@

然后三个不同环境的配置文件分别为:

application-dev.yml:

server:
  port: 8080
spring:
  application:
    name: mpp-dev

application-pro.yml:

server:
  port: 8081
spring:
  application:
    name: mpp-pro

application-test.yml:

server:
  port: 8082
spring:
  application:
    name: mpp-test

配置 maven 的 pom 文件,默认激活 dev 环境:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <spring.profiles.active>test</spring.profiles.active>
        </properties>
    </profile>
    <profile>
        <id>pro</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            //spring.profiles.active即在application.yml文件中
            //定义的参数@spring.profiles.active@
            <spring.profiles.active>pro</spring.profiles.active>
        </properties>
    </profile>
</profiles>

根据环境过滤只有当前环境的配置文件:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <!-- 替换占位符-->
        <filtering>true</filtering>
        <excludes>
            <exclude>application-*.yml</exclude>
        </excludes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <!-- 替换占位符-->
        <filtering>true</filtering>
        <includes>
            <include>application-${spring.profiles.active}.yml</include>
        </includes>
    </resource>
</resources>

需要加入 plugin 为:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
</plugin>

测试

在当前项目 pom 文件所在的目录下打开命令行,输入 clean package -Dmaven.test.skip=true -P pro

可以看到:

/target
    /classes
        /com
        application.yml
        application-pro.yml

打开 application.yml 文件:

server:
  port: 8080
spring:
  profiles:
    active: pro

可以发现之前 @spring.profiles.active@ 变量已经被替换成了 pro ,而且配置文件只关于生产环境,而且dev 和 test 的配置文件都已经被过滤了

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值