Tuesday, June 17, 2014

Implement FindBugs using ANT

What is FindBugs?

FindBugs is an open source program used to find bugs in Java code.It uses static analysis to identify hundreds of different potential types of errors in Java programs.

Potential errors are classified in four ranks: (i) scariest, (ii) scary, (iii) troubling and (iv) of concern.

This is a hint to the developer about their possible impact or severity. 

It is very similar to PMD, FindBugs operates on Java bytecode, rather than source code. Whereas PMD works on source code.

It can find major issues with code such as Possible NullpointerException, Security flwas, Performance issues etc. and many more. 


So, Let's do it >>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Following are required jar files:

FindBugs Version 3.0.0:

 annotations.jar
ant.jar
asm-debug-all-5.0.2.jar
bcel-6.0-SNAPSHOT.jar
commons-lang-2.6.jar
dom4j-1.6.1.jar
findbugs-ant.jar
findbugs.jar
jaxen-1.1.6.jar
jcip-annotations.jar
jdepend-2.9.jar
jFormatString.jar
jsr305.jar
yjp-controller-api-redist.jar

Documentation:


I am demonstrating
Ant Targets:

<!-- FindBugs Configurations -->
<!-- Put all jar files given above to classpath, in my case all the jar files are in the 
               D:\JavaTools\Findbugs\findbugs-3.0.0\lib -->

<path id="findbugs.classpath">
<fileset dir="D:\JavaTools\Findbugs\findbugs-3.0.0\lib">
<include name="*.jar" />
</fileset>
</path>

<!--Creating the required directories for findbugs--> <target name="findbugs-init" description="Creates the required directories for findbugs."> <echo>******Cleaning the ${basedir}/findbugs-reports******</echo> <delete dir="${basedir}/findbugs-reports" /> <echo>******Creating the required directories for findbugs******</echo> <mkdir dir="${basedir}/findbugs-reports" /> </target>

<target name="findbugs" depends="findbugs-init,compile" description="Run code analysis over code to check for problems.">
       <!-- Fail this target if FindBugs is not installed. -->
<echo>*****Performing FindBugs analysis on ${project.name}*****</echo>
       <taskdef name="findbugs"
                classname="edu.umd.cs.findbugs.anttask.FindBugsTask"
        classpathref="findbugs.classpath" />

          <!-- Run FindBugs.for HTML output-->
<findbugs classpathref="findbugs.classpath"
      output="html"
             outputFile="${basedir}/findbugs-reports/findbugs.html"
     stylesheet="${basedir}/findbugs/fancy-hist.xsl">

<!-- fancy-hist.xsl stylesheet can be found in findbugs.jar file, 
       There are other stylesheets available and can be used based on the needs -->

<!-- An optional nested element which specifies a source directory path containing source files used to  compile the Java code being analyzed.By specifying a source path, any generated XML bug output will have complete source information, which allows later viewing in the GUI. -->

          <sourcePath path="${src.dir}" />

 <!-- auxclasspath is an optional nested element which specifies a classpath (Jar files or        directories) containing classes used by the analyzed library or application, but which you don't want to analyze.It is specified the same way as Ant's classpath element for the Java task.-->

<auxclasspath refid="classpath"/>

<!-- A optional nested element specifying which classes to analyze.The class element must specify a location attribute which names the archive file (jar, zip, etc.), directory, or class file to be analyzed. Multiple class elements may be specified as children of a  single findbugs element. In addition to or instead of specifying a class element, the FindBugs task can contain one or more fileset element(s) that specify files to be analyzed. For example, you might use a fileset to specify that all of the jar files in a directory should be analyzed. -->

<class location="${build.dir}/WEB-INF/classes" />

        </findbugs>

 <!-- Run FindBugs.for XML output -->
<findbugs classpathref="findbugs.classpath"
     output="xml"
             outputFile="${basedir}/findbugs-reports/findbugs.xml">

<!-- An optional nested element which specifies a source directory path containing source files used to  compile the Java code being analyzed.By specifying a source path, any generated XML bug output will have complete source information, which allows later viewing in the GUI. -->

          <sourcePath path="${src.dir}" />

 <!-- auxclasspath is an optional nested element which specifies a classpath (Jar files or        directories) containing classes used by the analyzed library or application, but which you don't want to analyze.It is specified the same way as Ant's classpath element for the Java task.-->

<auxclasspath refid="classpath"/>

<!-- A optional nested element specifying which classes to analyze.The class element must specify a location attribute which names the archive file (jar, zip, etc.), directory, or class file to be analyzed. Multiple class elements may be specified as children of a  single findbugs element. In addition to or instead of specifying a class element, the FindBugs task can contain one or more fileset element(s) that specify files to be analyzed. For example, you might use a fileset to specify that all of the jar files in a directory should be analyzed. -->

<class location="${build.dir}/WEB-INF/classes" />

  </findbugs>
<echo>*****FindBugs analysis report saved into ${basedir}/findbugs-                    reports/findbugs.xml*****</echo>

</target>


  <!-- Compile the source code -->
<target name="compile" depends="init">
<echo>******Compile the source files******</echo>
<javac debug="on" srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes"
includes="**/*">
<classpath refid="classpath" />
</javac>
<!-- Compile the test cases -->
<javac debug="on" srcdir="${test.dir}" destdir="${build.dir}/WEB-INF/test"
includes="**/*">
<classpath refid="classpath" />
</javac>

</target>


No comments:

Post a Comment

Thanks for your comments/Suggestions.