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: