tumblr counter

SonarQube Tutorial

SonarQube Tutorial

SonarQube Tutorial explains about how to install /configure SonarQube server for continuous integration and improving code quality.

SonarQube (previously known as Sonar) is an open source platform for Continuous Inspection of code quality. It is written in java and supported for 25+ languages such as Java, C/C++, C#, PHP, Flex, Groovy, JavaScript, Python, PL/SQL, COBOL, etc, it is also used for Android Development

It helps for various tasks and provide reports on duplicated code, coding standards, unit tests, code coverage, complex code, potential bugs, comments and design and architecture.

SonarQube is internally using PMD,Findbugs,CheckStyle etc. You can add additionally plugins according to your requirement

Setup / Configure Sonar

SonarQube Installation Steps:

You can follow below steps in order to installing Sonar Java Code Analysis Tool. Which will dramatically imporve code quality.

  1. Copy and unzip sonar.zip and sonar-runner.zip
  2. Install Sonar Server

  3. Start the Sonar server using the script available in SONAR_HOME\bin\windows-x86-32\StartSonar.bat (OS dependent)
  4. Sonar Server Started

  5. After staring the server you can browse to http://localhost:9000
  6. Sonar Server Running

    Now, You can see sonar server is started

Creating Project & Analysing Using Sonar

  1. Set a new environment variable as SONAR_RUNNER_HOME. And its value should be C:\sonar\sonar-runner-2.3 (unzipped path of sonar-runner.zip)

  2. For setting environment variable, you can do following steps

    1. From the Desktop, right click on My Computer and click Properties.
    2. Click Advanced System Settings link in the left column.
    3. In the System Properties window click the Environment Variables button.
    4. Inside System variable, click on New Button

    Set Sonar Home Variable

    Also append C:\sonar\sonar-runner-2.3\bin to Path.

    Set Sonar Path Variable

    Note

    1. In this tutorial, we are using h2 database which is default configured with SonarQube, You can also use any of these databases (mysql,plsql,oracle etc),
      For example, If you are using mysql, just execute the following sql script
    2. CREATE USER 'sonar' IDENTIFIED BY 'sonar';
      GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
      GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
      FLUSH PRIVILEGES;
      		
    3. Edit sonar.properties in  \conf\sonar.properties.

      just comment Connection url for h2 and uncomment Connection url for mysql

      sonar.jdbc.url:jdbc:h2:tcp://localhost:9092/sonar
      sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true

  3. Create a new Java Project and add below sonar-project.properties file into the root of the project folder (see below project strcuture)

  4. sonar-project.properties
    # Required metadata
    sonar.projectKey=java-sonar-runner-simple
    sonar.projectName=Simple Java project analyzed with the SonarQube Runner
    sonar.projectVersion=1.0
    
    # Comma-separated paths to directories with sources (required)
    sonar.sources=src
    
    # Language
    sonar.language=java
    
    # Encoding of the source files
    sonar.sourceEncoding=UTF-8
    

    Sonar Project Structure

  5. Using the terminal go the project home and run the command sonar-runner (Sonar Server must be started)
  6. After the success run browse the web  http://localhost:9000 where you can see the report related to the project

    Sonar Project Report


SonarQube Tutorial Setup/Install/Configure SonarQube Server Sonar Tutorial How To Improve Code Quality Using Sonar Server SonarQube Static Code Analysis SonarQube Code Review Tool SonarQube Code Coverage Tool SonarQube Source Code Analysis SonarQube Code Inspection

3 Comments on "SonarQube Tutorial"
13.11.2013 17:05:34 Nicole
Thanks for the article but i would like to ask how i set a new environment variable?

Thank you
14.11.2013 12:30:18 admin
@Nicole,

I have update the sonar tutorial in order to avoid confusion about setting new environment variable

04.03.2014 17:04:01 sravan
Hi Admin,

I am new to SONAR. I am able to perform a project analysis in SONAR through SONAR - Runner.
I would like to know how to use the WebService API of SONAR.

Could you please provide a tutorial to understand how to consume the WebService API.

Thank you.