maven本质上是一个插件框架,几乎所有的功能都是通过各种各样的插件来实现的。maven默认会依据项目类型自动把构建时的各阶段(Lifecycle和phase)自动绑定(Lifecycle Mapping)到特定插件(plugin)提供的功能点(goals)上。例如java项目编译阶段(compile),实际上是调用了maven-compiler-plugin插件提供的compile功能点(goal)来实现的。

一、调用插件提供的功能(goal)

方式一:通过生命周期映射的方式,将插件的goal绑定到生命周期中的phase上,然后调用phase。例如:maven-jar-plugin插件提供了一个叫jar的goal,默认会绑定到生命周期的package阶段(phase)。调用mvn package就会自动调用maven-jar-plugin:jar生命周期中所有前置的phase会先自动执行。

package <==> maven-jar-plugin:jar

方式二:直接调用插件的某个功能(goal)。如mvn maven-jar-plugin:jar。maven有一个约定,如果插件的名字叫maven-xxxx-plugin或xxxx-maven-plugin的话。可以直接用mvn xxxx:goal的方式调用其提供的功能。所以前面这个命令就可以简写成:mvn jar:jar这种方式只会执行指定的goal

调用goal完整的命令格式为:

  mvn <plugin-prefix>:<goal>
  mvn [<plugin-group-id>:]<plugin-artifact-id>[:<plugin-version>]:<goal>

二、常用插件和常用配置

maven-resources-plugin 文件资源配置

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding> <!--配置资源文件编码-->
</configuration>
</plugin>

关于资源的配置,还可以参考:http://www.cnblogs.com/pixy/p/4798089.html

maven-compiler-plugin 编译配置

默认绑定到comile phase。当前版本的maven默认使用jdk1.5,使用更新的java版本必须手动配置。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
<compilerArgument>-Xlint:none</compilerArgument>
<compilerArguments>
<extdirs>libs</extdirs> <!--使用项目中的jar包-->
</compilerArguments>
</configuration>
</plugin>

maven-surefire-plugin 单元测试

默认绑定到test阶段。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore> <!--测试有失败用例时,是否继续构建-->
     <skipTests>true</skipTests> <!--是否跳过测试阶段,方式1-->
    <skip>true</skip> <!--是否跳过测试阶段,方式2-->
</configuration>
</plugin>

运行时指定:

mvn package -DskipTests
mvn package -Dmaven.test.skip=true
mvn package -Dmaven.test.failure.ignore=true

更多的配置可以参考另一篇文件:http://www.cnblogs.com/pixy/p/4718176.html

maven-jar-plugin  打jar包

这个是普通java项目(非java web项目和其他特殊类型的java项目)package阶段默认绑定的插件,能够将编译好的class和资源打成jar包。

  • 常用配置1:打出可以运行的有主类的jar包
 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
    <excludes> <!--打包时要排除的文件-->
    <exclude>agent.properties</exclude>
    </excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- <classpathPrefix>lib/</classpathPrefix> -->
<mainClass>com.demo.HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

maven-assembly-plugin  打包含依赖的全包

      <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
      <appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
      <archive>
<manifest>
         <mainClass>com.defonds.RsaEncryptor</mainClass>
         </manifest>
      </archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>

运行mvn assemlby:assembly在target下生成xxxx-with-dependencies.jar

assembly 插件的一个 bug:http://jira.codehaus.org/browse/MASSEMBLY-360,它在对第三方打包时,对于 META-INF 下的 spring.handlers,spring.schemas 等多个同名文件进行了覆盖,遗漏掉了一些版本的 xsd 本地映射。

maven-shade-plugin 打包含依赖的全包且可以配置主类

Über在德语中是"above,over"的意思。Uber-Jar就是包含所有依赖的全包、完整包。

这个插件只有shade:shade一个唯一的goal,绑定到package pahse。

<plugin>              
  <groupId>org.apache.maven.plugins</groupId>  
<artifactId>maven-shade-plugin</artifactId>   
<configuration>  
   <transformers>  
     <transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
         <mainClass>com.xun.pf.sayHello.HelloWorld</mainClass>  
     </transformer>  
   </transformers>  
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
      <resource>META-INF/spring.handlers</resource>  
    </transformer> 
</configuration>  
<executions>  
   <execution>  
      <phase>package</phase>  
      <goals>  
         <goal>shade</goal>  
      </goals>  
    </execution>  
 </executions>  
</plugin>

这是由于一些包重复引用,打包后的 META-INF 目录多出了一些 *.SF 等文件所致。 解决方法是在configuration节点下加:

