Findbugs & Ant - Stop the build for particular violation - java

I am building my application using ANT and I am checking my code for any Findbugs violations.
Now, my objective is to stop the build whenever my code contains particular findbug violation.
Is this possible using ANT & Findbugs?
N.B. Preferably not to write to any custom class.

Use the warningsProperty attribute on your findbugs task to set a property for any warnings:
<findbugs ... warningsProperty="findbugsFailure"/>
and fail task if warnings are produced:
<fail if="findbugsFailure">
For example:
<property name="findbugs.home" value="/export/home/daveho/work/findbugs" />
<target name="findbugs" depends="jar">
<findbugs home="${findbugs.home}"
output="xml"
outputFile="bcel-fb.xml"
warningsProperty="findbugsFailure">
<auxClasspath path="${basedir}/lib/Regex.jar" />
<sourcePath path="${basedir}/src/java" />
<class location="${basedir}/bin/bcel.jar" />
</findbugs>
<fail if="findbugsFailure">
</target>

An alternative idea (and worth the effort) would be to integrate Sonar into your ANT build process.
Sonar integrates Findbugs (and checkstyle and PMD) and you can centrally configure it to fail the build against any set of criteria using it's build breaker plugin. See:
How do I make Hudson/Jenkins fail if Sonar thresholds are breached?

Related

Ant to Gradle: JUnit (4.11) Test conversion

I'm not very familiar with JUnit, but I'm attempting to translate an ant target that makes use of junit into the gradle equivalent. It's not going so hot, since I'm getting some failures on the gradle side -- I'm under the impression that it's due to inputs not being found somehow/somewhere, but I can't confirm, since it's not readily present in the ant target.
Here's the ant script:
<target name="testing">
<junit printsummary="yes" showoutput="yes">
<classpath refid="classpath"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="someOutputLocation">
<fileset dir="${base}/testCode">
<include name="**/included.java"/>
<exclude name="**/excluded.java"/>
</fileset>
</batchtest>
</junit>
</target>
And here's (one of the variations) of my gradle attempt:
task testing(type:Test){
useJUnit()
testClassesDir = file("testCodeCompiled")
include '**/included.class'
exclude '**/excluded.class'
classpath = classpath
}
The two errors I get:
junit.framework.AssertionFailedError
java.lang.NullPointerException
The AssertionFailedError is supposedly caused by multiple JUnit dependencies, which I don't have. I import a local version of only junit-4.11.
I really don't know why it's not working, though I suspect it's due to some of gradle's complexities. I've seen people mention an ant-junit library, which I may try to use to at least replicate the results from within gradle.
EDIT: A thought occurs: I found JUnit within some of gradle's src files. By calling useJUnit(), I may be using that instead? If so, there could be a double dependency after all? Nope. Got rid of useJUnit() and the local jar separately. The former behaved as it did before whereas the latter exploded.
MORE INFO: The cause may likely be that the compiledTestCode is missing several of the directories/data that testCode contains. I probably have to copy over the relevant files. Alternatively, is there a way to make gradle's JUnit use .java files instead of .class?
For those of you wondering, the code is fine. I'm just dumb.

Most sensitive findbugs options in ant

I am running find bugs in ant and am trying to set everything (attributes, options, etc) in my ant build.xml to the most sensitive settings for finding bugs. From reading the documentation and looking at example here is what I concluded will be the most sensitive settings for if a bug is ran into. If it is not please let me know any attribute, options, etc that needed to be added or changed to find all the bugs that may be in my code.
<target name="findbugs" depends="jar">
<findbugs home="/home/me/Desktop/findbugs"
output="html"
outputFile="bc.html"
effort="max"
reportLevel="low"
workHard="true"
debug="true">
<auxClasspath path="../foo/bin"/>
<auxClasspath path="../bar/bin"/>
<sourcePath path="../foo2/src" />
<class location="bin/"/>
</findbugs>
</target>
No need for workHard="true" since you already have effort='max'. They're just synonyms of each other. Otherwise, it looks like you have it.
There are includeFilter and excludeFilter, but if you don't list either of those, you get all bugs.

FindBugs scanning external jars

