`
阅读更多

项目采用maven构建,想使用findbugs-maven-plugin 插件进行代码静态分析

官网:http://findbugs.sourceforge.net/

a program which uses static analysis to look for bugs in Java code

 

项目目录结构如下:

 最下面的文件就是maven 的配置文件pom.xml,类似于ant的build.xml文件,pom.xml内容如下:

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.kunlunsoft</groupId>
	<artifactId>isChinese</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<checkstyle.config.location>checkstyle.xml</checkstyle.config.location>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<reporting>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>findbugs-maven-plugin</artifactId>
				<version>2.5.2</version>
			</plugin>
		</plugins>
	</reporting>

	
</project>

 

 

此时target目录是空的。打开cmd,进入项目所在目录,运行mvn findbugs:findbugs

运行结果如下:

从上图(mvn findbugs:findbugs的运行结果) 来看,maven 并没有运行findbugs,为什么会这样呢?

我们看看findbugs官网是如何说明的:

FindBugs requires JRE (or JDK) 1.5.0 or later to run. However, it can analyze programs compiled for any version of Java, from 1.0 to 1.8. The current version of FindBugs is 2.0.2, released on 10:49:15 EST, 10 December, 2012. We are very interested in getting feedback on how to improve FindBugs. File bug reports on our sourceforge bug tracker

findbugs虽然是一个静态分析工具,但是它分析的不是java源代码(后缀名为.java),而是class文件(编译后的文件)。在运行mvn findbugs:findbugs 时,不会自动编译项目,即没有class文件,所以findbugs没有运行。

有的技术博客说:“clean findbugs:findbugs install ,这种写法是错的,可以运行的,但是并不产生findbugs报告”,说法是对的,但是并没有说明原因。

原因:运行clean后,class文件都被删除了,所以不会运行findbugs,或者说findbugs没有可分析的class文件,自然就没有产生分析结果。

使用maven运行findbugs前一定先编译,一定要有class文件!

解决方法:

(1)mvn clean compile findbugs:findbugs

(2)mvn clean test findbugs:findbugs (test会调用compile生命周期)

如下图:

 运行完之后,target目录会增加如下文件:findbugsXml.xml

测试结果是xml格式的,不方便查看、展示.

我们希望以html格式来展示findbugs的运行结果(报告),如下图:

 

如上图所示,如何实现html展示findbugs报告呢?

请参阅我的下一篇博客:http://hw1287789687.iteye.com/blog/1960331 

也可以参阅 http://stackoverflow.com/questions/8975096/maven-checkstyle-configlocation-ignored

  • 大小: 19.2 KB
  • 大小: 44 KB
  • 大小: 68.8 KB
  • 大小: 83.4 KB
2
0
分享到:

相关推荐

Global site tag (gtag.js) - Google Analytics