Friday, August 15, 2014

Automatic code coverage badge / reports for Java 8 project in

Recently, I've setup a Java project hosted on Github to get automatically build using TravisCI. It's super cool to have a nice badge saying that your recent builds are green. But then I discovered I could have some more report using free tools like TravisCI.
For example - code coverage tool like

This is where it got interesting quickly. It's written on the official site that it supports only dynamic languages but nothing about Java. But a quick search in Google pointed me to a maven plugin which connects with Coveralls' API. So I decided to give it a try.

Here are some useful links and hints for everybody trying to do it for his own project:
  1. Check if your project is Java 8If it is, you have to use the JaCoCo maven plugin, since this is the only code coverage tool supporting Java8 compiled classes. Use at least version than 0.7.0  (according to this bug report)

  2. Configure it your pom.xml to use JaCoCo
    You could use the snippet here but with newer JaCoCo version
  3. Configure in your parent pom.xml to use coveralls-maven-pluginCarefully read the README of the plugin. For security reasons don't publish your token on your repo.
  4. Update .travis.yml to try to upload your coverage report to
    after_success: mvn clean test jacoco:report coveralls:jacoco
  5. Add cool coverage badge to your README
    Like this one:  Coverage Status

    You've succesfully integrated: Github + TravisCI + + JaCoCO for a Java 8 Project

    Check out my example project which has successfully integrated all these wonderful systems.

    Kudos to Tapio Rautonen for creating such a useful plugin and documenting it in an excellent way!