Sonar Maven Plugin: How do I exclude test source directories?

Siddhu picture Siddhu · Sep 27, 2013 · Viewed 26.9k times · Source

I have a Maven project with Java sources and Scala test sources. I generate code coverage using Jacoco during the verify stage. When I try to run the sonar goal either during the verify phase by adding an execution, or by running mvn verify sonar:sonar, I end up with the test directory being added twice by Sonar:

  [INFO] [11:15:34.756] Test directories:
  [INFO] [11:15:34.756]   /Users/xxx/Documents/workspace/misc/xxx/src/test/scala
  [INFO] [11:15:34.756]   /Users/xxx/Documents/workspace/misc/xxx/src/test/scala/../scala

which results in the analysis failing with the following error:

Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.1:sonar (default-cli) on project kv-mapper: Can not execute SonarQube analysis: Unable to read and import the source file : '/Users/xxxx/Documents/workspace/misc/xxx/src/test/scala/../scala/xxx/xxxxx/xxx/xxx/xxxxx/xxxxxx.java' with the charset : 'UTF-8'. Duplicate source for resource

My pom.xml (for Sonar) looks like this.

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>${sonar.plugin.version}</version>
        <!-- no default executions -->
        <configuration>
            <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
            <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
            <sonar.language>java</sonar.language>
            <sonar.jacoco.itReportPath>
                ${basedir}/target/jacoco.exec
            </sonar.jacoco.itReportPath>
            <sonar.exclusions>
                **/test/*
            </sonar.exclusions>
        </configuration>
    </plugin>

How do I configure Sonar to either:

  • exclude test/scala directory entirely? or
  • remove the duplicate directory?

Answer

Wenbing Li picture Wenbing Li · May 8, 2014

sonar.test.exclusions should be used instead of sonar.exclusions

<sonar.test.exclusions>
   **/test/*
</sonar.test.exclusions>