Pages

Tuesday, March 26, 2013

Running an Analysis Using Sonar Runner for a C# Project

Running an Analysis Using Sonar Runner for a C# Project

The previous post discussed on installing and configuring Sonar and Sonar Runner. This post will discuss on configuring C# Plugins and running an analysis for a C# project.

C# Plugins Ecosystem

Before we get into running the analysis, we need to install the C# Plugins Ecosystem. Download the CSharpPluginsEcosystem-2.0.zip. Unzip the contents of this file into ‘extensions/plugins’ folder inside the sonar installation folder.

Restart Sonar server after this is done. Please refer to documentation for details on each of these plugins. Out of these FxCop, Gallio plugins require installation. StyleCop and Gendarme are bundled along with their respective plugins.
In order to run the analysis you need to define the ‘sonar-project.properties’ file. An example is shown below. This is a not an exhaustive list. Just a minimal set to help you run the analysis.

#Project identification
sonar.projectKey=key.Serializer
sonar.projectName=Serializer
sonar.projectVersion=1.0
sources=.
sonar.language=cs
#sonar.fxcop.mode=skip
#sonar.gendarme.mode=skip
sonar.gallio.mode=skip
sonar.ndeps.mode=skip
sonar.sourceEncoding=UTF-8
#The following property is not required if the SLN file is in the same folder as the sonar-project.properties file
#sonar.dotnet.visualstudio.solution.file=Serializer.sln
#The following property is not required as "*.Tests" is its default value
#sonar.donet.visualstudio.testProjectPattern=*.Tests

The sonar.projectKey property is used to set a unique key for the project being analyzed, and is used to group analysis results over time. The sonar.projectName indicates the project name to be shown in UI and sonar.projectVersion properties can be used in case if you have more than one code branch. The sonar.language is set to cs to indicate C#.
The sources property will point to the top-level folder where your source code exists. This is the same folder as your .sln file, where your sonar-project.properties file will also exist, so you can this to ‘.’. Internally your .sln and .csproj files will have relative paths and things should just be fine!
The .mode property of a plugin when set to ‘skip’ will skip the analysis / rule set.
There are few more properties, please refer to documentation.

Running an Analysis

After defining the sonar-project.properties ,place it in the same folder as the .sln file. Open a new shell and change the directory to the point to the same folder as .sln file. Invoke the sonar-runner command as shown below and hit enter.

You should see the OS, Server and Working Directory related information listed down during the analysis. Once the analysis is complete you will see the details being shown in the sonar server webpage. The home page lists down all the projects analyzed using Sonar and provide lot of useful statistics. The shell will look like something shown below. In case if you get any exception you may need to use the -X command line argument to get additional details.

The screen below shown various metrics gathered during code analysis.

Thus you can run a sonar analysis for a C# project. The next post will discuss on integrating Visual Studio 2010 and Sonar, to obtain the result of static code analysis inside VS IDE.

No comments:

Post a Comment