Setup Sonar in local step by step

Sonar Setup
SonarQube is the central place to manage code quality, offering visual reporting on and across projects and enabling to replay the past to follow metrics evolution.
In this post, we will see how to set up Sonar in local machine step by step (for .NET). So, let’s start.

  1. Create a folder somewhere. In my case, it is C:\sonar.
  2. Download SonarQuebe from here.
    sonardownload
    Sonar Download

    Extract the zip file and copy sonarquebe folder from it to C:\sonar.
  3. Rename it to sonarqube for simplicity.
  4. So, in my case, it is C:\sonar\sonarqube. This has sonarquebe folders like bin and others.
  5. Download Sonar CSharp plugin from here.
    sonarcsharp
    Sonar CSharp Plugin
  6. Copy the plugin jar file in C:\sonar\sonarqube\extensions\plugins.
  7. Download JRE from here. In my case, I have windows 64 bit. So, I downloaded Windows x64 Offline version.
  8. Install it. Note down the path of Directory where you have installed the Java JRE. In my case, it is C:\Program Files\Java\jre1.8.0_111.
  9. Set system environment variable JAVA_HOME with above path as value. Click here to see how to set an environment variable.
  10. Update system PATH variable with ;%JAVA_HOME%\bin. Click here to see how to update the PATH variable.
  11. Download Sonar Runner from here.
  12. Unzip the file and rename the inside folder to sonar-runner for simplicity. Copy the sonar-runner folder to C:\Sonar folder. So, in my case, it will be C:\sonar\sonar-runner which has a bin folder.
  13. Set system environment variable SONAR_RUNNER_HOME to C:\sonar\sonar-runner.
  14. Update system PATH variable with ;%SONAR_RUNNER_HOME%\bin. NOTE: Values are separated by a semi-colon (;).
  15. Go to C:\sonar\sonar-runner\conf. Open sonar-runner.properties in notepad. Uncomment below the line by removing hash.
    	sonar.sourceEncoding=UTF-8
  16. Go to C:\sonar\sonarqube\conf. Open wrapper.conf in notepad. Update wrapper.java.command as below.
    	wrapper.java.command=%JAVA_HOME%\bin\java
  17. Now We will create sonar project properties files for each project. This file will have information regarding the project source code location. So, let’s create a folder somewhere. E.g. C:\TestSonar. Go inside the new folder. Right-click and create an empty file named sonar-project.properties and provide following content.
    	sonar.projectKey=TestProject1
    	sonar.projectVersion=1.0
    	sonar.projectName=TestProject1
    
    	sonar.sources=E:/sandeep/websites/TestProject1
    	sonar.language=cs
    
    	sonar.projectBaseDir=E:/sandeep/websites/TestProject1
    	sonar.dotnet.visualstudio.solution.file=TestProject1.sln
    	sonar.dotnet.4.0.sdk.directory=C:/WIndows/Microsoft.NET/Framework/v4.0.30319
    	sonar.dotnet.version=4.0
    	sonar.working.directory=C:/TestSonar/sonarworkingdir

    We have provided sonarworkingdir directory as working directory. So, create sonarworkingdir folder in TestSonar folder. Basically, if you don’t provide this property then .sonar folder will be created within the project folder given in sonar.projectBaseDir.

    Note: Do provide forward slash in path.

  18. Go to C:\sonar\sonarqube\bin\windows-x86-64 and run StartSonar.bat with admin. Wait for some time till it is up and running.
    startsonar
    StartSonar.Bat
  19. Go to C:\TestSonar where you have created the sonar-project.properties file. Run cmd as an admin here and execute the sonar-runner command.
  20. You can view the Sonar dashboard by localhost:9000 in the browser.

FAQ:

  1. How to setup Sonar for multiple projects?
    sonarmultipleprojects
    Sonar Multiple Projects

    I would suggest creating new folders named after each project. Create sonar project properties files for each project in their respective folders which will have project specific information. This helpful when you have multiple projects source codes lying in different drives or folders. Then write a batch script that will start from TestSonar folder and go inside of TestProject folders and execute sonar-runner one by one.

    In case you have a parent folder where multiple projects folders are residing then you can refer this link.
  2. How to move “.sonar” folder created in Project folder?
    Use sonar.working.directory in sonar project properties file as shown in above example. Beware: the specified folder is deleted before each analysis. I did not see any side effects of this deletion so far.
  3. What is the difference between sonarquebe and sonar-runner?
    SonarQube (formerly just “Sonar”) is a server-based system. Of course, you can install it on your local machine (the hardware requirements are minimal). But it is a central server with a database. Analyses are performed by some Sonar “client” software, which could be the sonar-runner, the sonar ant task, the sonar Eclipse plugin etc. The analysis results can be automatically uploaded to the server, where they can be accessed via the sonar Web application.(Reference)
  4. How to resolve the Wrapper stopped error?
    Run task manager as admin, and end all java.exe tasks.
    Or run cmd as admin and execute below command.
    taskkill.exe /F /IM java.exe
  5. How to resolve: ERROR: Caused by: Start pointer [line=1, lineOffset=0] should be before end pointer [line=1, lineOffset=0].
    sonarfailure_startpointer
    Sonar Runner Failure

    It has a problem with C# plugin version. (Reference)
    Go to C:\sonar\sonarqube\extensions\plugins.
    Delete sonar-csharp-plugin-5.5.0.479.jar if it is there.
    Click here to download sonar-csharp-plugin-5.3.2-RC1.jar. (Reference)
    Copy sonar-csharp-plugin-5.3.2-RC1.jar to the plugins folder.
  6. Others properties in Sonar project property file
    Click here to read more about sonar properties file.

Hope this helps. Please do provide your valuable feedback in comments below. It will help me improve the blog content.

Special Thanks to my colleagues – Praveen Rekhapalli and Akshatha Shetty.