Maven插件-打包时多环境配置文件设置

Maven插件-打包时多环境配置文件设置

同一个项目,测试、生产环境配置内容是不同的,如何通过Maven插件在不同的环境下使用不同的配置文件呢?

项目结构

这里写图片描述

Profile

定义一些列配置信息,然后通过命令激活指定信息,一般在项目pom.xml文件中配置。

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault> <!-- 默认环境 -->
            </activation>
        </profile>
        <profile>
            <id>qa</id>
            <properties>
                <env>qa</env>
            </properties>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <env>prd</env>
            </properties>
        </profile>
    </profiles>

mvn打包命令:

mvn clean package -Pdev/qa/prd

build中resource

        <!-- java代码路径 -->
        <sourceDirectory>src/main/java</sourceDirectory>
        <!-- test代码路径 -->
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <!-- 测试资源路径 -->
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
        <!-- 资源路径可以配置多个 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/wfconfig/${env}</directory>
                <!-- 资源文件不包含web.xml -->
                <excludes>
                    <exclude>*.xml</exclude>
                </excludes>
            </resource>
        </resources>

通过配置resouces,我们就可以通过mvn clean package -Pqa指定不同环境下的配置文件,但是该方法仅仅可以把配置文件加载到webapp/classes文件夹下,无法替换webapp/WEB-INF/web.xml文件。

maven-war-plugin插件

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>

                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <!-- 使用web.xml文件路径 -->
                    <webXml>src/wfconfig/${env}/web.xml</webXml>

                    <!-- 将某些需要的文件拷贝到WEB-INF下 -->
                    <webResources>
                        <resource>
                            <directory>src/wfconfig/${env}</directory>
                            <targetPath>WEB-INF</targetPath>
                            <includes>
                                <include>**/*.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>

配置后在打包时即可按照-P参数从指定配置文件中拉去web.xml文件,maven-war-plugin更多操作参见(https://maven.apache.org/plugins/maven-war-plugin/war-mojo.html)。

完整示例

https://github.com/Wang-Jun-Chao/JavaEE_And_SpringBoot/tree/54322bd8d56f4b92ce2886aea9671f5a1c6cdbbe/002-Maven%E6%8F%92%E4%BB%B6-%E6%89%93%E5%8C%85%E6%97%B6%E5%A4%9A%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AE%BE%E7%BD%AE

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值