scoverage-maven-plugin

scoverage-maven-plugin is a plugin for Maven that integrates the scoverage code coverage library. Find out more about scoverage.

Join the chat at https://gitter.im/scoverage/scoverage-maven-plugin

Build Status Build Status License Maven Central

How to use

mostly used mojos:

additional, sometimes useful, mojos:

internal mojos:

Maven generated plugin documentation:

Version Documentation
1.4.0 Plugin Info
Prerequisities / limitations

Plugin is compatible with two Maven Scala compiler plugins:

Scoverage Maven plugin version

This can be set as project property.

<project>
    <properties>
        <scoverage.plugin.version>1.4.0</scoverage.plugin.version>
    </properties>
</project>
Scala version configuration

Plugin supports Scala 2.10.x, 2.11.x, 2.12.x and 2.13.x versions by automatically loading and configuring scalac-scoverage-plugin_2.10, scalac-scoverage-plugin_2.11, scalac-scoverage-plugin_2.12 or scalac-scoverage-plugin_2.13 Scalac SCoverage Plugin artifact. For this to work Scala version has to be set. It can be done by defining scalaVersion plugin configuration parameter or scala.version project property. Without this setting, coverage will not be calculated.

<project>
    <properties>
        <scala.version>2.13.0</scala.version>
    </properties>
</project>

or

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <scalaVersion>2.13.0</scalaVersion>
                    <!-- other parameters -->
                </configuration>
             </plugin>
        </plugins>
    </build>
</project>

The first method is better because once the property is defined it's value can be used in other places of the build file. For example in scala-library dependency version every Scala build should declare.

<project>
    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
    </dependencies>
</project>
Scalac SCoverage plugin version configuration

Maven SCoverage plugin uses by default the latest version of the scalac-scoverage-plugin available on its release day. If newer, better version of scalac-scoverage-plugin is available, it can be used instead. It can be configured by defining scalacPluginVersion plugin configuration parameter or scoverage.scalacPluginVersion project property.

<project>
    <properties>
        <scoverage.scalacPluginVersion>1.4.0</scoverage.scalacPluginVersion>
    </properties>
</project>

or

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <scalacPluginVersion>1.4.0</scalacPluginVersion>
                    <!-- other parameters -->
                </configuration>
             </plugin>
        </plugins>
    </build>
</project>
Integration tests coverage check and reports

integration-check and integration-report mojos are similar to check and report mojos, but they execute forked scoverage life cycle up to verify phase (integration tests are usually executed in integration-test phase).

Aggregated reports for multi-module projects

There is no separate mojo for aggregated reports, there is aggregate parameter. To additionally generate aggregated SCoverage report for root module, when generating regular reports, set aggregate parameter value to true. It works only in multimodule projects.

It can be configured by defining aggregate plugin configuration parameter or scoverage.aggregate project property.

<project>
    <properties>
        <scoverage.aggregate>true</scoverage.aggregate>
    </properties>
</project>

in build/plugins or build/pluginManagement section when running reports directly from console (e.g. mvn scoverage:report)

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <aggregate>true</aggregate>
                </configuration>
             </plugin>
        </plugins>
    </build>
</project>

or in reporting/plugins section when adding report to Maven generated site

<project>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <aggregate>true</aggregate>
                </configuration>
             </plugin>
        </plugins>
    </reporting>
</project>

Since version 1.4.0-M5 it's possible to generate aggregated report only, without generating reports for indivisual modules. For large projects it can decrease build time significantly.

To generate only aggregated SCoverage report, set aggregateOnly parameter value to true. It works only in multimodule projects.

It can be configured by defining aggregateOnly plugin configuration parameter or scoverage.aggregateOnly project property.

<project>
    <properties>
        <scoverage.aggregateOnly>true</scoverage.aggregateOnly>
    </properties>
</project>

in build/plugins or build/pluginManagement section when running reports directly from console (e.g. mvn scoverage:report)

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <aggregateOnly>true</aggregateOnly>
                </configuration>
             </plugin>
        </plugins>
    </build>
