Using the Crap4j Ant Task
Next, unzip it somewhere. This location will be used as the CRAP4J_HOME variable.
Inside the crap4j directory, there is a doc directory with an example_build.xml. We'll be using that as the basis of this description.
1) Inside your build file, set the CRAP4J_HOME property to point to your install directory.
Note: you can also pass CRAP4j_HOME into your ant launch with -D, and it will override what is in the file. (Also, be sure you have ANT_HOME set if the execution complains that it can't find Ant.)
<property name="CRAP4J_HOME" value="/Users/bobevans/Documents/projects/crap4j/org.crap4j/dist/crap4j"/>
Then paste in the taskdef for the crap4j task. Notice it includes all the necessary jars from the install directory.
<taskdef name="crap4j" classname="org.crap4j.anttask.Crap4jAntTask" >
<include name="**/*.jar" />
Finally create a target that launches crap4j. Inside this target you need to specify your project properties:
<pathElement location="bin" />
<pathElement location="src" />
<pathElement location="test_bin" />
<include name="**/*.jar" />
The crap4j task takes a projectdir attribute, which is the root project directory. This will be used as the base for the classpath settings if they are specified relatively (without initial drive letter or '/').
It takes outputDir, an output directory where the crap4j report will be deposited. The default is agitar/reports/crap4j.
The crap4j task requires 4 classpath entries to find the necessary data: classDirs, srcDirs, testClassDirs, and libClasspath. All 4 of these are Ant file tasks that can use any of Ant's file specification mechanisms.
classDirs is used to specify the project classes that you want analyzed.
srcDirs is used to specify where the sources for classDirs is to be found.
testClassDirs is used to specify where the tests that are to be run are located. Compiling the tests to a separate directory makes it easy to identify which classes are the JUnit tests. If you don't compile into a separate directory, specify the same directories as the classDirs, and the tool will name match for classes that start or end with Test to find the test classes.
It has four optional parameters, dontTest, debug, downloadAverages, server.
dontTest computes the CRAP score without running the tests -- this means that coverage will not be re-computed. If there is old coverage data in the agitar/.results directory, it will be picked up in the calculation, so you might want to re-run the tests, or clean that directory before running crap4j. (default is false)
debug will dump extra verbose information about the execution of Crap4j. (default is false)
downloadAverages will get the average of all projects seen at crap4j.org and include it in the CRAP percent bar graph on the report. (default is true)
server is the crap4j server from which to get project averages, and which to share your results if you desire. The option to share shows up as a link on the generated Crap4j report. (default is http://www.crap4j.org/benchmark/)