Friday, September 12, 2014

Coveralls for Java, Travis & the Maven Invoker

The other day, a colleague of mine integrated deadcode4j with Coveralls.
I had looked at Coveralls before, but seeing it did not support Java, I quickly turned away. Fortunately, my colleague knew of a neat Maven Plugin that does the job: it simply sends a JaCoCo (or Cobertura or Saga) coverage report to Coveralls using their API.

Integration was pretty easy. Then again, I noticed some missing paths and worse coverage than on my local machine. It took me a considerable amount of time to track down the problem; also, I found no place describing the issue (let alone a solution). In case you have some issues with Maven running on Travis, you might want to read on.

Missing Coverage

To record the code coverage, I actually executed unit tests, integration tests and (as deadcode4j is a Maven Plugin) the Invoker Plugin. By comparing the coverage reported on Coveralls with my local results, I could narrow down my problems to the Invoker Plugin: coverage results from the invoker execution were not showing up at Coveralls. After endless trickery with the travis.yml file, I finally stumbled over the problem: Travis exports a MAVEN_OPTS variable just before Maven is executed (by the usage of /etc/mavenrc), thus effectively preventing it from being overwritten. As this is how the JaCoCo Maven Plugin records the Invoker's code coverage, it just couldn't enable code coverage for the Invoker.

Solution

There are two approaches to work around that issue, all coming down to adding something to your travis.yml file:

The sledgehammer method would be to remove the /etc/mavenrc file:

  before_install: sudo rm /etc/mavenrc

If you refrain from using sudo, you may skip the execution of /etc/mavenrc (note however, that this approach also skips the execution of ~/.mavenrc:

  before_install: export MAVEN_SKIP_RC=true

That's about it. Enjoy running your tests! ;)

About deadcode4j

deadcode4j is a Maven plugin which finds dead code. To use it, go to the console, navigate to your Maven project, type
  mvn de.is24.mavenplugins:deadcode4j-maven-plugin:find ‑Dmaven.test.skip=true
and see what it finds.

Hop on over to GitHub to learn more, browse the code, or contribute.

Here you can read how it all started.

No comments:

Post a Comment