Monday, October 15, 2012

Install and configure Sonar on Windows 7 for .NET projects

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
2. Considerations
  • 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.
3. Installation
  1. Make sure to have Java JDK installed. Its default path for 32 bit version is C:\Program Files (x86)\Java\jdk1.6.0_29
  2. Unzip sonar-3.2.zip file. I used this path C:\Sonar
  3. Unzip sonar-runner-2.0.zip file. I used the Sonar's folder C:\Sonar\Sonar-runner-2.0
  4. Unzip the CSharpPluginsEcosystem-1.4.zip file into your Sonar’s plugins folder C:\Sonar\extensions\plugins
  5. Install FxCop 1.36. Its default installation folder is C:\Program Files (x86)\Microsoft FxCop 1.36
  6. Install Gallio. Its default installation folder is C:\Program Files\Gallio
  7. Install OpenCover to gather coverage statistics for your app. Its default installation folder is C:\Program Files (x86)\OpenCover
4. Configuration

Note: .properties files can be edited by any text editor like Notepad or Notepad++. I prefer to use Notepad++, it helps to read these files better.
  1. Edit Sonar configuration file (sonar.properties).  Path C:\Sonar\conf
    Remove
    the # sign of the following line and assign port number. I used port 9090 (view image).

  2. 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).
  3. 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

    Note
    : Notice that I’m using the same paths previously highlighted above.
  4. 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
  1. Find 32 bit version of Sonar.  Path C:\Sonar\bin\windows-x86-32and start Sonar 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 InstallNTService and then StartNTService.  If it doesn’t work just configure the service manually by changing the default user to Network Service.
  2. My Sonar's page is http://localhost:9090 as configured previously in the sonar.properties file.
  3. 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.
  4. View dashboard with Sonar Code Analysis project output.  Click on project name “Job Seeker Web” which was previously assigned in the sonar-project.properties file on line sonar.projectName=Job Seeker Web
  5. Login to Sonar by clicking Log in link on the top right of the page. The default user and password is admin as left in the sonar.properties file.  Once loged in you have other options to configured and extend.
6. Summary
We have successfully installed and configured Sonar and Sonar-Runner on Windows 7 OS for C# solution with 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.