Analyze your project with SonarQube


preview imageDevOps
by Anurag Srivastava,Jun 2, 2018, 10:49:54 AM | 4 minutes |

In my previous two blogs, I have explained the benefit of SonarQube and how to install it on an Ubuntu machine but this information is incomplete unless you know how to use it. If you want to know the Jenkins and SonarQube installation process and configuration then refer to my previous blog:

Why SonarQube is important for IT projects?  
Configure Jenkins for Automated Code Deployment 
SonarQube installation on Ubuntu 
Configure SonarQube Scanner with Jenkins 
Install Jenkins on Ubuntu  

In this blog, I am going to explain the complete process to set up a project and scan it using SonarQube. Let say I have a project for a blog and it is located at:

/var/www/html/blog

We have installed the SonarQube server but to scan a project we also need the sonar-scanner so now we need to install the sonar-scanner.

Install sonar-scanner on ubuntu:
To install a sonar-scanner we have to follow these steps:
1) Download the sonar-scanner file. https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.2.0.1227-linux.zip
2) Expand the downloaded file into /opt/sonar/ directory.
3) Open the sonar-scanner.properties file:

vim /opt/sonar/sonar-scanner-3.1.0.1141-linux/conf/sonar-scanner.properties

4) Set the SonarQube server location:

#----- Default SonarQube server
sonar.host.url=http://localhost:9000

5) Now add the /opt/sonar/sonar-scanner-3.1.0.1141-linux/bin directory to your path. We can also use the export command to set up the path:

export PATH=$PATH:/opt/sonar/sonar-scanner-3.1.0.1141-linux/bin


Project setup for SonarQube:

Now I need to setup SonarQube to check the code quality of the blog project so that I can fix the possible code issues. So for SonarQube setup with blog project, I am going to explain the setup in a step by step way so that you can easily understand the process:

1) Open the SonarQube application on a browser and log in. For opening the software we need to type the following URL:

http://localhost:9000

If you have not installed SonarQube on your local machine then type the URL of the SonarQube server.

2) Click on the 'Administration' link on the top menu to open the administration page.

3) Click on the 'Projects' link under administration and from the drop-down click on the 'Management' link. This will open the projects management page. On this page, we can see the project listing.

4) Click on the 'Create Project' button on the top-right part of the page.

5) This will open the popup for project creation where we need to enter the project name and key etc.

6) Go to your project directory using the following command on the terminal:

cd /var/www/html/blog/


7) Now create a file as 'sonar-project.properties'

sudo vim sonar-project.properties

8) In the file add the following code:

# must be unique in a given SonarQube instance
sonar.projectKey=blog_sonar
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=blog
sonar.projectVersion=1.0
# This property is optional if sonar.modules is set.
sonar.sources=.


9) Now run the following command to scan the project:

sonar-scanner


10) On successful execution we will get the success message.

11) Sometimes because of framework files the report size increase and creates issues during upload. This causes an error in scanner execution. For fixing this issue we can add the following line in our project's sonar-project.properties file:

sonar.exclusions=directory1/**, directory2/**, public/**


12) After the successful execution of the sonar-scanner we can go to the browser to check the SonarQube server.

13) Click on the 'Projects' link from the top menu, this will list the project listing page.

14) Now click on the 'blog'  project which we have created for this demo.

This way we can set up a project on SonarQube and can scan the project directory to get a complete insight. I will explain the report analysis part in my next blog. Hope this series will help you to understand the SonarQube configuration for any project.

In case of any query or doubt please leave a comment.


Comments (4)

  • user image
    Ravi Geetha Vandana
    Mar 27, 2019, 12:32:53 PM

    while downloading the sonar scanner using the link you provided it is not working

  • user image
    Ravi Geetha Vandana
    Mar 28, 2019, 12:42:49 PM

    while creating sonar-project2.properties it is showing [ Error writing sonar-project2.properties: Permission denied ]

  • user image
    Ravi Geetha Vandana
    Mar 30, 2019, 1:16:34 PM

    How To Generate PDF Report Of Code Analysis In SonarQube for sonar7.6 version

  • user image
    Anurag Srivastava
    Dec 28, 2020, 5:32:31 AM

    Thanks for the comment Ravi, I have modified the URL as it was modified

Leave a comment

Related Blogs

preview thumbnail
Introduction to Logstash

Dec 20, 2019, 11:38:31 AM | Anurag Srivastava

preview thumbnail
Importing MongoDB data into Elasticsearch

Mar 9, 2019, 8:20:38 AM | Anurag Srivastava

preview thumbnail
Importing MySQL data into Elasticsearch

Feb 9, 2019, 12:06:18 PM | Anurag Srivastava

preview thumbnail
Snapshot and Restore Elasticsearch Indices

Sep 16, 2019, 5:55:06 AM | Anurag Srivastava

preview thumbnail
Log analysis with Elastic stack

Jan 31, 2018, 6:11:29 AM | Anurag Srivastava

preview thumbnail
Creating Elasticsearch Cluster

Apr 6, 2019, 8:41:41 PM | Anurag Srivastava

Top Blogs

preview thumbnail
Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

preview thumbnail
Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

preview thumbnail
Importing MongoDB data into Elasticsearch

Mar 9, 2019, 8:20:38 AM | Anurag Srivastava

preview thumbnail
Importing MySQL data into Elasticsearch

Feb 9, 2019, 12:06:18 PM | Anurag Srivastava

preview thumbnail
Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

preview thumbnail
Deploying Angular code using Python script

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava