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

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

引入公司SSO时,需要在web.xml文件中配置不同跳转URL,测试、生产环境配置内容是不同的,如何通过Maven插件在不同的环境下使用不同的配置文件呢?

项目结构

project:
  wfconfig
    dev
      web.xml
    qa
      web.xml
    prd
      web.xml
  src
    main
      java
      resources
      webapp
        WEB-INF

Profile

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

        <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>
# mvn打包命令:
mvn clean package -Pdev/qa/prd

仅仅介绍常用操作。

build中resource

        <!-- java代码路径 -->
        <sourceDirectory>src/main/java</sourceDirectory>
        <!-- test代码路径 -->
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <!-- 资源路径可以配置多个 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>wfconfig/${env}</directory>
            </resource>
        </resources>
        <testResources>
            ......
        </testResources>

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

maven-war-plugin插件

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <!-- 使用web.xml文件路径 -->
                    <webXml>wfconfig/${env}/web.xml</webXml>
                </configuration>
            </plugin>

配置后在打包时即可按照-P参数从指定配置文件中拉去web.xml文件,maven-war-plugin更多操作参见

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容