Saturday, 16 June 2012

Configuration of Findbugs with Jenkins for Ant projects

After spending a lot of time on the configuration of Findbugs with Jenkins for Ant projects. I thought of putting it altogether just to save your time:-)

Well lets have a brief introduction about these two tools.

Jenkins:- Jenkins is an awesome tool that monitors executions of repeated jobs, such as building a software project or jobs run by cron.
Findbugs:- FindBugs is a static analysis tool that finds coding mistakes by examining Java classfiles or Jar files. It actually reads your bytecode of the class files. Static analysis means that FindBugs can find bugs by simply inspecting a program's code: executing the program is not necessary.

So, let's dive in a step by step manner.

First install the default plugin provided by the Jenkins.
Goto >> Dashboard >> Manage Jenkins >> Findbugs Plugin. This plugin requires the utility plugin-in called
"Static Analysis Utilities". Please ensure that the latest version of this plug-in is also installed.

Ant Configuration:-

Download the .zip or .tzr.gz from http://findbugs.sourceforge.net/downloads.html
Extract it in the plugins folder of the jenkins.
Copy the "lib/findbugs-ant.jar" from your FindBugs installation to the "lib" folder of your ant installation.
Set the environmental variables:-
ANT_HOME----
JAVA_HOME----
FINDBUGS_HOME----{Your findbugs .zip(extracted path)}
Go to Java>>jdk>>jre>>lib copy findbugs-ant.jar.

Now comes the most important part to write ant script for your findbugs.So there you go.



      
        Ant build sample for running 
        - findbugs
   










 
 






   
       
   


       
           
               
           
       
   


   
   
       
       
       
              message="Error: FINDBUGS_HOME not set or findbugs.jar not found."/>
       
                 classname="edu.umd.cs.findbugs.anttask.FindBugsTask"
                 classpath="${findbugs.home.dir}/lib/findbugs-ant.jar"/>

       
       
       
 jvmargs="-Xmx1024m"
 timeout="3600000"
 home="${findbugs.home.dir}"
 workHard="true"
 output="xml:withMessages" 
                                  outputFile="${report.findbugs.dir}/findbugs.xml">
 
 
       


   
  
   
       
 out="${htm.dir}/findbugs.html"
 style="${findbugs.home.dir}/src/xsl/default.xsl" />

   
    
   
            description="Run the build"
            depends="clean, 
prepare.report.dir,
                findbugs, 
                report.findbugs">
   
 





INCLUDING MULTIPLE JAR FOR ANALYSIS:-

There are basically three ways to include multiple jars your build.xml
1. Put all the jars inside the  
     one by one.
2. Use rejarForAnalysis  script inside the bin folder of your findbugs.zip folder. There has not been a proper     document given by the findbugs team. http://findbugs.sourceforge.net/manual/rejarForAnalysis.html. This I   think is a little vague. So I thought of putting everything altogether.
    Install Cygwin on the windows to run the rejarForAnalysis.sh on your windows environment.
    http://cygwin.com/cygwin-ug-net/setup-net.html
    Command to create jar(using multiple jars) from cygwin.
    find . -name "*.jar" | xargs "${findbugs.home.dir}\bin\rejarForAnalysis"
 This will create a single jar comprising of multiple jars. Put this single jar for analysis.

3. Let's make it a little more simple. (Frankly speaking it took almost half a day to conclude this)....:-)
    Add this in your build.xml just before calling the findbugs task.---->
    
    
 
 


This will create a single jar at run-time and you don't need to install cygwin.


========================================================================\All the best with Jenkins and Findbugs.






4 comments:

  1. Hi! This seems fairly recent so I hope you are going to be adding more...I'm currently trying to learn about integrating FindBugs with Jenkins for my job and I got excited when I saw this blog! I'm super interested in what you had to go through so even if you don't post please feel free to contact me -- that way when I have a question I know who to go to :)

    ReplyDelete
  2. Of course I am going to add more. Though if you have any specific question you can always ask me...:-)

    ReplyDelete
  3. can you post this examples? i know its a old one but the markup is all wrong

    ReplyDelete
  4. Can you please explain what actually are you looking for, so that I can help.

    ReplyDelete