<filters>

  <filter>

    <artifact>*:*</artifact>

    <excludes>

      <exclude>META-INF/*.SF</exclude>

      <exclude>META-INF/*.DSA</exclude>

      <exclude>META-INF/*.RSA</exclude>

    </excludes>

  </filter>

</filters>

ResourceTransformer

如修改配置文件,包含/排除特定文件等

http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#DontIncludeResourceTransformer

maven-shade-plugin插件有个配置属性:createDependencyReducedPom,默认值为true.

注意这个属性,如果你用这个插件来deploy,或者发布到中央仓库

这个属性会缩减你的pom文件,会把你依赖的<dependency>干掉

正确的做法是把这个值改成false

  1. <configuration>
  2. <createDependencyReducedPom>false</createDependencyReducedPom>
  3. </configuration>

maven-war-plugin 打war包

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
   <configuration>
<warName>${project.artifactId}</warName>
<webResources>
  <resource> <!--将额外的jar依赖打入war包-->
<directory>libs/</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
        <packagingExcludes>css/**,html/**</packagingExcludes>
 
</configuration>
</plugin>

maven-source-plugin 打包源码

生成的源码包名为xxxx-sources.jar。

  <plugin>        
     <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

maven-exec-plugin  执行程序

直接调用java goal,执行java程序。

mvn exec:java -Dexec.mainClass="com.demo.HelloWorld"

在pom文件中配置

            <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.demo.config.DocMapper</mainClass>
<arguments>
<argument>${project.build.outputDirectory}\doc-path-map.txt</argument>
<argument>${basedir}\src</argument>
<argument>**/resource/*.java</argument>
</arguments>
</configuration>
</plugin>

maven-dependency-plugin 依赖分析

mvn dependency:copy-dependencies -DoutputDirectory=lib          #导出所有依赖库
mvn dependency:list
mvn dependency:tree
mvn dependency:analyze
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive> <!--是否排除间接依赖的包-->
<stripVersion>true</stripVersion> <!--复制的jar文件是否去掉版本信息-->
</configuration>
  <executions>
    <execution>
   <id>copy-dependencies</id>
  <phase>package</phase>
  <goals>
   <goal>copy-dependencies</goal>
  </goals>
  </execution>
  </executions>
</plugin>

maven-install-plugin 产出安装到maven本地库

默认绑定到install阶段。将生成的构建产出安装到本地库。

goals:

    • install-file    将本地jar包安装到本地仓库
mvn install:install-file -Dfile=classes12_g.jar -DgroupId=com.oracle -DartifactId=oracle -Dversion=10.2.0.2. -Dpackaging=jar -DgeneratePom=true

maven-release-plugin 整合svn创建tag、更新版本

暂无

参考文档:http://juvenshun.iteye.com/blog/376422

cobertura  测试覆盖率计算

基于jcoverage,原理是对class文件插桩,然后执行测试并生成覆盖率报告。参考资料

Pom.xml文件需要加入配置:

<reporting>
   <outputDirectory>target/site</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>

运行 mvn cobertura:cobertura 将会插桩class文件、测试、生成覆盖率报告。

cobertura支持的goal:

  • check     Check the Last Instrumentation Results.
  • clean   Clean up rogue files that cobertura maven plugin is tracking.
  • dump-datafile     Cobertura Datafile Dump Mojo.
  • instrument         Instrument the compiled classes.
  • cobertura           Instruments, Tests, and Generates a Cobertura Report.

findbugs  静态java代码检查

基于规则匹配静态检查java代码中存在的问题。参考资料

pom.xml中配置:

<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.3.1</version>
</plugin>
</plugins>
</reporting>

运行 mvn findbugs:findbugs 将开始执行检查,并生成bugs报告(默认在target\site\findbugs目录)。 findbugs:findbugs绑定到compile pahse即在编译时自动检查。

findbugs插件支持的goal:

  • check        fail the build if there were any FindBugs violations in the source code. An XML report is put out by defualt in the target directory with the errors
  • findbugs   Generates a FindBugs Report when the site plugin is run. The HTML report is generated for site commands only.
  • gui            Launch the Findbugs GUI. It will use all the parameters in the POM fle.
  • help          Display help information on findbugs-maven-plugin.   mvn findbugs:help -Ddetail=true -Dgoal=<goal-name>

maven-eclipse-plugin 生成Eclispe工程文件

生成.classpath和.project文件,并且配置Eclispe将Maven作为External工具。GoalsProperties

运行:mvn eclipse:eclipse 生成.classpath和.project文件,

maven-idea-plugin 生成IntelliJ IDEA工程文件

IDEA工程文件扩展名为.ipr和.iws

            <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-idea-plugin</artifactId>
<version>2.2</version>
<configuration>
<jdkName>1.6</jdkName>
<jdkLevel>6.0</jdkLevel>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<dependenciesAsLibraries>true</dependenciesAsLibraries>
<useFullNames>false</useFullNames>
<deploymentDescriptorFile>src/main/webapp/WEB-INF/web.xml</deploymentDescriptorFile>
</configuration>
</plugin>

maven-jetty-plugin  jetty服务器

        <plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.6</version>
<configuration>
<contextPath>/</contextPath>
<scanIntervalSeconds>3</scanIntervalSeconds>
<scanTargetPatterns>
<scanTargetPattern>
<directory>src/main/webapp/WEB-INF</directory>
<excludes>
<exclude>**/*.jsp</exclude>
</excludes>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</scanTargetPattern>
</scanTargetPatterns>
<requestLog implementation="org.mortbay.jetty.NCSARequestLog">
<filename>target/yyyy_mm_dd.request.log</filename>
<retainDays>90</retainDays>
<append>true</append>
<extended>false</extended>
<logTimeZone>GMT</logTimeZone>
</requestLog>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>80</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>

