When I set "sonar.exclusions" and "sonar.test.inclusions", unit test is "-", not a number, but coverage is right

  • SonarQube 8.3.1, Jenkins SonarQubeScanner plugin 4.6.0.2311, maven 3.6.3

I use maven command in Jenkins:

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install org.jacoco:jacoco-maven-plugin:report -DskipTests=false -Dmaven.test.failure.ignore=true -Dmaven.test.skip=false org.owasp:dependency-check-maven:aggregate -Dformats=XML,HTML -DfailOnError=false -DautoUpdate=false

Sonar Analysis Properties:

    sonar.login=admin
    sonar.password=admin
    sonar.projectVersion=${versionName}
    sonar.projectKey=$JOB_NAME
    sonar.projectName=$JOB_NAME
    sonar.sources=$WORKSPACE
    #sonar.exclusions=**/*Test*/**,**/*test*/**
    sonar.test=$WORKSPACE
    #sonar.test.inclusions=**/*Test*/**,**/*test*/**
    sonar.java.binaries=$WORKSPACE
    sonar.sourceEncoding=UTF-8
    sonar.coverage.jacoco.xmlReportPaths=**/jacoco.xml
    sonar.junit.reportPaths=module1/target/surefire-reports,module2/target/surefire-reports
    sonar.dependencyCheck.xmlReportPath=target/dependency-check-report.xml
    sonar.dependencyCheck.htmlReportPath=target/dependency-check-report.html

When I set “sonar.exclusions” and “sonar.test.inclusions”, the unit test is “-”, not a number.
When I delete this two properties,the scan will terminate with an error:xxxxTest.java can’t be indexed twice.
But coverage is right. I do not know why?
Someone help me. Thanks a lot!!

Hi,

Welcome to the community!

Combining inclusions and exclusions can be tricky. Before we get into that though, how are you analyzing? You’re building with Maven, but You’ve given a list of properties as though from a sonar-properties.project file (which is not picked up by Maven analysis).

If the properties you’re showing are indeed the ones used, then by setting sonar.sources to $WORKSPACE you’ve identified your tests as “code” files. But then Maven would know that they’re actually tests and include them in the list of tests => double counting/indexing.

But let’s start with your actual analysis command, please.

 
Ann

Thank you for your reply!
I use Jenkins + Jenkins SonarQubeScanner plugin + Jenkins Maven plguin to build the code scanning task. The build steps in Jenkins are as follows

  1. Pull the code first

  2. Then add “Call Maven Top Level Target” and configure the above mvn command.

  3. Finally, add “Execute SonarQube Scanner” and configure the above Analysis Properties.

Hi,

It sounds like you’re running a SonarScanner CLI analysis. I would start by switching that to a Maven analysis. So:

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install org.jacoco:jacoco-maven-plugin:report -DskipTests=false -Dmaven.test.failure.ignore=true -Dmaven.test.skip=false org.owasp:dependency-check-maven:aggregate -Dformats=XML,HTML -DfailOnError=false -DautoUpdate=false sonar:sonar

 
HTH,
Ann