Enable Jacoco with Jetty

To enable code coverage in your functional regression test, for example when using Selenium, it’s often handy to measure the code coverage. In our current project we’re using Jacoco for that, since it’s currently the only open source solution which is still maintained and suitable for Java 7.

To enable Jacoco in Jetty, we need to do the following steps:

  1. Download the Jacoco agent
  2. Enable the Java agent in Jetty
  3. Enable the reporting in Maven

Download the Jacoco agent.

The latest version of the Jacoco agent can be downloaded here. In the downloaded jar is another jar, the jacocoagent.jar, which is needed to do online instrumenting of classes.

Enable the Java agent in Jetty.
Once the Jacoco Agent has been downloaded, Jetty needs to be made aware of this. To do this, Jetty needs to be passed the -javaagent parameter. The easiest way to do this is by setting the MAVEN_OPTS environment variable in the following way:


Alternatively, you can also pass parameters to the -javaagent setting, like this:


Enable the reporting in Maven

To enable the reporting in Maven the Jacoco Maven plugin is needed. This can be configured in the following way:

Once this has been configured, the report can be created using the jacoco:report task. This will create a jacoco directory in the reporting directory, which is by default the target/site directory. When everything has been set up correctly, a report like the following should be available:


Jacoco code coverage with Gradle

One of the new features of Gradle 1.6, the Groovy based build tool, is Jacoco support. Jacoco is one of the few (only?) Open Source code coverage tools still in active development, and starting from Gradle 1.6, it’s now a supported plugin. Enabling the task is easy, just add

to your build.gradle, and run ./gradlew jacocoTestReport and it.. does nothing. For those running into the same issues, this might save you a bit of time:

After a bit of searching, it turns out the Jacoco plugin is a bit broken. When using it with a Java project, as I’m doing, you need to do a couple of things to enable it, but also then, it unfortunately doesn’t work as advertised, though the fix seems not to complex.

First, you need to enable the following:

And then, instead of running ./gradlew jacocoTestReport as mentioned in the docs here, you need to run ./gradlew test jacocoTestReport. Unfortunately, no location is printed where the documentation is created, so no clickable links in your terminal, but you can find it under build/reports/jacoco/test/html/index.html. Well, that wasn’t so bad now, was it? Have fun!

More resources

For more information about using Jacoco and Gradle, you can checkout the following resources: