How can I incorporate Cobertura code coverage into my Ant script?

CategoryNeedProofRead I tried to follow this document for NB 6.5 and found these problems:

  • It is not explained how looks like
  • target -post-test-run cannot depend on test (would create cyclical dependencies)
  • Putting ${build.instrumented.dir} into run.test.classpath creates an error in the Project Properties > Libraries dialog


Cobertura is a code coverage tool and can be found at When incorporated into a build script it can then easily be used in a continuous integration engine like Hudson ( to monitor testing quality.


  • You are familiar with NetBeans.
  • You have an existing project that has a default build script.
  • You have downloaded Cobertura distribution 1.9 and above.
  • You have copied the (cobertura.jar, asm.jar, asm-tree.jar, jakarta-oro.jar, log4j.jar) jars into the <netbeans-install-root>/java2/ant/lib directory.
  • You have set your ANT_HOME system property to <netbeans-install-root>/java2/ant



  1. Open your NetBeans project.
  2. Open the "Files" tab.
  3. Open the <project-name>/nbprojects/ file
  4. Add the following lines to he properties file:
    1. build.instrumented.dir=${build.dir}/instrumented
  5. Edit the "run.test.classpath" property to look like:
  1. Save the properties file

Edit build.xml

  1. Open the <project-name>/build.xml
  2. Add the following entries:
    <property environment="env"/>
    <taskdef classpath="cobertura.jar" resource=""/>
    <target name="-pre-compile-test">
      <delete dir="${build.instrumented.dir}" />
      <delete dir="${}" />
      <mkdir dir="${build.instrumented.dir}" />
      <mkdir dir="${}" />
      <cobertura-instrument todir="${build.instrumented.dir}">
        <fileset dir="${build.classes.dir}">
            <include name="**/*.class"/>
    <target name="-post-test-run" depends="test">
      <!-- You can disable the html portion if you are using the hudson plugin and rely on the xml -->
      <cobertura-report format="html" srcdir="${src.dir}" destdir="${}"/>
      <!-- Used by the hudson plug-in -->
      <cobertura-report format="xml" srcdir="${src.dir}" destdir="${}"/>
      <delete file="cobertura.ser" />


There are a couple of ways to run the test:

  1. Create a batch or shell script file to do the run: build.bat
echo "Building: " %1
@echo off
set CP=c:/java/lib/j2ee/activation.jar:c:/java/lib/j2ee/common-annotations.jar:c:/java/lib/j2ee/connector.jar:
ant -Dj2ee.platform.classpath=%CP% %1
  1. Calling ant with the "-D" option to set the j2ee.platform.classpath
  2. Calling ant from within NetBeans and do not worry about the j2ee.platform.classpath property.


Once the setup is complete you can now have local code coverage reports in the build/report/cobertura directory so you can get instant coverage reports without checking into he integration server. A special thanks to the Cobetura team, and the NetBeans folks for having the sense to use an open build system like Ant.

[{Image src='report-example_CoberturaAnt.jpg' caption='Report Example' style='font-size: 80%; color: blue;' link=''}]

Special Note

If you do deploy and use the NetBeans build from Hudson or want a special J2EE Library to be used for testing, you need to pass the:

j2ee.platform.classpath to Ant via: "ant -Dj2ee.platform.classpath=<custome-path>/j2ee.jar".

This is very important for successful enterprise tests, and builds.

Applies to:

  • NetBeans IDE 6.0 and Above.
  • Cobertura 1.9


  • All
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo