Maven常用插件
maven利用各种插件来管理构建项目,本文记录下工作中常用到的插件及使用方法。每个插件都会提供多个目标(goal),用于标示任务。各插件配置在pom.xml里,如下:
<build>
[...]
<plugins>
<plugin>
...
</plugin>
</plugins>
[...]
</build>
常用插件见:http://maven.apache.org/plugins/index.html,http://www.mojohaus.org/plugins.html。
maven-compiler-plugin
用于编译源代码,默认在compile阶段被调用。两个goal,compiler:compile/compiler:testCompile
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<!--源码的Java版本-->
<source>1.7</source>
<!--运行环境的Java版本-->
<target>1.7</target>
</configuration>
</plugin>
maven-surefire-plugin
test阶段执行单元测试,路径src/test/java
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<!--不执行的单元测试文件-->
<excludes>
<exclude>bvt/*.java</exclude>
</excludes>
<!--并行运行-->
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
appassembler-maven-plugin
生成启动Java应用的脚本,命令 mvn package appassembler:assemble
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<!-- Set the target configuration directory to be used in the bin scripts,默认路径是etc -->
<configurationDirectory>configuration</configurationDirectory>
<!-- 源配置文件目录,默认目录是src/main/config -->
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<!--Copy the contents from configurationSourceDirectory to the target configuration directory-->
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<!-- Include the target configurationDirectory in the beginning of the classpath declaration in the bin scripts -->
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<!--assemble the artifacts in and place the bin scripts-->
<assembleDirectory>release</assembleDirectory>
<!--jar存放目录-->
<repositoryName>lib</repositoryName>
<!--lib下直接存放jar,没有路径文件夹(如com/apache)-->
<repositoryLayout>flat</repositoryLayout>
<programs>
<program>
<!--指定主类,脚本名。会生成shell/bat两种类型,也可用platforms指定运行平台-->
<mainClass>com.mycompany.app.App</mainClass>
<name>startapp</name>
</program>
</programs>
</configuration>
</plugin>
maven-assembly-plugin
对项目的源码、jar包、依赖、文档、目录等文件归档生成分发包,支持jar/war/zip/tar/tar.gz等归档格式。如写mapreduce job时可以使用生成一个jar包,包含所有的依赖,便于上传job。
命令mvn assembly:single,也可以绑定到package阶段,自动执行。下面的配置会生成如下文件:target/sample-1.0-SNAPSHOT-jar-with-dependencies.jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<!--使用插件提供的配置,包含jar和依赖-->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!--也可以自定义打包内容,使用descriptor指定路径-->
<!--descriptor>src/main/assembly/assembly.xml</descriptor-->
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!--目标single绑定到package阶段,package阶段自动被调用-->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>