Using OpenClover for Code Coverage

How to set up OpenClover for Apache Ant

Courtney Zhan
3 min readMay 29, 2022

Lately, I have been looking into code coverage solutions in Java. I initially tried Cobertura, but it was a pain to get all its dependent libraries (many of them). So, I tried Clover code coverage and found it was much easier to set up. Clover was acquired by Atlassian (renamed Atlassian Clover) in 2007 but was turned open source in 2017 (this version is named OpenClover).

This tutorial will show you how to set up OpenClover for Apache Ant.

Note that OpenClover has its own Apache Ant installation guide. However, I had some trouble with the generating the build report when I followed the guide so I have written this article.

Demonstration

Here is a run of the Clover code coverage for my sample Java project.

Commands you can run yourself

Follow the commands from the above video to try it out yourself (Apache-Ant is required).

git clone https://github.com/testwisely/buildwise-samples.git
cd buildwise-samples/ant-junit
ant -lib lib/clover clean with.clover test.unit.console
ant clover.html
open build/clover-html/index.html

Setting up OpenClover

The buildwise-samples repo already had the OpenClover in the build.xml file. For using OpenClover in your project, follow these steps:

  1. Add the clover.jar file (downloadable from openclover.org) to the lib folder.
clover.jar located in lib/clover

I put it in a separate subfolder lib/clover to separate it from the app’s libraries.

In the build.xml file, follow the Ant QuickStart Guide on the OpenClover website. For convenience, the steps are summarised below:

2. Add the Clover ant task definition at the top of the file before any targets are defined:

<taskdef resource="cloverlib.xml" classpath="lib/clover/clover.jar"/>

3. Create a target to set up Clover for use.

<target name="with.clover">
<clover-setup/>
</target>

4. Add the desired report target for your report output type/s.
Clover supports HTML, PDF, XML and command line reporting. I will use the HTML type here but see the guide for the other report types.

<target name="clover.html">
<clover-html-report outdir="build/clover-html"/>
</target>

You can change the output directory in the outdir argument.

Run with Ant and Generate Your Clover Report

You need to run two Ant commands to get your Clover coverage report. From the QuickStart guide it is a bit misleading.

First, run the tests with Clover activated (with.clover):

ant -lib lib/clover clean with.clover test.unit.console

Note: The -lib tells ant where to find the clover.jar file.

This will generate a .clover folder, which will contain several artefact files. If you look inside it, the files will be named similar to these:

ant-junit % ls .cloverclover4_4_1.db clover4_4_1.db1ve9rdjkh1hkc_2_exisgz_l3qlqbrv.s
clover4_4_1.db1ve9rdfmetdmg_0_exisgz_l3qlqbqv.s clover4_4_1.db1ve9rdjkh1hkc_2_njjup9_l3qlqbrv.s
clover4_4_1.db1ve9rdfmetdmg_0_njjup9_l3qlqbqv.s clover4_4_1.dbexisgz_l3qlqbqg
clover4_4_1.db1ve9rdhlfxfl1_1_exisgz_l3qlqbrl.s clover4_4_1.dbnjjup9_l3qlqbr0

Next, run the clover reporting target (in my case, HTML):

ant clover.html

Open the index.html file inside the output directory specified in the clover.html target. It will display the code coverage of your program.

Full Build.xml

For the full build.xml file, see the sample repo’s file here:

--

--

No responses yet