maven-javadoc-plugin 生成java文档

        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>javadoc</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<verbose>false</verbose>
<show>public</show>
<subpackages>com.pwrd.mobileqa.assist.resource</subpackages>
<doclet>com.sun.jersey.wadl.resourcedoc.ResourceDoclet</doclet>
<docletPath>${path.separator}${project.build.outputDirectory}</docletPath>
<docletArtifacts> <!--解析项目生成javadoc需要的依赖-->
<docletArtifact>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.6.1</version>
</docletArtifact>
             ......
</docletArtifacts>
<!-- the following option is required as a work around for
version 2.5 of the javadoc plugin which will be used
by a maven version > 2.0.9-->
<useStandardDocletOptions>false</useStandardDocletOptions>
<additionalparam>-output ${project.build.outputDirectory}/resourcedoc.xml</additionalparam>
</configuration>
</plugin>

mybatis-generator-maven-plugin  生成mybatis映射

默认会在根目录查找并使用名为mybatis-config.xml的配置文件。

            <plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>

maven常用插件总结的更多相关文章

  1. maven常用插件pom配置

    一.问题描述: 部署一个maven打包项目时,jar包,依赖lib包全部手动上传至服务器,然后用maven部署报错:Exception in thread "main" java. ...

  2. Maven常用插件

    maven利用各种插件来管理构建项目,本文记录下工作中常用到的插件及使用方法.每个插件都会提供多个目标(goal),用于标示任务.各插件配置在pom.xml里,如下: <build> [. ...

  3. maven常用插件配置详解

    常用插件配置详解Java代码    <!-- 全局属性配置 --> <properties> <project.build.name>tools</proje ...

  4. Maven常用插件简单配置

    好久不见,甚是想念.一日不见,如隔三秋. 从春节到现在已经很久没有回归博客园了,今天回来温习一下maven常用的一些插件的配置,学东西一个很简单的诀窍就是重复重复再重复,这样一定能把知识掌握的很牢靠. ...

  5. [maven] 常用插件解析

    参考资料:http://my.oschina.net/zh119893/blog/276090 我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完 ...

  6. 【转】maven常用插件介绍

    我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...

  7. Maven学习总结(22)——Maven常用插件介绍

    我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...

  8. maven常用插件: 打包源码 / 跳过测试 / 单独打包依赖项

    一.指定编译文件的编码 maven-compile-plugin <plugin> <groupId>org.apache.maven.plugins</groupId& ...

  9. maven常用插件配置

    1.maven-jar-plugin插件 <!-- 排除资源文件中的properties文件,不需要打到jar中,后面通过assembly插件打包到conf目录中 --><plugi ...

随机推荐

  1. python scrapy cannot import name xmlrpc_client的解决方案,解决办法

    安装scrapy的时候遇到如下错误的解决办法: "python scrapy cannot import name xmlrpc_client" 先执行 sudo pip unin ...

  2. 字符串匹配与KMP算法实现

    >>字符串匹配问题 字符串匹配问题即在匹配串中寻找模式串是否出现, 首先想到的是使用暴力破解,也就是Brute Force(BF或蛮力搜索) 算法,将匹配串和模式串左对齐,然后从左向右一个 ...

  3. codeigniter 对数据库的常用操作

    codeigniter (CI)是一个优秀.敏捷的PHP开源框架,尤其封装了对数据库的操作,很方便,以下是php ci常用的数据库操作,作个记录: /* ======================= ...

  4. hdu 1215 筛法

    求小于n的n的因子之和 Sample Input 3 2 10 20Sample Output 1 8 22 #include<cstdio> #include<iostream&g ...

  5. 电赛菜鸟营培训(一)——STM32F103CB之LED控制

    一.STM32F103C8 引脚分布 二.LED的共阴.共阳接法 这里应该是七段数码管的接法. 限流电阻选择为470,在Multism中仿真,也需要接入,否则会出现闪烁情况.或者直接更改属性. 三.消 ...

  6. JNI,NDK

    jni的调用过程 1)安装和下载Cygwin,下载Android NDK 2)在ndk项目中JNI接口的设计 3)使用C/C++实现本地方法 4)JNI生成动态链接库.so文件 5)将动态链接库复制到 ...

  7. intersection

    用来找到两个rdd的交集,注意,最终的new rdd的分区数量取决于两个rdd中的最大分区数量. 测试一下: val data1 = sc.parallelize(1 to 20,1) val dat ...

  8. 食物链 poj 1182

    C - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  9. jquery概要--基础02

    复制节点:clone();默认不会复制绑定事件,如果传入参数true会复制:替换节点: replaceWith()              //原节点放在前,新节点放在在后: replaceAll( ...

  10. js:数据结构笔记2---列表

    列表: 定义:一组有序的数据: function List() { this.listSize = 0; this.pos = 0; this.dataStore = []; this.find = ...