经过几天对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的替换。