3

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?

Kavi Chinna
  • 237
  • 2
  • 7
  • 18

1 Answers1

2

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

Fabio Formosa
  • 904
  • 8
  • 27
  • Good advice. My only comment is about the snippet. There is a small mistake in the snippet: the property name is `sonar.coverage.exclusions` as you pointed out correctly with the command line option. – dbaltor Feb 02 '23 at 18:59
  • For those using gradle, here is the syntax: `sonarqube { properties { property "sonar.coverage.exclusions", "**/JavaClassToExclude.java" } }` – dbaltor Feb 02 '23 at 19:00
  • YĆ , correct. They both are valid properties, but for different purposes. I'm going to edit the answer to fix it, thans @dbaltor. – Fabio Formosa Feb 03 '23 at 14:56