Sunday, January 25, 2015

Install & Configure Sonar with TFS 2012


This article demonstrates how to configure Sonar with TFS 2012 in Microsoft Windows Server 2008R2 environment with Microsoft SQL Server 2008R2 as the sonar database.

I've skipped the TFS installation and configuration part. You must have a configured TFS 2012 installation, before configuring sonar.


SonarQube Platform Overview
The SonarQube platform is made of 3 components:

1. A database to store:
The configuration of the SonarQube instance (security, plugins settings, etc.)
The quality snapshots of projects, views, etc.
2. A Web Server for users to browse quality snapshots and configure the SonarQube instance
3. One or more Analysers to analyze projects


Configure the Database

1. Open up the SQL Management Studio and create a new login as below.
login name : sonar
password : sonar


2. Create a new database : Database name : "sonar"
3. Create the database user and tick all the permissions


** points noted from sonar documentation:
Note that collation must be case-sensitive (CS) and accent-sensitive (AS).
Install and Configure the Web Server

1. Download and install the latest SonarQube version from here.
2. Create a folder on a disk partition that has a reasonable amount of free space and rename it as "Sonar". Inside of that, create another and rename it to "SonarQube".
3. Extract the downloaded SonarQube binaries inside the "SonarQube" folder.
4. Edit the below file and apply the below configurations.

X:\Sonar\SonarQube\conf\sonar.properties



- Apply the username and the password that was given when you create the sql user.
- Alter the Ms-SQL configuration string as below

#----- Microsoft SQLServer
# The Jtds open source driver is available in extensions/jdbc-driver/mssql. More details on http://jtds.sourceforge.net
# The validation query is optional.
sonar.jdbc.url: jdbc:jtds:sqlserver://<database server>;databaseName=SONAR;instanceName=<database instance>;selectMethod=cursor;
sonar.jdbc.driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
sonar.jdbc.validationQuery: select 1
sonar.jdbc.dialect= mssql

5. Now move to the below folder and create a new folder called "mssql" if it does not already exist.
6. Download Microsoft SQL JDBC Driver and extract the sqljdbc4.jar file to mssql folder.
7. Execute the StartSonar.bat from either x:\Sonar\SonarQube\bin\Windows-x64-64 or Windows-x64-32 depending on your configuration.
8. Then open up a command prompt as run as Administrator and execute InstallNTService.bat located in the folder used in step 7.



9. Restart your PC, and then navigate to http://localhost:9000 (from inside the installed server)
Default Admin user name/password : Admin/Admin


10. Now download and extract the SonaQubeRunner from here and extract to the below folder:
x:\Sonar\SonarQube\sonar-runner-2.4\

11. Replace Ms-SQL section (with what you have used in the previous step)of the configuration file located in the conf folder.


12. Now create a system variable called "SONAR_RUNNER_HOME" and point it to the Sonar Runner folder.

13. Add Sonar-runner bin folder to the "Path" Environment Variable


14. Open-up a new console and type "sonar-runner", if perform some checks.. then you are still on the road.
15. Create a file "sonar-project.properties" add it to the root folder of your Visual Studio solution, and add it to TFS.


16. Add these lines to that file.


17. Another 2 installations are needed to be done.
C# Plugin | Analysis Bootstrapper for Visual Studio Projects Plugin

Copy both downloaded plugins to x:\Sonar\Sonar-runner-2.4\extensions\plugin\ and then restart the SonarQube server.

18. To analyze your C# project. Open up a common prompt and navigate to the folder where your sonar-runner property file is located in. Type sonar-runner and once the analysis is finished, open up the Sonar portal, and you will see a new project is listed.





19. Now the last step, include a post build script in your main project.

20. Final step, create a CI build definition and trigger it, and when the build success, check the  http://<your sonar installed server>:9000, and you will see the project. You can create custom dashboards with inbuilt widgets to get more insight into your code base.

A useful Visual Studio extension called VSSonarExtension can be used to analyse code against the sonar default rules. You can read more about the extension here.

Most of the stuff that I've summarized here were extracted from the below sites :
http://docs.sonarqube.org/display/SONAR/Installing
http://www.sezok.de/sonar/sonar.html