Pages

Sunday, March 24, 2013

Installing and Configuring Sonar and Sonar Runner

What is Sonar?

Sonar is an open platform to manage code quality. It covers the 7 axes of code quality.

Sonar is a web-based application. Rules, alerts, thresholds, exclusions, settings… can be configured online. By leveraging its database, Sonar not only allows to combine metrics altogether but also to mix them with historical measures.
Sonar can be extended using various plugins. Covering new languages, adding rules engines, computing advanced metrics can be done through a powerful extension mechanism. More than 50 plugins are already available. It covers more than 20 programming languages including Java, C#, C++, PL/SQL, COBOL, etc.
The Sonar runner consumes and analyzes the source code via various plugins configured in the server.  This information get pushed to the Sonar database and the Sonar web server gets the analysis results from the database and populates the UI for reviewing them.

Prerequisites

Plugins like FxCop perform the analysis on the Compiled Intermediate Language / assemblies; hence you might need the latest working version of the code that compiles without any errors. You need the .NET framework installed in your machine. Also, you might need Windows 7 SDK as FxCop 10.0 is exclusively bundled along with this.

Java JDK

You read it right, you need Java JDK. Even though you are going to run the analysis for .NET project, you need this! Set the JAVA_HOME environment variable pointing to your JDK root folder. JAVA_HOME= C:\Program Files\Java\jre7.

Installing FxCop 10.0 without installing Windows SDK

As mentioned earlier FxCop 10.0 is exclusively available only with Windows SDK. You need to download and install Windows SDK from Microsoft Windows SDK for Windows 7 and .NET Framework 4 Version 7.1. Using elevated privileges execute FxCopSetup.exe from the ‘%ProgramFiles%\Microsoft SDKs\Windows\v7.1\Bin\FXCop’ folder. There is shortcut instead.
You need to download the ISO version of the Windows SDK. The idea is to extract the FxCop setup from this ISO. 7Zip  is an excellent utility for compressing files. It also helps us inspect contents of an ISO file. Extract the contents of the ISO into a folder and try to locate ‘Setup\WinSDKNetFxTools\cab1.cab’.
Now open it with Windows and copy out the file named, ‘WinSDK_FxCopSetup.exe_all_enu_1B2F0812_3E8B_426F_95DE_4655AE4DA6C6’. Just rename this to ‘WinSDK_FxCopSetup.exe’ and install. That’s it! For your convenience, I have this file in my ‘Dropbox’ folder here.

Installing Sonar

You can download Sonar from here. This page lists the various releases of Sonar and the version at the time of this writing was 3.5 (released on March 13, 2013).
Once you download this extract the contents to the folder of your choice. I had a folder for Sonar in my C Drive, and I installed Sonar 3.5 and Sonar runner in this folder. [More on Sonar Runner later in this post]
My local sonar installation path was ‘C:\Sonar\sonar-3.5’. Starting your Sonar server is pretty simple. Go to the \bin directory and you will find folders specific to operating systems. For example if you have Windows 7 Professional 64 bit operating system, you need to use batch files in ‘windows-x86-64’.

Running Sonar service using a console application as host

You can execute the StartSonar.bat with elevated privileges and this will launch a console host. There are other options as well; you can configure this as a windows service so that you do not need to worry about restarting your system. You can set the startup mode of the service to ‘Automatic’.

Once the service is up and running, you will see something as shown below.

In order to test if Sonar is up and running, visit the link http://localhost:9000, which will show the Sonar dashboard. In case if you get some error, keep refreshing, it takes some time. The page below shows one project with code analysis results. You can drill down and see more specific details for any project.

Running Sonar using a windows service as host

You will find two batch files for installing and starting Sonar as a windows service. These are named ‘InstallNTService.bat’ and ‘StartNTService.bat’ respectively. Remember to execute these with elevated permissions. During my initial run, installation went on without any issues. I faced a problem while starting the service.

After reading other blogs on Sonar, the service was trying to create a folder named Temp inside C:\Windows\system32\config\systemprofile\AppData\Local\ and it failed due to lack of permissions. Just follow the steps listed below. These are few steps which got the service working, if you find any root cause other than this or better solutions other than this, kindly post a comment.
  • Create the folder ‘Temp’ in the path shown above.
  • Go to folder properties and set the security rights for the user account. You need to provide full control to the user account.


  • Run the batch files listed below with elevated permissions, from the SONAR installation folder:
o    bin\windows-x86-64\InstallNTService.bat
o    bin\windows-x86-64\StartNTService.bat
·         Ensure that the service is started and the startup type is set to automatic mode:

Installing and Configuring Sonar Runner

Download the Sonar Runner from here. You need to unzip the contents into a folder of your choice. In my case, sonar runner and share the same parent folder [C:\SONAR\]. At the time of this writing sonar runner version was 2.0. [C:\SONAR\sonar-runner-2.0]. Please refer to the image below.

You can refer to the ‘Installation’ section in this link. For default installation using the built in database, the minimal steps are listed below.
  • You need add the following system variable under environment variables.




  • You need to add sonar-runner-2.0/bin directory to the ‘Path’ variable in system variables under environment variables.

In order to verify if the configuration, you can open a new shell and execute the command shown below.
sonar-runner -h
The output will be something like:

The next blog post will discuss about setting up the C# plugins for Sonar.

No comments:

Post a Comment