04/30
2014

Maven常用插件配置和使用

主要介绍Maven的几个常见第三方插件(cobertura、findbugs、source、assembly、插件开发)配置和使用,接Maven介绍

 

maven本质上是一个插件框架,它的所有工作都交给插件来做,每个插件可以有多个goal。
除了自带的插件之外还有很多比较成熟的第三方插件,我们也很容易上手进行简单的插件开发,下面一一介绍

 

1 自带插件
maven自带的核心插件为Build plugins和Reporting plugins。
mvn compile编译源码实际上就利用到了maven-compiler-plugin,其他phase也类似用到了相应的插件
关于maven自带的核心插件见:http://maven.apache.org/plugins/index.html

 

2 第三方插件
2.1 maven有很多成熟的第三方插件
如jetty 对于web开发使用jetty作为容器
native 编译c和c++代码
sql 执行sql脚本
其他更多见:http://maven.apache.org/plugins/index.html#Outside_The_Maven_Land
下面具体介绍下单元测试覆盖率插件cobertura、findbugs

 

2.2 maven2的cobertura插件
2.2.1 cobertura

cobertura是一款用来计算java代码测试覆盖率的工具,基于jcoverage。能计算每个类、包、整个工程的行覆盖率和分支覆盖率以及代码复杂度(Cyclomatic complexity)并生成html或xml形式的报告,让用户很方便的查看代码的单元测试覆盖率情况。cobertura的原理是通过对class文件进行插桩然后计算。

 

2.2.2 maven2的cobertura插件介绍
插件地址为http://mojo.codehaus.org/cobertura-maven-plugin/index.html
a、首先在pom中添加配置如下

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

 
b、运行goal
到项目根目录下运行mvn cobertura:cobertura 将会插桩class文件、测试、生成覆盖率报告
cobertura支持的goal如下:
cobertura:check Check the Last Instrumentation Results.
cobertura:clean Clean up rogue files that cobertura maven plugin is tracking.
cobertura:dump-datafile Cobertura Datafile Dump Mojo.
cobertura:instrument Instrument the compiled classes.
cobertura:cobertura Instruments, Tests, and Generates a Cobertura Report.
c、在target\site\cobertura目录下生成报告文件,打开index.html可以查看具体报告
mvn cobertura:cobertura执行前会执行test phase,即执行单侧代码

 

2.3 maven2的findbugs插件
2.3.1 findbugs

是静态检查java代码的工具,根据一些bugs的表达式检查代码中的bugs,可以自定义检查规则

 

2.3.2 maven2的findbugs插件介绍
插件地址为http://mojo.codehaus.org/findbugs-maven-plugin/index.html
a、首先在pom中添加配置如下
不同goal的配置略有不同,可自己调整,以下介绍的是mvn findbugs:findbugs的配置

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

 
b、运行goal
到项目根目录下运行mvn findbugs:findbugs将会开始检查,并生成bugs报告
findbugs支持的goal如下:

findbugs:check
  Fail the build if there were any FindBugs violations in the source code. An
  XML report is put out by default in the target directory with the errors. To
  see more documentation about FindBugs' options, please see the FindBugs
  Manual..

findbugs:findbugs
  Generates a FindBugs Report when the site plugin is run. The HTML report is
  generated for site commands only.

findbugs:gui
  Launch the Findbugs GUI. It will use all the parameters in the POM fle.

findbugs:help
  Display help information on findbugs-maven-plugin.
  Call
    mvn findbugs:help -Ddetail=true -Dgoal=<goal-name>
  to display parameter details.

c、在target\site\findbugs目录下生成报告文件,打开index.html可以查看具体报告
mvn findbugs:findbugs绑定到了compile phase,即在编译时自动检查

 

2.4 maven的source插件
2.4.1 source

用来将工程打包成带源代码的jar包
2.4.2 pom配置如下

<build>
	<plugins>
	  <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>
	</plugins>
</build>

直接运行mvn clean install会在target下打出两个包,带***-sources.jar的为源码包

 

2.5 maven的assembly插件
2.5.1 assembly

可用来将工程依赖的jar包和工程都打成一个jar打包
2.5.2 pom配置如下

<build>
	<plugins>		  
	  <plugin>
		<artifactId>maven-assembly-plugin</artifactId>
		<configuration>
		  <descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		  </descriptorRefs>
		</configuration>
	  </plugin>
	</plugins>
</build>

直接运行mvn assembly:assembly会在target下出现***-with-dependencies.jar的jar包

 

2.6 插件开发
maven的插件开发相当简单,可以参考http://trinea.iteye.com/blog/1171957

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

13 thoughts on “Maven常用插件配置和使用