Search code examples
spring-bootmavensonarqubegitlab-ci

Sonar exclusions are not working which mentioned in sonar-project.properties file


I'm using GitLab CI/CD pipeline for deploying the Springboot project. I have integrated SonarQube in my project. In my sonar-project.properties file I have mentioned the sonar.coverage.exclusions. But its not excluded properly.

below is my Project structure

Test-Service

  • user-service
  • async-service
  • audit-service

So, I have added the sonar property file under Test-service, in that file I have mentioned the sonar coverage exclusions like below

sonar.coverage.exclusions=**/com/cadmium/async/business/config/*,**/com/cadmium/async/business/domain/*,**/com/cadmium/audit/business/config/*,**/com/cadmium/audit/business/domain/*,**/com/cadmium/user/business/config/*,**/com/cadmium/user/business/domain/*

and In gitlab-ci.yml file I have specified the sonar job like below

Sonar_test:
  stage: sonar
  #when: manual
  image: maven
  script:
    - mvn --batch-mode verify sonar:sonar -Dsonar.host.url=sonar-url -Dsonar.login=admin  -Dsonar.password=admin -Denv="$PROFILE" -Dsonar.qualitygate.wait=true

The problem is, when I'm running the pipeline its not considering my sonar.coverage.exclusions list, So build gate getting failed because of coverage.

If I mentioned the sonar exclusion list in gitlab.yml file like below its working fine

Sonar_test:
  stage: sonar
  #when: manual
  image: maven
  script:
    - mvn --batch-mode verify sonar:sonar -Dsonar.host.url=sonar-url -Dsonar.login=admin  -Dsonar.password=admin -Denv="$PROFILE" -Dsonar.coverage.exclusions=**/com/cadmium/async/business/config/*,**/com/cadmium/async/business/domain/*,**/com/cadmium/audit/business/config/*,**/com/cadmium/audit/business/domain/*,**/com/cadmium/user/business/config/*,**/com/cadmium/user/business/domain/* -Dsonar.qualitygate.wait=true

Why its not considering the exclusion list when I mentioned in sonar-project.properties file?


Solution

  • As a sonar sourcer says here, maven projects cannot use sonar-project.properties.

    You can define the properties directly in the pom.xml

    <properties>
         ...
        <sonar.coverage.exclusions>**/JavaClassToExclude.java</sonar.coverage.exclusions>
      </properties>
    

    or via the attribute -Dsonar.coverage.exclusions=**/JavaClassToExclude.java in command line like you've mentioned