为了了解findbugs的插件运行原理,特地翻了findbugs插件的源码,插件的源码视图如下:
FindBugsMojo.java就是我们的findbugs主类
代码如下:
/**
* Generates a FindBugs Report when the site plugin is run.
* The HTML report is generated for site commands only.
*
* @goal findbugs
* @phase compile
* @requiresDependencyResolution compile
* @requiresProject
* @threadSafe
* @author <a href="mailto:gleclaire@codehaus.org">Garvin LeClaire</a>
* @version $Id: FindBugsMojo.groovy 16932 2012-06-21 01:13:14Z gleclaire $
*/
public class FindBugsMojo
extends AbstractMavenReport
implements groovy.lang.GroovyObject
findbugs的实现是使用groovy实现的,在FindBugsMojo.groovy中找到executeFindbugs的方法,
ant.java(classname: "edu.umd.cs.findbugs.FindBugs2", inputstring: getFindbugsAuxClasspath(), fork: "${fork}", failonerror: "true", clonevm: "false", timeout: "${timeout}", maxmemory: "${maxHeap}m") {
def effectiveEncoding = System.getProperty( "file.encoding", "UTF-8" )
if ( sourceEncoding ) {
effectiveEncoding = sourceEncoding
}
log.debug("File Encoding is " + effectiveEncoding)
sysproperty(key: "file.encoding" , value: effectiveEncoding)
if ( jvmArgs && fork ) {
log.debug("Adding JVM Args => ${jvmArgs}")
String[] args = jvmArgs.split(FindBugsInfo.BLANK)
args.each() {jvmArg ->
log.debug("Adding JVM Arg => ${jvmArg}")
jvmarg(value: jvmArg)
}
}
if ( debug || trace ) {
sysproperty(key: "findbugs.debug" , value: true)
}
classpath() {
pluginArtifacts.each() {pluginArtifact ->
log.debug(" Adding to pluginArtifact ->" + pluginArtifact.file.toString())
pathelement(location: pluginArtifact.file)
}
}
findbugsArgs.each { findbugsArg ->
log.debug("Findbugs arg is ${findbugsArg}")
arg(value: findbugsArg)
}
}
调用的是edu.umd.cs.findbugs.FindBugs2来实现检查代码的,这个是findbugs.jar中实现的,maven插件是调用findbugs.jar来实现的。
findbugs.jar可以单独使用,findbugs的maven插件中java与groovy是如何相关交互的这些都是新的问题
然后在findbugs的maven pom中的依赖
<dependency>
<groupId>
com.google.code.findbugs
</groupId>
<artifactId>
findbugs-ant
</artifactId>
<version>
${findbugsVersion}
</version>
</dependency>
version是2.0.1
<findbugsVersion>2.0.1</findbugsVersion>