jacoco-badge-maven-plugin

A simple maven plugin to generate JaCoCo build badges

License

License

Categories

Categories

Maven Build Tools JaCoCo Application Testing & Monitoring Code Coverage
GroupId

GroupId

com.sigpwned
ArtifactId

ArtifactId

jacoco-badge-maven-plugin
Last Version

Last Version

0.1.4
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

jacoco-badge-maven-plugin
A simple maven plugin to generate JaCoCo build badges
Project URL

Project URL

https://github.com/sigpwned/jacoco-badge-maven-plugin
Source Code Management

Source Code Management

https://github.com/sigpwned/jacoco-badge-maven-plugin.git

Download jacoco-badge-maven-plugin

How to add to project

<plugin>
    <groupId>com.sigpwned</groupId>
    <artifactId>jacoco-badge-maven-plugin</artifactId>
    <version>0.1.4</version>
</plugin>

Dependencies

compile (2)

Group / Artifact Type Version
org.apache.maven : maven-plugin-api jar 2.0
org.codehaus.plexus : plexus-utils jar 3.0.8

provided (1)

Group / Artifact Type Version
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.2

test (2)

Group / Artifact Type Version
com.google.guava : guava jar 24.0-jre
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

jacoco-badge-maven-plugin Build Status Test Coverage

A simple maven plugin to generate JaCoCo build badges

Configuration

Before we can generate a JaCoCo test coverage badge, we need to run JaCoCo test coverage!

JaCoCo measures test coverage of surefire unit tests and failsafe integration tests. Both cases require that JaCoCo be configured to integrate directly with the plugin in question.

Configuring JaCoCo for Unit Tests

Here is a simple setup for surefire and JaCoCo that generates a formatted HTML JaCoCo test coverage report.

<!-- Set up surefire to run just unit tests and take
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.15</version>
  <configuration>
    <argLine>${surefireArgLine}</argLine>
    <skipTests>${skip.unit.tests}</skipTests>
    <excludes>
      <!-- Standard approach to excluding integration tests -->
      <exclude>**/IT*.java</exclude>
    </excludes>
  </configuration>
</plugin>

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.0</version>
  <configuration>
    <excludes>
      <!-- Any exclusions you require, *.class -->
    </excludes>
  </configuration>
  <executions>
    <!-- Ask JaCoCo to generate a test report from surefire tests -->
    <execution>
      <id>prepare-code-coverage</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <propertyName>surefireArgLine</propertyName>
      </configuration>
    </execution>

    <!-- Ask JaCoCo to format test report into browsable HTML -->
    <!-- Multi-module builds should include report-aggregate and use the -->
    <!-- default outputDirectory. -->
    <!-- Single-module builds should exclude report-aggregate and use -->
    <!-- the given outputDirectory. -->
    <execution>
      <id>report-code-coverage</id>
      <goals>
        <goal>report</goal>
        <!-- <goal>report-aggregate</goal> -->
      </goals>
      <configuration>
        <!-- Multi-module builds should 
        <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
      </configuration>
    </execution>

    <!-- Make sure we have at least 70% coverage -->
    <execution>
      <id>verify-test-coverage</id>
      <goals>
        <goal>check</goal>
      </goals>
      <configuration>
        <rules>
          <rule>
            <element>BUNDLE</element>
            <excludes>
              <exclude>*Mojo</exclude>
            </excludes>
            <limits>
              <limit>
                <counter>INSTRUCTION</counter>
                <value>COVEREDRATIO</value>
                <minimum>70%</minimum>
              </limit>
            </limits>
          </rule>
        </rules>
        <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

With this configuration, running mvn test should generate a friendly HTML report of test coverage at target/site/jacoco-aggregate/index.html.

Configuring Badge Generation

Once JaCoCo has been configured to generate a formatted report, it's time to generate a badge from that report. Here is an example configuration of this plugin to generate a badge based on unit tests:

<plugin>
  <groupId>com.sigpwned</groupId>
  <artifactId>jacoco-badge-maven-plugin</artifactId>
  <version>0.1.3</version>
  <executions>
    <execution>
      <id>generate-jacoco-badge</id>
      <phase>verify</phase>
      <goals>
        <goal>badge</goal> <!-- Generate a badge from a unified JaCoCo report -->
      </goals>
      <configuration>
        <!-- What coverage level is considered passing? Optional, default 70. -->
        <passing>70</passing>

        <!-- Legal values: instruction, branch, line, method. Optional, default instruction. -->
        <metric>instruction</metric>
      </configuration>
   </execution>
  </executions>
</plugin>

Including the Badge in your README

There are a couple of good ways to include the badge file into your README:

  • GitHub Only -- Generate the badge file into a resource directory, and then embed a reference to the badge into your README. This has the benefit of being very simple, but won't allow for the testing of pull requests, etc. For an example, look at this README.

  • CodeBuild -- Save the generated badge file as an artifact; configure S3 to send an event every time a badge is uploaded; use a lambda function to copy the latest badge to a known, fixed location; embed a link to that fixed location.

Versions

Version
0.1.4
0.1.3
0.1.0