</project>

or in reporting/plugins section when adding report to Maven generated site

<project>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <aggregateOnly>true</aggregateOnly>
                </configuration>
             </plugin>
        </plugins>
    </reporting>
</project>
Adding SCoverage report to site

Add plugin to reporting section of your project and configure it to generate one of reporting mojos. By default Maven executes all plugin's reporting mojos, but SCoverage plugin has three such mojos and it does not make sense, to execute them all because every executed report will overwrite the previous one. Configure one of them depending on your case.

<project>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                            <!-- or <report>integration-report</report> -->
                            <!-- or <report>report-only</report> -->
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>
</project>

Which reporting mojo should be selected:

Reporting mojo When
report When not using integration tests (most cases)
integration-report When using integration tests
report-only When coverage data was already generated (usually by check or integration-check mojo)
Customizing code instrumentation

If you want to customize plugin's configuration parameters used by compilation supporting part of the plugin, do it in 'plugins' or 'pluginManagement' section:

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <highlighting>true</highlighting>
                    <!-- example configuration for Play! Framework 2.x project -->
                    <excludedPackages>views.html.*</excludedPackages> 
                    <excludedFiles>.*?routes_(routing|reverseRouting)</excludedFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Read SBT SCoverage Plugin documentation for more information about highlighting and excludedPackages.

Checking minimum test coverage level
<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <minimumCoverage>80</minimumCoverage>
                    <failOnMinimumCoverage>true</failOnMinimumCoverage>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal> <!-- or integration-check -->
                        </goals>
                    </execution>
                </executions>
             </plugin>
        </plugins>
    </build>
</project>

Run mvn scoverage:check to perform the check. See below if you want to use mvn verify to perform the check.

Read SBT SCoverage Plugin documentation for more information.

Checking minimum test coverage level AND adding report to site
<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <minimumCoverage>80</minimumCoverage>
                    <failOnMinimumCoverage>true</failOnMinimumCoverage>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal> <!-- or integration-check -->
                        </goals>
                    </execution>
                </executions>
             </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report-only</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>
</project>

Run mvn scoverage:check to perform the check and mvn scoverage:report to generate the report.

Checking minimum test coverage level automatically

If you want mvn verify and mvn install to check the coverage level, you have to change your POM so that SCoverage takes over running all the tests.

The reason for this is that SCoverage instruments classes during compilation and writes them to disk. We don't want to accidentally deploy these instrumented classes, so SCoverage keeps them separate. SCoverage does this by forking the current Maven build and running it again, while performing instrumentation. In a normal setup this causes the tests to be run twice: once in the outer run with the original classes and once in the SCoverage-forked run with the instrumented classes.

Since version 1.4.0-M5 it's possible to make the tests run only once. You have to configure your pom to turn off testing in the outer run and tell SCoverage to run all tests in the forked run.

This example shows the required configuration:

<project>
    <properties>
        <skipTests>true</skipTests> <!-- disable surefire and failsafe tests -->
    </properties>
...
    <build>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <minimumCoverage>80</minimumCoverage>
                    <failOnMinimumCoverage>true</failOnMinimumCoverage>

                    <!-- enable surefire and failsafe tests in SCoverage -->
                    <additionalForkedProjectProperties>skipTests=false</additionalForkedProjectProperties>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal> <!-- or integration-check -->
                        </goals>
                        <phase>prepare-package</phase> <!-- or any other phase -->
                    </execution>
                </executions>
             </plugin>
        </plugins>
    </build>
</project>

Run mvn clean verify or mvn clean install to run the tests with coverage and all other static analysis you have configured.

If you want to set multiple properties from within <additionalForkedProjectProperties>, for instance because you want to disable other plugins from running twice, you can separate them with a semicolon:

<additionalForkedProjectProperties>skipTests=false;skip.scalafmt=false</additionalForkedProjectProperties>

Examples

There are many example projects. Go to one of them and run mvn site.

License

This software is licensed under the Apache 2 license, quoted below.

Copyright 2014-2019 Grzegorz Slowikowski

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.