maven 根据环境打包不同配置文件

经过几天对maven的学习,大概了解了打包的不同方式,目前应该用的都是maven 3了,本文教程主要基于maven 3。

主要的方式有以下几种:

利用profile 配置build或assembly

配置文件放置在不同的文件夹中,比如resources_dev, resources_qa, resouces_pro放到不同的文件夹中。

利用build元素进行配置

<profiles>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
    <id>dev</id>
    <build>
        <resources>
          <resource>
            <directory>D:\Users\liubin\workspace\simple-service-webapp\src\main\resources</directory>
          </resource>
        </resources>

    </build>
    </profile>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
    <id>qa</id>
    <build>
        <resources>
          <resource>
            <directory>D:\Users\liubin\workspace\simple-service-webapp\src\main\resources_qa</directory>
          </resource>
        </resources>

    </build>
    </profile>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
    <id>prod</id>
    <build>
        <resources>
          <resource>
            <directory>D:\Users\liubin\workspace\simple-service-webapp\src\main\resources_prod</directory>
          </resource>
        </resources>

    </build>
    </profile>
  </profiles>

利用assembly进行配置,assembly example:
https://maven.apache.org/plugins/maven-assembly-plugin/examples/multimodule/module-source-inclusion-simple.html

使用assembly的不同任务配置

如下所示:

<profiles>
    <profile>
    <id>dev</id>
<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>test</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>   <descriptor>${project.basedir}/src/main/assembly/dev.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>

        </executions>
      </plugin>
      </profile>
      <profile>
      <id>qa</id>
<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>test</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors><descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
      </profile>
      </profiles>

其中${project.basedir}/src/main/assembly/test.xml,请参考https://github.com/khmarbaise/multiple-artifacts/tree/master/war/src/main/assembly

利用assembly的file元素

参考http://stackoverflow.com/questions/7189003/different-configuration-file-for-dev-and-prod-using-maven

两种方式各有利弊,第一种方式通过不同的编译参数生成不同的结果,需要执行多次编译命令。
命令为:mvn -Pqa package
第二种方式则一次性生成所有目标,占用空间。
具体选择哪一种,看各自需求了。

如何处理web.xml

因为web.xml默认不是统一放在resource_qa目录下,对于web.xml的处理,也可以单独对maven-war-plugin进行配置来实现。
参考如下代码配置方式:
https://github.com/adaiguoguo/GerritDashboard/blob/3e7ffb078bc62e5b7c12154205f86ef7f3f71d5e/pom.xml

在这个工程中,在不同的profile中定义了不同的变量,使用变量替换完成web.xml的替换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值