Skip to content
Roch Delsalle edited this page Nov 21, 2013 · 2 revisions

Assuming you are using maven-scala-plugin : First, make sure your main and test scalac binding goals are separated in the plugin configuration and have referenceable ID's, so we can extend the main compilation with instrumentation support. Something like this:

<build>
  <plugins>
    <plugin>
      <groupId>org.scala-tools</groupId>
      <artifactId>maven-scala-plugin</artifactId>
      <executions>
        <execution>
          <id>main-scalac</id>
          <goals><goal>compile</goal></goals>
        </execution>
        <execution>
          <id>test-scalac</id>
          <goals><goal>testCompile</goal></goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Then add a custom profile which binds the coverage instrumentation to the main compilation goal by ID, and sets all the necessary environment variables for the test run:

<profiles>
  <profile>
    <id>coverage</id>
    <dependencies>
      <dependency>
        <groupId>com.github.scct</groupId>
        <artifactId>scct_2.9.2</artifactId>
        <version>0.2.1</version>
      </dependency>
    </dependencies>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.6</version>
          <configuration>
            <systemPropertyVariables>
              <scct.project.name>${project.name}</scct.project.name>
              <scct.coverage.file>${project.build.outputDirectory}/coverage.data</scct.coverage.file>
              <scct.report.dir>${project.build.directory}/coverage-report</scct.report.dir>
              <scct.source.dir>${project.build.sourceDirectory}</scct.source.dir>
            </systemPropertyVariables>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.scala-tools</groupId>
          <artifactId>maven-scala-plugin</artifactId>
          <executions>
            <execution>
              <id>main-scalac</id>
              <configuration>
                <args>
                  <args>-P:scct:projectId:${project.name}</args>
                  <args>-P:scct:basedir:${project.basedir}</args>
                </args>
                <compilerPlugins>
                  <compilerPlugin>
                    <groupId>com.github.scct</groupId>
                    <artifactId>scct_2.9.2</artifactId>
                    <version>0.2.1</version>
                  </compilerPlugin>
                </compilerPlugins>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

Run your unit tests with:

$ mvn -Pcoverage clean test

Then open:

./target/coverage-report/index.html

Multiproject builds

Add the following bits and pieces to your parent pom:

<pluginRepositories>
  <pluginRepository>
    <id>sonatype-nexus-snapshots</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
  </pluginRepository>
</pluginRepositories>
...
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-site-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <reportPlugins>
          <plugin>
            <groupId>com.github.scct</groupId>
            <artifactId>maven-scct-multiproject-report</artifactId>
            <version>0.2-SNAPSHOT</version>
          </plugin>
        </reportPlugins>
      </configuration>
    </plugin>
  </plugins>
</build>

Then run coverage for all child projects with:

$ mvn -Pcoverage clean test

This creates individual coverage reports into $childProjectDir/target/coverage-report/ Merge all the existing child reports into $parentProjectDir/target/site/coverage-report/ with:

$ mvn com.github.scct:maven-scct-multiproject-report:scct-report
Clone this wiki locally