I am using the following link to create an ant script to run findbugs on a web application:
Chapter 6. Using the FindBugs™ Ant task
I am setting the auxClasspath parameter to my jars folder.
But when i run the task using ant findbugs from the command prompt, it takes a very long time(~45 minutes) and the output xml contains analysis of the jars in the auxClasspath as well as my source code.
I want only my source code to be analyzed.
This is the code in my build.xml:
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"/>
<property name="findbugs.home" value="C:/Software/FindBugs" />
<target name="findbugs" >
<echo message="Finding Bugs From ${basedir}/src/java"/>
<findbugs home="${findbugs.home}"
output="xml:withMessages"
outputFile="${basedir}\findbugs.xml"
stylesheet="fancy-hist.xsl"
timeout="6000000"
jvmargs="-Xmx1200m">
<auxClasspath path="${basedir}/Jars/*.jar" />
<sourcePath path="${basedir}/src/java"/>
<class location="${basedir}/build/myApp-1.0.jar" />
</findbugs>
</target>
I have added findbugs-ant.jar to lib of my ant installation.
The findbugs directory exists.
Other information:
IDE: Netbeans 7.3
OS: Microsoft Windows XP
Ant Version: 1.8.4
Find Bugs Version: 2.0.2
Update
If i leave out this line:
<auxClasspath path="${basedir}/Jars/*.jar" />
I get my desired output(i.e. analysis of only my source code).
But it raises a warning:
[findbugs] The following classes needed for analysis were missing:
[findbugs] javax.servlet.http.HttpServlet
[findbugs] javax.servlet.http.HttpServletRequestWrapper
[list continues]....
Any idea, why find bugs is analyzing jars which it should not analyze(according to the documentation)
I tried to track which Jars are being used in the source code.
In the findbugs xml output, i found a line: ${basedir}\Jars\antlr-2.7.2.jar
The findbugs analysis report showed that all the other jars(except antlr-2.7.2.jar) were missing.
There were no more auxClassPath entries. Solved this by specifying each class path entry in a different line.
If anyone has any better ideas, kindly contribute.
I used the following auxClasspath settings to pull in all my jars in my lib folder and all the jars in directories under my lib folder.
<auxClasspath>
<fileset dir="${lib.dir}">
<include name="*/**"/>
<include name="*.jar"/>
</fileset>
</auxClasspath>
Place it inside the findbugs tag.
Try to remove this line
<class location="${basedir}/build/myApp-1.0.jar" />
Now to try to analyze both jar and source files, that's it takes so long time.
This line is needed and used to find a class if it encounters during analysis of your source.
<auxClasspath path="${basedir}/Jars/*.jar" />
Maybe to can limit it to only really needed jars, not all in the folder.

Running junit ant-task without build.xml

I'm trying to work out how I can run an ant task without actually needed a build.xml.
In particular I want to run a JUnit task with a formatter. In xml format this looks like below:
<junit printsummary="true" errorProperty="test.failed" failureProperty="test.failed">
<classpath refid="run.class.path" />
<!-- console log -->
<formatter type="xml" classname="be.x.SFFormatter" />
<test name="be.x.SF" outfile="result" todir="${build.output.dir}" />
</junit>
It works when running the ant script, but I would like to get my app running as a runnable jar.
Running the tests from Java was easy:
JUnitCore junit = new JUnitCore();
testResult = junit.run(SeleniumFramework.class);
However, I struggle to work out how to actually get the formatter to work.
The formatter is of type org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter so I doubt I can just plug it in somewhere without running ant.
Has anyone done something similar before?
Thanks!
Ant doesn't do any magic. All it does is read the XML file, create the beans specified in it and then execute the methods as per the Task API (org.apache.tools.ant.Task).
So all you need is to do the same in your code. Don't forget to create a Project :-)
You may use Ant via Groovy to avoid the xml syntax.
See => Using Ant from Groovy for details.

When using ANT, how can I define a task only if I have some specific java version?

I have the problem that an specific step in Ant can only be executed when we have Java 1.5 installed in the build computer. The task definition uses uses a jar file that was compiled using 1.5, so running with a 1.4 virtual machine will throw an IncompatibleClassVersion exception.
I have to find a solution meanwhile to have this task working for this specific project that requires 1.4, but a question came to me. How can I avoid defining this task and executing this optional step if I don't have a specific java version?
I could use the "if" or "unless" tags on the target tag, but those only check if a property is set or not. I also would like to have a solution that doesn't require extra libraries, but I don't know if the build-in functionality in standard is enough to perform such a task.
The Java version is exposed via the ant.java.version property. Use a condition to set a property and execute the task only if it is true.
<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="default">
<target name="default" depends="javaCheck" if="isJava6">
<echo message="Hello, World!" />
</target>
<target name="javaCheck">
<echo message="ant.java.version=${ant.java.version}" />
<condition property="isJava6">
<equals arg1="${ant.java.version}" arg2="1.6" />
</condition>
</target>
</project>
The property to check in the buildfile is ${ant.java.version}.
You could use the <condition> element to make a task conditional when a property equals a certain value:
<condition property="legal-java">
<matches pattern="1.[56].*" string="${ant.java.version}"/>
</condition>

Categories

Resources