Blog Detail

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

Analyze your project with SonarQube

In my previous two blogs I have explained the benefit of SonarQube and how to install it on Ubuntu machine but this information is incomplete unless you know how to use it. My previous blogs on SonarQube:

Why SonarQube is important for IT projects ?
SonarQube installation on Ubuntu

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

/var/www/html/blog

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

Install sonar-scanner on ubuntu:
To install sonar-scanner we have to follow these steps:
1) Download the sonar-scanner file. https://sonarsource.bintray.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 export command to setup 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 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 login. 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 'Administration' link on the top menu to open the administration page.

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

4) Click on 'Create Project' button on 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 to your project directory using following command on 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 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 the error in scanner execution. For fixing this issue we can add following line in our project's sonar-project.properties file:

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


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

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

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

This way we can setup a project on SonarQube and can scan the project directory to get the 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.

About Author

Anurag Srivastava

Author | Blogger | Tech Lead | Data Scientist | Innovator |

View Profile

Comments (0)

Leave a comment

Related Blogs

Load csv Data into Elasticsearch

Oct 15, 2018, 6:25:43 PM | Anurag Srivastava

htop: An Interactive Process Viewer

Oct 13, 2018, 8:49:59 PM | Anurag Srivastava

Configure Logstash to push MySQL data into Elasticsearch

Jul 7, 2018, 8:51:30 AM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

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

Configure Jenkins for Automated Code Deployment

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

Configure SonarQube Scanner with Jenkins

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

Build and deploy Angular code using Python

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

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Install Kafka on Ubuntu

Jul 12, 2018, 7:40:51 PM | Anurag Srivastava

Top Blogs

Build and deploy Angular code using Python

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

Configure SonarQube Scanner with Jenkins

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

Configure Logstash to push MySQL data into Elasticsearch

Jul 7, 2018, 8:51:30 AM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

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

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

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

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

Install Jenkins on Ubuntu

May 26, 2018, 6:42:02 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Install Kafka on Ubuntu

Jul 12, 2018, 7:40:51 PM | Anurag Srivastava