maven常用插件总结
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
- <configuration>
- <createDependencyReducedPom>false</createDependencyReducedPom>
- </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工具。Goals,Properties。
运行: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常用插件总结的更多相关文章
- maven常用插件pom配置
一.问题描述: 部署一个maven打包项目时,jar包,依赖lib包全部手动上传至服务器,然后用maven部署报错:Exception in thread "main" java. ...
- Maven常用插件
maven利用各种插件来管理构建项目,本文记录下工作中常用到的插件及使用方法.每个插件都会提供多个目标(goal),用于标示任务.各插件配置在pom.xml里,如下: <build> [. ...
- maven常用插件配置详解
常用插件配置详解Java代码 <!-- 全局属性配置 --> <properties> <project.build.name>tools</proje ...
- Maven常用插件简单配置
好久不见,甚是想念.一日不见,如隔三秋. 从春节到现在已经很久没有回归博客园了,今天回来温习一下maven常用的一些插件的配置,学东西一个很简单的诀窍就是重复重复再重复,这样一定能把知识掌握的很牢靠. ...
- [maven] 常用插件解析
参考资料:http://my.oschina.net/zh119893/blog/276090 我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完 ...
- 【转】maven常用插件介绍
我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...
- Maven学习总结(22)——Maven常用插件介绍
我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...
- maven常用插件: 打包源码 / 跳过测试 / 单独打包依赖项
一.指定编译文件的编码 maven-compile-plugin <plugin> <groupId>org.apache.maven.plugins</groupId& ...
- maven常用插件配置
1.maven-jar-plugin插件 <!-- 排除资源文件中的properties文件,不需要打到jar中,后面通过assembly插件打包到conf目录中 --><plugi ...
随机推荐
- Linux设置IP
进入 vi /etc/sysconfig/network-scripts/ifcfg-eth0 root # ifconfig eth0 192.168.22.232 root # route ad ...
- Kafka学习笔记(一):概念介绍
Kafka是一个开源的,分布式的,高吞吐量的消息系统.随着Kafka的版本迭代,日趋成熟.大家对它的使用也逐步从日志系统衍生到其他关键业务领域.特别是其超高吞吐量的特性,在互联网领域,使用越来越广泛, ...
- Redis、Memcache和MongoDB的区别
>>Memcached Memcached的优点:Memcached可以利用多核优势,单实例吞吐量极高,可以达到几十万QPS(取决于key.value的字节大小以及服务器硬件性能,日常环境 ...
- [原]AppPoolService-IIS应用程序池辅助类(C#控制应用程序池操作)
using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...
- JAVA 获取web文件的相对路径
转自:http://wwwdd2315.blog.163.com/blog/static/66661889201091953350298/ 在JAVA文件中获取该项目的相对路径1.基本概念的理解 绝对 ...
- ☆☆在Eclipse中编译NDK的so文件(普通安卓项目转换为NDK项目的设定)
1 将Native的编译链接配置加入项目中 2 进行编译 3 项目支持Native后,在首尾分别新增了两个编译过程
- 图解VS2010打包全过程(转)
最近刚刚打包发布了用VS2010开发的一个收费系统,借此讲一讲打包过程,供大家参考. 首先打开已经完成的工程,如图: 下面开始制作安装程序包. 第一步:[文件]——[新建]——[项目]——安装项目. ...
- Harris角点
1. 不同类型的角点 在现实世界中,角点对应于物体的拐角,道路的十字路口.丁字路口等.从图像分析的角度来定义角点可以有以下两种定义: 角点可以是两个边缘的角点: 角点是邻域内具有两个主方向的特征点: ...
- LoadRunner之自定义HTTP请求
LoadRunner之自定义HTTP请求 性能测试开发脚本时使用的都是同样的模式.对在性能测试规划时指定的典型业务逻辑场景进行录制,形成基本的脚本骨架. 录制脚本后需要对脚本进行编辑,以满足性能测试需 ...
- Code 128 规则解析
1.CODE 128 标准 1.1 code 128码格式: 格式: 从左起: 空白区域,起始字符(Start),数据区域(data),校验码(check),结束字符(Stop),空白区域. ...