Add code coverage metrics to your maven project

While working on the bitcoinj project I have added support for code coverage collection and tracking. This post summarizes steps you have to take to add that for your own maven projects.

1. Figure out what type of code coverage you want

For the bitcoinj library it is important to have a “unit test coverage” – it works by verifying which parts of the source code are reached during unit testing phase during build process.
This type of code coverage is the one we usually mean when we say “code coverage”, but it’s important to know that you can also instrument a live application and see which parts of application are used and how frequently – you should consult for example wikipedia here: or here: and choose the tool you want to use.
Because for libraries we have to focus on “unit test coverage” I have choosen JaCoCo

2. Add JaCoCo maven plugin

Adding JaCoCo maven plugin is quite straightforward, those are the parts you have to add to your pom.xml:
<!-- Code coverage plugin, generates coverage report to target/site/jacoco/
     To skip coverage generation add -Djacoco.skip=true
         <exclude>**/Protos*.class</exclude>         <!-- Exclude generated protobuf classes -->
         <exclude>org/bitcoinj/jni/*</exclude>         <!-- Exclude JNI classes -->
         <exclude>org/bitcoin/*</exclude>         <!-- Exclude native classes -->
Important things to note here:
  • in excludes we can exclude files from being included in the report – it’s worth to exclude automatically generated files
  • the jacoco.exec file will be useful in the next steps as well
Additionally you have to update configuration for maven-surefire-plugin:
<!-- Unit tests plugin, to skip runing test add -Dmaven.test.skip -->
      <argLine>${surefireArgLine}</argLine>      <!-- This is required for code coverage to work. -->
These 2 plugins work together – JaCoCo will be used as a Java agent during the unit testing phase.
From now on you can just run
mvn clean test
Which will rerun all tests and recreate coverage report.
to skip running tests and generating coverage report run maven commands like this:
mvn clean package -Dmaven.test.skip=true -Djacoco.skip=true

3. Setup support

As a additional step (especially for projects using travis-ci) you can configure to keep track of your code coverage over time.
It’s enough to add this maven plugin:
<!-- Coveralls is a online code coverage tool -->
And then in the .travis.yml add this:
  - mvn jacoco:report coveralls:report

4. Add Intellij IDEA integration

Following guide works for Intellij Community edition (I have tested this on version 14 but it should work on earlier versions as well)
To add Intellij integration you have to do following things:
  1. Hit Ctrl+Alt+6
  2. Select previously generated file jacoco.exec
  3. The code coverage report should refresh every time you run mvn test
This is how Intellij looks with code coverage enabled Imgur
  • A redundant commenter

    Hi Wojciech, I tried below but it did not work:





    <!-- exclude generated code from coverage report -->