It has been a very long time since I post anything. I apologize for this. I will definitely try to do this more often.
Recently, I have been managing and analyzing code quality metrics using Sonar with C# plugins for .NET projects. Sonar, as described on their website, is a quality management platform, dedicated to continuously analyze and measure technical quality from project portfolio to method. It is extensible and configurable for multiple programming languages. It provides a report friendly accessible web user interface to manage project analysis results that contain detailed information for code coverage, complexity, coupling, duplication, rules violations for coding standards, etc. It is definitely a good option to consider for code analysis management.
Even though it’s very easy to follow the steps explained on their site, I have prepared this detailed post on how to install and configure Sonar on Windows 7 OS. there are a few things to consider for minimizing the effort and prevent troubleshooting too much while installing and configuring Sonar. The main purpose of this post is to get hands on experience with Sonar and have it running in 10 minutes… of course after the pre-requisites are installedJ.
Let’s get down to business with my sample solution named JobSeekerWeb:1. Software used
- Windows 7 64 bits Service Pack 1
- Visual Studio 2010
- Java JDK 1.6.0_29 (32 bit)
- Sonar 3.2
- Sonar-Runner 2.0
- CSharp Plugins Ecosystem 1.4
- FxCop 1.36
- Gallio Bundle 3.4.14.0
- OpenCover 4.0.301 (which is optional and it’s used to get coverage statistics results).
- Run command prompt (cmd) as Administrator.
- Sonar uses port 9000 by default; it could be configure to use other available port. In my example, I configured port 9090 (shown after). Check port availability by running netstat –a in a command prompt.
- If you’re running 32 bit version of Java JDK, you must also run 32 bit version of Sonar as well (shown after). Check your current Java JDK version by running java –version in a command prompt.
- Make sure to have Java JDK installed. Its default path for 32 bit version is C:\Program Files (x86)\Java\jdk1.6.0_29
- Unzip sonar-3.2.zip file. I used this path C:\Sonar
- Unzip sonar-runner-2.0.zip file. I used the Sonar's folder C:\Sonar\Sonar-runner-2.0
- Unzip the CSharpPluginsEcosystem-1.4.zip file into your Sonar’s plugins folder C:\Sonar\extensions\plugins
- Install FxCop 1.36. Its default installation folder is C:\Program Files (x86)\Microsoft FxCop 1.36
- Install Gallio. Its default installation folder is C:\Program Files\Gallio
- Install OpenCover to gather coverage statistics for your app. Its default installation folder is C:\Program Files (x86)\OpenCover
Note: .propertiesfiles can be edited by any text editor like Notepad or Notepad++.I prefer to use Notepad++, it helps to read these files better.
- Edit sonar-runner configuration file (sonar-runner.properties). Path C:\Sonar\sonar-runner-2.0\conf
Remove the # sign of the following two lines and assign host and port configured in sonar.properties. The same for the encoding at the end (view image). - Add or Edit two new environment variables %JAVA_HOME% and %SONAR_RUNNER_HOME%. Also Edit %PATH% variable to add Sonar-Runner path (view image).
To open the environment variables dialog box:
Right click > Computer > click Properties > click Advanced system settings > tap Advance > click button Environment Variables - Create a file: sonar-project.properties and configure it (view image). Save this file in the same folder of your solution (.sln file). The path for my sample solution is C:\Workspaces\JobSeekerWeb
Text of above screen shot:
# Project identification
sonar.projectKey=JS:JobSeekerWeb
sonar.projectVersion=1.0
sonar.projectName=Job Seeker Web
# Info required for Sonar
sonar.sources=.
sonar.language=cs
#Core C# Settings
sonar.dotnet.visualstudio.solution.file=JobSeekerWeb.sln
sonar.dotnet.4.0.sdk.directory=C:/WIndows/Microsoft.NET/Framework/v4.0.30319
sonar.dotnet.version=4.0
sonar.donet.visualstudio.testProjectPattern=*UnitTests*;*test*;*Test*
sonar.fxcop.installDirectory=C:/Program Files (x86)/Microsoft FxCop 1.36
sonar.gallio.installDirectory=C:/Program Files/Gallio
sonar.opencover.installDirectory=C:/Program Files (x86)/OpenCover
sonar.gallio.coverage.tool=OpenCover
sonar.gallio.runner=IsolatedProcess
sonar.gallio.timeoutMinutes=60
sonar.fxcop.assemblyDependencyDirectories=c:/Program Files (x86)/Microsoft ASP.NET/ASP.NET MVC 3/Assemblies,C:/Program Files (x86)/Microsoft Visual Studio 10.0/Common7/IDE/PublicAssemblies,c:/Program Files (x86)/Microsoft ASP.NET/ASP.NET Web Pages/v1.0/Assemblies
# Exclusions Settings
sonar.skippedModules=UnitTests
Note: Notice that I’m using the same paths previously highlighted above.
5. Running Sonar and Getting Analysis Reports
- Find 32 bit version of Sonar. Path C:\Sonar\bin\windows-x86-32and startSonar Service by opening a command prompt and running StartSonar (view image). You should get an output like the image below.
Note: It could also be install as a service by running InstallNTServiceand then StartNTService. If it doesn’t work just configure the service manually by changing the default user to Network Service. - Execute Sonar-Runner against the sample solution. Start up command prompt from where the solution (.sln) and sonar-project.properties files are located and start sonar-runner (view image). After it finishes refresh the Sonar 's page(previous step). You should see the project analysis result listed.
Note: It could take a couple of minutes. All depends on the size of the solution, number of projects, tests to run, etc.
6. Summary
We have successfully installed and configured Sonar and Sonar-Runner on Windows 7 OS for C# solutionwith its default configuration and embedded database.
Here are some features I would recommend you try:
1. Configure another database, such as Oracle, SQL Server, MySQL, etc. to store analysis output.
2. Access Sonar service remotely other than localhost. The sonar-runner needs to be local, but not the service.
3. Modify rules template for FxCop.
4. Configure Gallio differently than default.
5. Switch coverage engine, try NCover for example.
6. Activate Sonar’s support for integration tests feature.
7. Configure Sonar to be part of your deployment.
If you want more information and details about Sonar follow the link: http://docs.codehaus.org/display/SONAR/Documentation.
Thanks for reading, I really hope you find this helpful.