/jacoco-gerrit-maven-plugin

Maven plugin that collects JaCoCo code coverage for a particular Gerrit review request and uploads the data to Gerrit.

Primary LanguageJava

JaCoCo to Gerrit Maven plugin

JaCoCo to Gerrit Maven plugin enables your Gerrit with test code coverage visible as an editor's gutter.
Gerrit Test Code Coverage Gutter Example

What it does

This maven plugin collects test line coverage for files changed in a particular Gerrit changset and uploads the results to Gerrit. However, following tweaks need to be in place

Usage

In Gerrit, the user (denoted by gerrit.user bellow) needs to have enabled HTTP access: Settings -> HTTP Password -> Generate HTTP password.

To run this maven plugin

  1. at first, execute mvn test (or mvn verify) with enabled JaCoCo probes (see next chapter to get hints how to do that), which creates jacoco.exec file with coverage data. This data file is read by this plugin and the collected information is uploaded to Gerrit.

  2. to run this plugin:

    • if executed as a Jenkins/Hudson job with enabled Gerrit Trigger (if the build is triggered by an Gerrit Event, several system environment variables (prefixed with GERRIT_*) are set and used by this plugin)

        $ net.uvavru.maven.plugin:jacoco-gerrit-maven-plugin:jacoco-to-gerrit -Djacoco.execFile=target/jacoco.exec -Dgerrit.password=***** -Dgerrit.port=8080 -Dgerrit.username=robot -Dgerrit.host=gerrit.someorg.com
      
    • otherwise additional parameters must be set explicitly:

        $ ... -Dgerrit.projectName=gerrit-project-name -Dgerrit.branchName=master -Dgerrit.changeId=I156abb8bf7d9bbb151ebfe130bb4568320c3252b -Dgerrit.revisionId=d80c5bffddcebc7d88baf57ad4cc3da42a9692e3
      

      these parameters are configurable by system environment variables as well: GERRIT_PROJECT, GERRIT_BRANCH, GERRIT_CHANGE_ID, GERRIT_PATCHSET_REVISION (which are automatically set by Jenkins/Hudson if Gerrit Trigger is used).

Running tests with JaCoCo probes

To run maven tests with JaCoCo probes, jacoco agent has to be enabled in the JVM running the tests. For further information, refer to Jacoco plugin site.

  1. Configure org.jacoco:jacoco-maven-plugin to generate JVM agent property or to set surefire or failsafe argLine directly

  2. Configure the location where to write Jacoco probes - the jacoco.exec file

  3. Configure maven-surefire-plugin (or maven-failsafe-plugin) to use the enhanced JVM parameters

  4. Execute test (or verify) maven phase

    $ mvn clean test
    

    or

    $ mvn clean verify
    
  5. Now, when the jacoco.exec file is generated, this plugin can be run in order to collect relevant information and upload it to Gerrit.

As a concrete example of a configuration of Jacoco and test execution in Maven, one can consider looking at Jersey sources.

Notes

This maven plugin is a substitution for https://github.com/muryoh/sonar-gerrit-plugin which is capable of the same functionality (i.e., to upload test code coverage to Gerrit). However, in that case, Sonarqube is required and the Sonar analysis has to be executed.