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 ...
随机推荐
- 搭建Maven工程的时候,做单元测试,报ClassNotFoundException
搭建Maven工程的时候报错 问题原因是在spring.xml中配置的 classpath:config.properties 没有在工程中创建.
- Auguse 2nd, Week 32nd Tuesday, 2016
Love me little and love me long.不求情意绵绵,但愿天长地久. Friends are relatives you make for yourself.朋友是你自己结交的 ...
- vs2013 error c4996: 'fopen': This function or varia
做opencv练习时,使用vs2013遇到如下错误: 错误1error C4996: 'fopen': This function or variable may be unsafe. Consid ...
- Eclipse 中Tomcat 启动 与直接启动Tomcat的区别
这段时间不用Java 了突然发现在用的时候出问题了. 首先现在Eclipse和Tomcat,解压后 如图1所示: 图1: 进入里面的bin文件目录后发现有 如图2所示包含了startup.bat,st ...
- javamail实现邮箱验证功能
javamail是基于SMTP协议和POP3协议的邮件发送与接收系统,在用户注册与登陆的过程中,常需要用到邮箱验证功能,下面是基于javamail的一个简单实现 用户注册后通过servlet得到邮箱地 ...
- 态势感知 > 技术运维问题
http://blog.csdn.net/sanmaoljh/article/details/52670226 http://u.sanwen.net/subject/250516.html http ...
- Spark编译安装和运行
一.环境说明 Mac OSX Java 1.7.0_71 Spark 二.编译安装 tar -zxvf spark-.tgz cd spark- ./sbt/sbt assembly ps:如果之前执 ...
- Java Hour 13 集合基础
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为13 Hour,请各位不吝赐教. Java 中的集 ...
- Codeforces Round #329 (Div. 2)
推迟了15分钟开始,B卡住不会,最后弃疗,rating只涨一分... 水(暴力枚举) A - 2Char /******************************************** ...
- hihoCoder 1160 攻城略地
原图可能有多个连通分量,先DFS找出每个连通分量中最小节点,这些必然是要攻占的城市. 设 n 为节点数, m 为边数, cnt 为初始连通分量数,在剩下的边数不小于 m - (n - cnt) 的时候 ...