maven常用插件配置和使用

11 篇文章 0 订阅
11 篇文章 0 订阅

最新内容见:Maven常用插件配置和使用

 

本文主要是介绍maven的几个常见第三方插件(cobertura、findbugs、source、assembly、插件开发)配置和使用,接http://trinea.iteye.com/blog/1290898

 

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是一款用来计算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如下

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,即在编译时自动检查

http://qa.taobao.com/?p=4206

 

2.4 maven的source插件

2.4.1 source插件用来将工程打包成带源代码的jar包

2.4.2 maven2的source插件介绍

<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 maven2的assembly插件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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值