To make your build more portable, and therefore easier to deploy into Jenkins, it is a good idea to place
the Cobertura distribution you are using within your project directory, and to save it in your version
control system. This way it is easier to ensure that the build will use the same version of Cobertura no
matter where it is run.
Assuming you have downloaded the latest Cobertura installation and placed it within your project in a
directory called tools, you could do something like this:
<property name="cobertura.dir" value="/Users/johnsmart/Projects/Books/jenkins-the-definitive-guide/hudsonbook-content/tools/cobertura" />¶
<path id="cobertura.classpath">·
<fileset dir="${cobertura.dir}">
<include name="cobertura.jar" /> ̧
<include name="lib/**/*.jar" />¹
</fileset>
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
¶ Tell Ant where your Cobertura installation is.
· We need to set up a classpath that Cobertura can use to run.
̧ The path contains the Cobertura application itself.
¹ And all of its dependencies.
Next, you need to instrument your application classes. You have to be careful to place these instrumented
classes in a separated directory, so that they don’t get bundled up and deployed to production by accident:
<target name="instrument" depends="init,compile">¶
<delete file="cobertura.ser"/>·
<delete dir="${instrumented.dir}" /> ̧
<cobertura-instrument todir="${instrumented.dir}">¹
<fileset dir="${classes.dir}">
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
</cobertura-instrument>
</target>
¶ We can only instrument the application classes once they have been compiled.
· Remove any coverage data generated by previous builds.
̧ Remove any previously instrumented classes.
¹ Instrument the application classes (but not the test classes) and place them in the
${instrumented.dir} directory.
At this stage, the ${instrumented.dir} directory contains an instrumented version of our application
classes. Now all we need to do to generate some useful code coverage data is to run our unit tests against
the classes in this directory: