·
Go to Visual Studio ->
tools->Extension manager and search for VSSonarExtension
online. Download and Install the plugin. This will integrate the Sonar with
Visual Studio.
RUN SONAR ANALYSIS
Create Sonar-Project.properties file
Create a sonar-project.Properties File in the folder where your project solution file is located.
Each Solution will need to have it's own sonar-project.properties file. This file will need to exist in the folder from which you execute the sonar-runner. To make this easy, I would suggest putting the file in the same folder as your .sln file.
The file will have a few sections, which I will describe here. Note that some of these, I believe, can be put in the sonar-runner.properties file if you have the same settings for each project.
Important Note: Any folder names in the config file will need to either escape the backslash with another backslash (\\) or use a forward slash (/). I've chosen the latter.
Project Identification:
This section will provide the project key used by the Sonar server to group analysis results over time, as well as provide a useful name in the UI, etc. This should be unique across projects. The project version can be used to track different branches, etc.
# Project identification
sonar.projectKey=Jwright:DemoApp
sonar.projectVersion=trunk
sonar.projectName=DemoApplication
Then, describe the source code layout. The "sources" field points to the top-level folder where the source code exists. If you're .sln and .csproj files have relative paths internally, then this should be the top-level folder. Assuming you don't have any strange layouts, this will likely be the same folder as your .sln file (which is likely where your .properties file exists), so can just be ".". Additionally, you need to denote that the language is C# using the "cs" value.
# Info required for Sonar
sonar.sources=.
sonar.language=cs
C#-specific settings:
Here, you'll need to provide information about where the .sln file is located and where key libraries are located, and which version of .Net you're using.
#Core C# Settings
sonar.dotnet.visualstudio.solution.file=DemoApp.sln
sonar.silverlight.4.mscorlib.location=
C:/Program Files (x86)/Reference Assemblies/Microsoft/Framework/Silverlight/v4.0
sonar.dotnet.excludeGeneratedCode=true
sonar.dotnet.4.0.sdk.directory=C:/WIndows/Microsoft.NET/Framework/v4.0.30319
sonar.dotnet.version=4.0
Plug-in Specific Sections:
For each plugin, there is a "mode" setting. If blank, then the plugin will run. If you want to skip/not run a plugin, set the mode to "skip".
For Gallio, you can stipulate if you want to use OpenCover (free) or NCover (not free). You can also stipulate the runner mode. I had trouble using anything other than "Local". You will also need to stipulate the naming pattern (regular expressions, I believe) for the Visual Studio projects that include unit tests. You can have multiple patterns, seperated by semicolons.
#Gendarme
sonar.gendarme.mode=
# Gallio / Unit Tests
sonar.gallio.mode=
sonar.gallio.coverage.tool=OpenCover
sonar.gallio.runner=Local
sonar.dotnet.visualstudio.testProjectPattern=*UnitTest*;Testing*
sonar.opencover.installDirectory=C:/Program Files (x86)/OpenCover/
# FXCop
sonar.fxcop.mode=skip
#StyleCop
sonar.stylecop.mode=
#NDeps
sonar.ndeps.mode=
The tools for which the mode property is blank, all those tools will run if we command to run sonar analysis on that project.
Run analysis
So, now we are done with our setup and ready to run the analysis.
Go to CMD -> Go to project solution file folder and run sonar-runner. The sonar will execute and you can see the results on http://localhost:9000. We can also run the analysis by going to Sonar tab in Visual Studio.