Reputation: 527
I have an Eclipse RCP application that is built via Maven using Tycho. It has some tests that are located in plugin fragments, so each plugin with operational code has its own fragment with the tests inside. The tests are executed during the maven build using Tycho Surefire. I want to measure the code coverage so I included the Sonarqube plugin which then uses Jacoco to collect the coverage data. This is where the problem is: The code coverage is recorded, but only for the test fragments. The plugins with the code under test are reported as having 0% code coverage. I suspect the issue is that Jacoco does not instrument the plugins that don't contain the tests, but I couldn't find any documentation on how to force instrumentation of code.
This is the parent pom with the relevant Sonarqube/Jacoco configuration:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my-project</groupId>
<artifactId>my-project-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<tycho-version>1.4.0</tycho-version>
<tycho-eclipserun-version>${tycho-version}</tycho-eclipserun-version>
<maven-surefire-plugin-version>2.22.2</maven-surefire-plugin-version>
<maven-compiler-plugin-version>3.8.1</maven-compiler-plugin-version>
<maven-clean-plugin-version>2.6.1</maven-clean-plugin-version>
<maven-dependency-plugin-version>2.10</maven-dependency-plugin-version>
<maven-resources-plugin-version>2.7</maven-resources-plugin-version>
<m2e-lifecycle-mapping-plugin-version>1.0.0</m2e-lifecycle-mapping-plugin-version>
<junit-version>4.12</junit-version>
<maven-checkstyle-plugin-version>3.1.0</maven-checkstyle-plugin-version>
<spotbugs-maven-plugin-version>3.1.12</spotbugs-maven-plugin-version>
<jacoco-maven-plugin-version>0.8.4</jacoco-maven-plugin-version>
<findbugs-maven-plugin-version>3.0.5</findbugs-maven-plugin-version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>${nexus-staging-maven-version}</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<extensions>true</extensions>
<configuration>
<stagingProfileId>${stagingProfileId}</stagingProfileId>
<serverId>${project.staging.repository.id}</serverId>
<nexusUrl>${project.staging.repository.url}</nexusUrl>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<resolver>p2</resolver>
<environments>
<environment>
<os>linux</os>
<ws>gtk</ws>
<arch>x86_64</arch>
</environment>
<environment>
<os>win32</os>
<ws>win32</ws>
<arch>x86_64</arch>
</environment>
<environment>
<os>macosx</os>
<ws>cocoa</ws>
<arch>x86_64</arch>
</environment>
</environments>
<target>
<artifact>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<classifier>${root-path}/dev/org.my-project/targetPlatforms/my-project</classifier>
</artifact>
</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin-version}</version>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>${maven-clean-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.eclipse.tycho.extras</groupId>
<artifactId>tycho-eclipserun-plugin</artifactId>
<version>${tycho-eclipserun-version}</version>
</plugin>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>${m2e-lifecycle-mapping-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${maven-site-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>${maven-project-info-reports-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin-version}</version>
<configuration>
<configLocation>${project.basedir}/${root-path}/dev/templates/checkstyle/Project Checkstyle Set v1.0.xml</configLocation>
<propertyExpansion>samedir=${project.basedir}/${root-path}/dev/templates/checkstyle</propertyExpansion>
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
<sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
</sourceDirectories>
<omitIgnoredModules>true</omitIgnoredModules>
<encoding>UTF-8</encoding>
<consoleOutput>false</consoleOutput>
<failsOnError>false</failsOnError>
<linkXRef>false</linkXRef>
<outputFileFormat>xml</outputFileFormat>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${findbugs-maven-plugin-version}</version>
<configuration>
<xmlOutput>true</xmlOutput>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>sonarqube-analysis</id>
<properties>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.java.checkstyle.reportPaths>${project.build.directory}/checkstyle-result.xml</sonar.java.checkstyle.reportPaths>
<sonar.java.spotbugs.reportPaths>${project.build.directory}/spotbugsXml.xml</sonar.java.spotbugs.reportPaths>
<sonar.coverage.jacoco.xmlReportPaths>${project.build.directory}/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<configuration>
<generatedSiteDirectory>false</generatedSiteDirectory>
<generateProjectInfo>false</generateProjectInfo>
<generateReports>true</generateReports>
<generateSitemap>false</generateSitemap>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<configuration>
<argLine>${tycho.testArgLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<includes>my.project.base.package.*</includes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<configuration></configuration>
<reportSets>
<reportSet>
<reports></reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<reportSets>
<reportSet>
<id>checkstyle-report</id>
<reports>
<report>checkstyle</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<reportSets>
<reportSet>
<id>spotbugs-report</id>
<reports>
<report>spotbugs</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<id>jacoco-report</id>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</profile>
</profiles>
</project>
I tried adding the base package for instrumentation in the jacoco plugin configuration...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<includes>my.base.package.*</includes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
...but Sonarqube still reports 0% coverage. As I said, there doesn't seem to be any documentation anywhere on how to configure this. Does someone know how to make this work?
Upvotes: 4
Views: 875