Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 325
Siddhu Avatar asked Sep 27 '13 10:09

Siddhu


2 Answers

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

<sonar.test.exclusions>
   **/test/*
</sonar.test.exclusions>
like image 53
Wenbing Li Avatar answered Sep 19 '22 17:09

Wenbing Li


Either add a step to remove the folder before running the SonarQube analysis. Or set exclusions on test files. See http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-IgnoreFiles

like image 22
David RACODON - QA Consultant Avatar answered Sep 19 '22 17:09

David RACODON - QA Consultant