Findbugs 是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题。利用这个工具,就可以在不实际运行程序的情况对软件进行分析。它可以帮助改进代码的质量。
Findbugs提供了方便操作的可视化界面,同时也可以作为Eclipse的一个插件来使用,而我们使用得最多的还是作为Eclipse的插件来使用。
doCheck.bat
------------
@ant -f fingbugs.xml
---------
fingbugs.xml
---------------------------------
<project name="sharedTargetsMacros" default="findbugs">
<!-- DEFINE SHARED PROPERTIES -->
<property name="project.path" value="D:/projectAddr"/>
<property name="auditCode.path" value="${project.path}/classes/com/此次省略" />
<!--<property name="findbugs.home" value="C:/Programing/Java/findbugs-1.3.9" />-->
<property name="findbugs.home" value="E:/Apps/findbugs-1.3.9" />
<property name="lib.path" value="${project.path}/WEB-INF/lib"/>
<!-- DEFINE PATH REFERENCES -->
<path id="findbugs.classpath">
<fileset dir="${findbugs.home}/lib" includes="*.jar" />
</path>
<target name="metrics" depends="findbugs"/>
<target name="findbugs">
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpathref="findbugs.classpath"/>
<findbugs projectName="projectName" home="${findbugs.home}" output="html" outputFile="projectName.html" jvmargs="-Xmx256m -Xmx256m" reportLevel="Low" excludeFilter="findbugs_exclude.xml"><!--过滤文件-->
<auxClasspath path="classes" />
<!--
<auxClasspath path="${lib.path}/*.jar" />
<auxClasspath path="${lib.path}/test/*.jar" />
<auxClasspath path="${lib.path}/wseeclient/*.jar" />
-->
<auxClasspath path="${lib.path}/struts.jar" />
<auxClasspath path="${lib.path}/jcs-1.3.jar" />
<auxClasspath path="${lib.path}/poi-2.5.1-final-20040804.jar" />
<auxClasspath path="${lib.path}/javax.ejb_3.0.1.jar" />
<auxClasspath path="${lib.path}/javax.jsp_1.1.0.0_2-1.jar" />
<auxClasspath path="${lib.path}/json-lib-2.2.2-jdk15.jar" />
<auxClasspath path="${lib.path}/commons-lang.jar" />
<auxClasspath path="${lib.path}/commons-httpclient-2.0.jar" />
<auxClasspath path="${lib.path}/weblogic.jar" />
<auxClasspath path="${lib.path}/quartz-all-1.6.0.jar" />
<auxClasspath path="${lib.path}/jaxb1-impl-2.2.jar" />
<auxClasspath path="${lib.path}/junit-4.8.1.jar" />
<class location="${auditCode.path}/action/*.class" />
<class location="${auditCode.path}/bean/*.class" />
<class location="${auditCode.path}/delegate/*.class" />
<class location="${auditCode.path}/form/*.class" />
<class location="${auditCode.path}/helper/*.class" />
<class location="${auditCode.path}/helper/ap/*.class" />
<sourcePath path="${lib.path}/trunk/src/com/此处省略/**/*.java" />
</findbugs>
</target>
</project>
-------------------
过滤文件
<FindBugsFilter>
<Match>
<Method name="main" />
</Match>
<Match>
<Class name="~.*\.*Test" />
</Match>
<Match>
<Class name="~.*\.*Value" />
</Match>
<Match>
<Class name="~.*\.*Value\$.*" />
</Match>
<Match>
<Class name="~.*\.*Table" />
</Match>
</FindBugsFilter>
这样会过滤Value和Table,Cache等类
<project name='annogen.findbugs' default='all.clean' basedir='.'>
<!-- Properties -->
<property environment='env'/>
<property name='output_dir' location='build/findbugs'/>
<property name='output_file' location='${output_dir}/index.html'/>
<property name='findbugs_home' location='external/findbugs'/>
<property name='findbugs_jar' location='${findbugs_home}/lib/findbugs-ant.jar'/>
<property name='stax_api_jar' location='external/lib/stax-api.jar'/>
<property name='junit_jar' location='external/lib/junit.jar' />
<path id='sourcepath'>
<dirset dir='.'>
<include name='**/src/'/>
<include name='**/src_150'/>
</dirset>
</path>
<!-- Targets -->
<target name='all.clean' depends='clean,all'/>
<target name='clean'>
<delete dir='${output_dir}'/>
</target>
<target name='all'>
<mkdir dir='${output_dir}'/>
<taskdef name='findbugs'
classname='edu.umd.cs.findbugs.anttask.FindBugsTask'
classpath='${findbugs_jar}'/>
<findbugs home='${findbugs_home}'
output='html'
outputFile='${output_file}' >
<auxClasspath path='${stax_api_jar};${env.ANT_HOME}/lib/ant.jar;${junit_jar}' />
<sourcePath refid='sourcepath' />
<class location='build/classes'/>
</findbugs>
<echo message=''/>
<echo message='Findbugs complete, output in ${output_file}'/>
</target>
<target name='view' depends='all.clean'>
<exec dir='.' executable='explorer.exe' os='Windows 2000, Windows XP'>
<!--<arg line="X:\metajam\jam\annogen_new\build\findbugs\index.html"/>-->
<arg line='${output_file}'/>
</exec>
</target>
</project>
本文介绍 FindBugs 工具的配置及使用方法,FindBugs 是一款强大的静态代码分析工具,用于检查 Java 类文件和 JAR 文件中的潜在错误。文章详细展示了如何通过 Ant 构建脚本来集成 FindBugs,并设置了过滤规则。
4677

被折叠的 条评论
为什么被折叠?



