Exclude of non-related class in gradle showing coverage in sonar dashboard

I am currently using community edition of sonar Version 7.9.1. Added the below piece of code in my build.gradle for the exclusion of some of classes, but sonar dashboard of this particular project shows coverage for the below class file.

sonarqube {
properties {
property “sonar.exclusions”, “com.sample.scenario.repository.*”
}
}

Hi,

Have you tried configuring this exclusion through the UI?

 
Ann

I haven’t checked that but i have many projects repository so that developers can add their exclusion in build.gradle. Otherwise i need to configure exclusion for all the projects in UI.

Hi,

Right now I see two possibilities:

  • the exclusion configuration is wrong
  • the gradle property configuration is wrong

IIWY, I’d use the UI to refine the exclusion configuration to make sure it’s working as expected, and then try to move it into your Gradle file.

 
HTH,
Ann

Hi Ann,

I tried adding the exclusion in UI and it works perfectly fine.

But my only concern is if this is the approach i follow then i have to configure for every projects (let’s say i have 100 projects) . It is difficult to add the exclusion for all the projects. And whenever developers add new java class files then i need to manually update them in UI

Do do suggest me any other approach ? or if the exclusion configuration is wrong then please provide me the steps to add exclusion in build.gradle.

Hi,

If you’ve gotten the exclusion to work from the UI, this it’s “just” a question of how you’re passing the value into analysis which IMO is more a Gradle question than an analysis question.

Regarding managing this configuration, you have a couple options:

  • figure out how to set this via gradle & let the devs handle it - the option you’re pursuing
  • make sure a dev on every team has admin on the team’s projects & let them configure it at the project level via the UI
  • if there are common exclusions, configure them at the global level; they’ll be inherited by every project that doesn’t override the exclusions locally

 
HTH,
Ann

I have tried the below scenario in build.gradle but it didn’t help

sonarqube {
    properties {
        property 'sonar.exclusions', "com.sample.scenario.repository"
    }
}

scenario 2

sonarqube {
    properties {
        property 'sonar.coverage.exclusions', "**/com/sample/scenario/repository/**"
    }
}


Your scenarios appear to be mixing excluding sources file from analysis overall and excluding them just from coverage.

If you got the setting working properly based on the UI, then specifying the exact same property with the exact same value in quotes within the build.gradle file should work.

If you don’t mind tracking another file within each project, you can also consider specifying the coverage exclusion in a sonar-project.properties file at the root of the project. The advantage would be that once you get this setting working, you can simply drop a copy of the same file in every other project that uses the same exclusion settings. Of course the disadvantage is that it’s yet another file for each project team to maintain, and they may each prefer seeing this encapsulated within the build.gradle file if that’s what they’re accustomed to.