Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Starting with v1.8, Cobertura will create detailed reports for Groovy source.  Below is an example build file for using Cobertura in your project.

Notice that you must compile your groovy source into .class files so that Cobertura can add instrumentation to those .class files.

 <p><a name="code1"><b>Listing 1.  build.xml</b></a><br />
<table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td><pre><code class="section">

<?xml version="1.0"?>
<project name="sample" default="jar" basedir=".">
    <!- properties for project directory structure ->
    <property name="dir.src" value="src"/>
    <property name="" value="build"/>
    <property name="dir.dist" value="dist"/>
    <property name="dir.lib" value="lib"/>

    <!- setup a project classpath that includes the external libs ->
    <path id="project.classpath">
        <!- include the classes in this project ->
        <pathelement location=" ${}"/>
        <!- include external libraries ->
        <fileset dir=" ${dir.lib/}" includes="*/.jar"/>
     <!- add external tasks ->
    <taskdef name="groovyc"    classpathref="project.classpath"   classname="org.codehaus.groovy.ant.Groovyc"/>
    <taskdef name="groovy" classpathref="project.classpath" classname="org.codehaus.groovy.ant.Groovy"/>
    <!- create output directories ->
    <target name="prepare">
        <mkdir dir=" ${}"/>

    <!- typical clean ->
    <target name="clean" description="Remove all generated files.">
        <delete dir=" ${}"/>
    <!- compile java and groovy source ->
    <target name="runGroovyC" depends="prepare,copyResourceFiles">
        <javac srcdir=" ${dir.src/}" destdir=" ${}">
            <classpath refid="project.classpath"/>
        <groovyc srcdir=" ${dir.src/}" destdir=" ${}" stacktrace="true">
            <classpath refid="project.classpath"/>
        <!-- work around if groovyc tasks doesn't work right
        <java classname="org.codehaus.groovy.ant.Groovyc" fork="yes" maxmemory=" ${maxmemory/}">
            <classpath refid="project.classpath"/>
            <arg value=" ${}"/>
            <arg value=" ${dir.src/}"/>

    <!- Cobertura Test Coverage Tool ->
    <path id="cobertura.classpath">
        <!- include the classes in this project ->
        <fileset dir=" ${dir.lib/}/cobertura" includes="*/.jar"/>
        <pathelement location="target/instrumented-classes"/>
        <pathelement location=" ${dir.src/}"/>

    <taskdef classpath=" ${dir.lib/}/cobertura/cobertura.jar" resource=""
    <!- adds the logging code to the already compiled class files ->
    <target name="instrument" >
       <delete quiet="false" failonerror="false">
        <fileset dir="target/instrumented-classes" includes="*/.class"/>
      <cobertura-instrument todir="target/instrumented-classes">
        <fileset dir=" ${}">
          <include name="*/.class"/>
          <exclude name="**/*Test.class"/>
    <!- setup class path to include instrumented classes before non-instrumented ones ->
    <path id="cover-test.classpath">
        <fileset dir=" ${dir.lib/}" includes="*/.jar"/>
        <pathelement location="target/instrumented-classes"/>
        <pathelement location=" ${}"/>

    <!- run all my junit tests using the instrumented classes ->
    <target name="cover-test" depends="instrument">
      <mkdir dir=" ${}/cobertura" />
        <junit printsummary="yes"
            haltonerror="no" haltonfailure="no" fork="yes">
            <formatter type="plain" usefile="false"/>
              <fileset dir="target/instrumented-classes" includes="**/*Test.class" />
            <classpath refid="cover-test.classpath"/>
    <!- create the html reports ->
    <target name="coverage-report" depends="cover-test">
        <cobertura-report srcdir=" ${dir.src/}" destdir="cobertura"/>


  • No labels