7

Currently I have an instance of SonarQube 5.1.2 with C# plugin and MSBuild runner in order to analyze a 1.200.000 LOC project. I intend to reduce the classes that are analyzed, I created a sonar.properties file with the line

sonar.exclusions=**/Databases/**/*.*

but after reading the log from the analysis, files inside the Databases folder were analyzed. following the instructions from Eric Starr, I set this simple exclusion rule in the call of the runner:

"C:\sonarqube-5.1.2\bin\MSBuild.SonarQube.Runner.exe" begin /k:MyProject /n:MyProject /v:2 /d:sonar.exclusions="file:C:\codesource\Databases/**/*.*" /d:sonar.scm.provider=tfvc /d:sonar.tfvc.username=*************  /d:sonar.tfvc.password.secured={aes}*************************** "/d:sonar.cs.vscoveragexml.reportsPaths=C:\codesource\CodeCoverage\Results.coveragexml"

I found that the runner creates a sonar-project.properties file, and it contains a lot of files located in the databases folder:

BC78C8C4-8ECD-47CB-9781-F621AE109FE4.sonar.projectName=myDatabase
BC78C8C4-8ECD-47CB-9781-F621AE109FE4.sonar.projectBaseDir=BC78C8C4-8ECD-47CB-9781-F621AE109FE4.sonar.projectName=myDatabase
BC78C8C4-8ECD-47CB-9781-F621AE109FE4.sonar.projectBaseDir=C:\\codesource\\Databases\\myDatabase
BC78C8C4-8ECD-47CB-9781-F621AE109FE4.sonar.sources=\
C:\\codesource\\Databases\\myDatabase\\Scripts\\PreDeployment\\PATCH_20150527_01.sql,\
C:\\codesource\\Databases\\myDatabase\\Scripts\\PreDeployment\\ROCOMMON.DBVERSION.sql,\
,\.....

as I understood, there should be no files in the databases folder. Am I wrong?

XtianGIS
  • 967
  • 16
  • 39
  • Hi @XtianGIS if the answer below has solved your question please consider [accepting it](http://stackoverflow.com/a/35722039/1132448) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – Foxocube Apr 11 '17 at 15:16

1 Answers1

14

You are using the SonarQube Scanner for MSBuild which is very different from the regular SonarQube Scanner used for all other languages.

The sonar.exclude line that you are trying to use would only work if you would use the regular SonarQube scanner, because that takes in the Sonar-project.properties file. The SonarQube Scanner for MSBuild only has a SonarQube.Analysis.Xml file that contains project-related settings that you can tweak.

You can use couple of overwriting strategies for the SonarQube.Analysis.Xml file:

  • A project-specific property defined in the MSBuild *.*proj file (corresponding to a SonarQube module) can override:
  • A property defined in the command line (/d:propertyName=value) has which can override:
  • A property defined in the SonarQube.Analysis.xml configuration file
  • A property defined in the SonarQube User Interface at project level which can override everything
  • A property defined in the SonarQube User Interface at global level which can't override anything

To exclude specific folders or extensions from your Solution:

You need to add the excludes into each individual projects' .csproj file. Here's the syntax which you should use within the main root node, called <Project...> and into one of the targets, preferably <Target Name="BeforeBuild">. Hope the syntax below is self-explanetory enough, but in case it isn't, please leave a comment under this answer and I'll update it right away.

<Target Name="BeforeBuild">
    <ItemGroup>
          <SonarQubeSetting Include="sonar.exclusions">
              <Value>**/Databases/**/*</Value>
          </SonarQubeSetting>
      </ItemGroup>
  </Target>

Hope it helps!

Source

anthonymonori
  • 1,754
  • 14
  • 36
  • I made an upgrade to SonarQube 5.3, configure the exclusions in the administration section, as well, I set the exclusions in the command line of sonarqube. when runner is executed, it generates it own SonarQube.Analysis.xml file with the exclusions patterns defined in both places. None is applied :( – XtianGIS Mar 01 '16 at 18:46
  • @XtianGIS Yes, that is why you need to make the changes in the .csproj file because it should generate the SonarQube.Analysis.xml file based on these too. – anthonymonori Mar 02 '16 at 12:41
  • I suppose if the SonarQube.Analysis.xml contains the exclusions patterns it should work. – XtianGIS Mar 02 '16 at 19:23
  • Forget about the .xml file. Have you tried entering the ignores into the .csproj file? – anthonymonori Mar 04 '16 at 08:31
  • @XtianGIS Hi there, I have updated my answer, checked it out personally and it seems to be working fine for folder exclusions. You might want to play around with your path as I don't have an overview of your folder structure, but putting the above lines of code into the BeforeBuild target seems to be working. Please check it out for yourself and do mark it as an answer if it is working! Thanks you :) – anthonymonori Mar 10 '16 at 09:07
  • @anthonymonori Have you tried with multiple exclusions patterns? How could it be achieved? I'm going to try with comma (,) separated values. – Jesus Angulo Jun 14 '16 at 17:45
  • 4
    @JesusAngulo Yes, I achieved that by using commas inside the tag, like this: ```**/Databases/**/*,**/Properties/**/*.js,**/App_Config/*``` Hope this helps! – anthonymonori Jun 18 '16 at 15:25
  • 2
    In case it is useful to someone, you don't actually need it inside a target you can just have the itemgroup in your proj file. – David Martin Nov 28 '16 at 16:54
  • 2
    @anthonymonori reply it's not true in my case. I can use /d:sonar.exclusions without creating a xml file. In this page it says that both ways work: https://github.com/SonarSource-VisualStudio/sonar-.net-documentation/blob/master/doc/appendix-2.md – bdovaz Dec 08 '16 at 13:26
  • Do I need to run all three commands again for this change to reflect? It still doesn't exclude the files. – Akhil Nambiar Apr 07 '17 at 10:47