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: mvn [ id>:] id>[: ]:
二、常用插件和常用配置
maven-resources-plugin 文件资源配置
<plugin> <artifactId>maven-resources-pluginartifactId> <configuration> <encoding>UTF-8encoding> configuration> plugin>
关于资源的配置,还可以参考:http://www.cnblogs.com/pixy/p/4798089.html。
maven-compiler-plugin 编译配置
默认绑定到comile phase。当前版本的maven默认使用jdk1.5,使用更新的java版本必须手动配置。
<plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> <version>2.3.2version> <configuration> <source>1.6source> <target>1.6target> <encoding>utf-8encoding> <compilerArgument>-Xlint:nonecompilerArgument> <compilerArguments> <extdirs>libsextdirs> compilerArguments> configuration> plugin>
maven-surefire-plugin 单元测试
默认绑定到test阶段。
<plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-surefire-pluginartifactId> <configuration> <testFailureIgnore>truetestFailureIgnore> <skipTests>trueskipTests> <skip>trueskip> 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.pluginsgroupId> <artifactId>maven-jar-pluginartifactId> <version>2.4version> <configuration> <excludes> <exclude>agent.propertiesexclude> excludes> <archive> <manifest> <addClasspath>trueaddClasspath> <mainClass>com.demo.HelloWorldmainClass> manifest> archive> configuration> plugin>
maven-assembly-plugin 打包含依赖的全包
<plugin> <artifactId>maven-assembly-pluginartifactId> <configuration> <appendAssemblyId>falseappendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependenciesdescriptorRef> descriptorRefs> <archive> <manifest> <mainClass>com.defonds.RsaEncryptormainClass> manifest> archive> configuration> <executions> <execution> <id>make-assemblyid> <phase>packagephase> <goals> <goal>assemblygoal> 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.pluginsgroupId> <artifactId>maven-shade-pluginartifactId> <configuration> <transformers> <transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.xun.pf.sayHello.HelloWorldmainClass> transformer> transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlersresource>
transformer>
configuration> <executions> <execution> <phase>packagephase> <goals> <goal>shadegoal> goals> execution> executions> plugin>
这是由于一些包重复引用,打包后的 META-INF 目录多出了一些 *.SF 等文件所致。 解决方法是在configuration节点下加:
<filters>
<filter>
<artifact>*:*artifact>
<excludes>
<exclude>META-INF/*.SFexclude>
<exclude>META-INF/*.DSAexclude>
<exclude>META-INF/*.RSAexclude>
excludes>
filter>
filters>
ResourceTransformer
如修改配置文件,包含/排除特定文件等
http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#DontIncludeResourceTransformer
maven-shade-plugin插件有个配置属性:createDependencyReducedPom,默认值为true.
注意这个属性,如果你用这个插件来deploy,或者发布到中央仓库
这个属性会缩减你的pom文件,会把你依赖的
正确的做法是把这个值改成false
- <configuration>
- <createDependencyReducedPom>falsecreateDependencyReducedPom>
- configuration>
maven-war-plugin 打war包
<plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-war-pluginartifactId> <version>2.1.1version> <configuration> <warName>${project.artifactId}warName> <webResources> <resource> <directory>libs/directory> <targetPath>WEB-INF/libtargetPath> <includes> <include>**/*.jarinclude> includes> resource> webResources>
css/**,html/**
configuration> plugin>
maven-source-plugin 打包源码
生成的源码包名为xxxx-sources.jar。
<plugin>
<groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-source-pluginartifactId> <version>2.1.2version> <executions> <execution> <id>attach-sourcesid> <phase>verifyphase> <goals> <goal>jar-no-forkgoal> goals> execution> executions> plugin>
maven-exec-plugin 执行程序
直接调用java goal,执行java程序。
mvn exec:java -Dexec.mainClass="com.demo.HelloWorld"
在pom文件中配置
<plugin> <groupId>org.codehaus.mojogroupId> <artifactId>exec-maven-pluginartifactId> <version>1.2.1version> <executions> <execution> <phase>testphase> <goals> <goal>javagoal> goals> execution> executions> <configuration> <mainClass>com.demo.config.DocMappermainClass> <arguments> <argument>${project.build.outputDirectory}\doc-path-map.txtargument> <argument>${basedir}\srcargument> <argument>**/resource/*.javaargument> 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-pluginartifactId> <configuration> <outputDirectory>${project.build.directory}/liboutputDirectory> <excludeTransitive>falseexcludeTransitive> <stripVersion>truestripVersion> configuration> <executions> <execution> <id>copy-dependenciesid> <phase>packagephase> <goals> <goal>copy-dependenciesgoal> 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.0 -Dpackaging=jar -DgeneratePom=true
maven-release-plugin 整合svn创建tag、更新版本
暂无
参考文档:http://juvenshun.iteye.com/blog/376422
cobertura 测试覆盖率计算
基于jcoverage,原理是对class文件插桩,然后执行测试并生成覆盖率报告。参考资料。
Pom.xml文件需要加入配置:
<reporting>
<outputDirectory>target/siteoutputDirectory> <plugins> <plugin> <groupId>org.codehaus.mojogroupId> <artifactId>cobertura-maven-pluginartifactId> 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.mojogroupId> <artifactId>findbugs-maven-pluginartifactId> <version>2.3.1version> 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=
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.pluginsgroupId> <artifactId>maven-idea-pluginartifactId> <version>2.2version> <configuration> <jdkName>1.6jdkName> <jdkLevel>6.0jdkLevel> <downloadSources>truedownloadSources> <downloadJavadocs>truedownloadJavadocs> <dependenciesAsLibraries>truedependenciesAsLibraries> <useFullNames>falseuseFullNames> <deploymentDescriptorFile>src/main/webapp/WEB-INF/web.xmldeploymentDescriptorFile> configuration> plugin>
maven-jetty-plugin jetty服务器
<plugin> <groupId>org.mortbay.jettygroupId> <artifactId>maven-jetty-pluginartifactId> <version>6.1.6version> <configuration> <contextPath>/contextPath> <scanIntervalSeconds>3scanIntervalSeconds> <scanTargetPatterns> <scanTargetPattern> <directory>src/main/webapp/WEB-INFdirectory> <excludes> <exclude>**/*.jspexclude> excludes> <includes> <include>**/*.propertiesinclude> <include>**/*.xmlinclude> includes> scanTargetPattern> scanTargetPatterns> <requestLog implementation="org.mortbay.jetty.NCSARequestLog"> <filename>target/yyyy_mm_dd.request.logfilename> <retainDays>90retainDays> <append>trueappend> <extended>falseextended> <logTimeZone>GMTlogTimeZone> requestLog> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>80port> <maxIdleTime>60000maxIdleTime> connector> connectors> configuration> plugin>
maven-javadoc-plugin 生成java文档
<plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-javadoc-pluginartifactId> <version>2.4version> <executions> <execution> <goals> <goal>javadocgoal> goals> <phase>compilephase> execution> executions> <configuration> <encoding>UTF-8encoding> <verbose>falseverbose> <show>publicshow> <subpackages>com.pwrd.mobileqa.assist.resourcesubpackages> <doclet>com.sun.jersey.wadl.resourcedoc.ResourceDocletdoclet> <docletPath>${path.separator}${project.build.outputDirectory}docletPath> <docletArtifacts> <docletArtifact> <groupId>xercesgroupId> <artifactId>xercesImplartifactId> <version>2.6.1version> docletArtifact> ...... docletArtifacts> <useStandardDocletOptions>falseuseStandardDocletOptions> <additionalparam>-output ${project.build.outputDirectory}/resourcedoc.xmladditionalparam> configuration> plugin>
mybatis-generator-maven-plugin 生成mybatis映射
默认会在根目录查找并使用名为mybatis-config.xml的配置文件。
<plugin> <groupId>org.mybatis.generatorgroupId> <artifactId>mybatis-generator-maven-pluginartifactId> <version>1.3.0version> <configuration> <verbose>trueverbose> <overwrite>trueoverwrite> configuration> <executions> <execution> <id>Generate MyBatis Artifactsid> <goals> <goal>generategoal> goals> execution> executions> plugin>