maven常用插件总结
2015-11-19 14:27
459 查看
maven本质上是一个插件框架,几乎所有的功能都是通过各种各样的插件来实现的。maven默认会依据项目类型自动把构建时的各阶段(Lifecycle和phase)自动绑定(Lifecycle Mapping)到特定插件(plugin)提供的功能点(goals)上。例如java项目编译阶段(compile),实际上是调用了maven-compiler-plugin插件提供的compile功能点(goal)来实现的。
方式二:直接调用插件的某个功能(goal)。如mvn maven-jar-plugin:jar。maven有一个约定,如果插件的名字叫maven-xxxx-plugin或xxxx-maven-plugin的话。可以直接用mvn xxxx:goal的方式调用其提供的功能。所以前面这个命令就可以简写成:mvn jar:jar。这种方式只会执行指定的goal。
调用goal完整的命令格式为:
关于资源的配置,还可以参考:http://www.cnblogs.com/pixy/p/4798089.html。
运行时指定:
更多的配置可以参考另一篇文件:http://www.cnblogs.com/pixy/p/4718176.html
常用配置1:打出可以运行的有主类的jar包
运行mvn assemlby:assembly在target下生成xxxx-with-dependencies.jar
assembly 插件的一个 bug:http://jira.codehaus.org/browse/MASSEMBLY-360,它在对第三方打包时,对于 META-INF 下的 spring.handlers,spring.schemas 等多个同名文件进行了覆盖,遗漏掉了一些版本的 xsd 本地映射。
这个插件只有shade:shade一个唯一的goal,绑定到package pahse。
这是由于一些包重复引用,打包后的 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>
在pom文件中配置
goals:
install-file 将本地jar包安装到本地仓库
参考文档:http://juvenshun.iteye.com/blog/376422
Pom.xml文件需要加入配置:
运行 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.
pom.xml中配置:
运行 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>
运行:mvn eclipse:eclipse 生成.classpath和.project文件,
一、调用插件提供的功能(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.0 -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>
相关文章推荐
- iOS获取设备的语言
- 平板中UI 模拟餐馆真实布局
- ScheduledExecutorService定时周期执行指定的任务
- 工作中的程序员如何进阶
- Eclipse中jsp、js文件编辑时,卡死现象解决汇总
- 在Asp.net MVC中使用Authorization Manager (AzMan)进行Windows用户身份认证
- 使用jquery ajaxForm提交表单
- javascript 进制转换(2进制、8进制、10进制、16进制之间的转换)
- form表单实现用户注册界面
- javascript Date format(js日期格式化)
- 获得chekbox的id
- java通过反射Method、属性字段名、值、数据类型
- 判断网络是否可用
- JAVA 定时器时间格式
- java图片格式转换的三段代码
- 在启动页中判断是否自动登录
- 学习C++ vector(摘录)
- 汉诺塔问题
- UIBezierPath和CAShapeLayer结合绘制任意弧度的圆形(包含旋转动画)
- CentOS 7 设置静态IP