Posted in Tools

Up and Running : SonarQube

SonarQube is an open source quality management platform, dedicated to continuously analyze and measure technical quality of a project. SonarQube, with the help of SonarQube scanner, will analyze the project and produces the detailed report. It allows analysis in the following methods:

  • SonarQube Scanner for MSBuild: Launch analysis of .Net projects
  • SonarQube Scanner for Maven: Launch analysis from Maven with minimal configuration
  • SonarQube Scanner for Gradle: Launch Gradle analysis
  • SonarQube Scanner for Ant: Launch analysis from Ant
  • SonarQube Scanner For Jenkins: Launch analysis from Jenkins
  • SonarQube Scanner: Launch analysis from the command line when none of the other analyzers is appropriate

Step 1: Download SonarQube server

You can download and unzip the SonarQube distribution to the desired location.

Step 2: Start SonarQube server

SonarQube distribution has bin folder in it that contains folder for windows, linux and mac operating systems. Open the right folder that matches your operating system with the right bit – x86 or x64. Execute StartSonar.bat file (for linux and mac, sonar.sh). It should launch JVM (Java Virtual Machine). Leave the command prompt open.

Note: It should become idle with these lines:

jvm 1 | 2017.07.25 15:09:54 INFO app[][o.s.a.SchedulerImpl] Process[ce] is up
jvm 1 | 2017.07.25 15:09:54 INFO app[][o.s.a.SchedulerImpl] SonarQube is up

If you are not seeing this, probably you are missing any of these requirements. Fix those before proceeding to next step.

Step 3: Download SonarQube Scanner

Out of all available methods for analyzing the code, we make use of SonarQube Scanner to launch analysis from the command line. You can download (select the appropriate file based on your operating system and its bit configuration) and unzip the SonarQube Scanner to the desired location (better if it is unzipped to the same location where you unzipped SonarQube server in the first step).

Step 4: Change directory to project location

Open a command prompt and change the location to your project’s directory:

cd my-project/directory

Step 5: Add properties file

For the SonarQube to analyze the project, we need to define few properties to let it know about the project and its location. Create a file named sonar-project.properties and add following details:

sonar.projectKey=<project_identifier>
sonar.projectName=<project name>
sonar.projectVersion=<project version>

sonar.sources=<directories>
  • projectKey needs the unique identifier for the project.
  • projectName can be given in the readable form.
  • projectVersion holds the version of the project like 1.0, 1.1,… This is used for version history in SonarQube report.
  • sources mention the directories which SonarQube would analyze. To analyze more than one directory, separate directories using commas.

Step 6: Analyze the project

Run the sonar-scanner.bat (for linux and mac sonar-scanner) file from the command prompt exactly from the project location set in fourth step.

Note: SonarQube Scanner will automatically download plugins for all languages once. This should take some time. From next analysis, it just loads appropriate plugin.

Now, analysis will start. Depending on the number of files and its size, time taken to complete the analysis differs. You are done, when you see something like this:

INFO: Task total time: 20.469 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 2:14.900s
INFO: Final Memory: 49M/196M
INFO: ------------------------------------------------------------------------

Step 7: See the results

You can see the results of the analyzes at http://localhost:9000/

Under the projects tab, you will find the results with the project name that you specified in the sonar-project.properties file. Or you can directly land by clicking this.

I see this for one of my project:

Hiring Portal Modified.png

Share your results!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s