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. If you want to know the Jenkins and SonarQube installation process and configuration then refer to my previous blog:
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:
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-184.108.40.2067-linux.zip
2) Expand the downloaded file into /opt/sonar/ directory.
3) Open the sonar-scanner.properties file:
4) Set the SonarQube server location:
#----- Default SonarQube server
5) Now Add the /opt/sonar/sonar-scanner-220.127.116.111-linux/bin directory to your path. We can also use export command to setup the path:
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:
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:
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
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
# This property is optional if sonar.modules is set.
9) Now run the following command to scan the project:
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.
Author | Blogger | Tech Lead | Elastic Stack | Innovator |View Profile
Ravi Geetha Vandana
Mar 27, 2019, 12:32:53 PM
while downloading the sonar scanner using the link you provided it is not working
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 ]
Ravi Geetha Vandana
Mar 30, 2019, 1:16:34 PM
How To Generate PDF Report Of Code Analysis In SonarQube for sonar7.6 version
Leave a comment
Sep 16, 2019, 5:55:06 AM | Anurag Srivastava
Oct 13, 2018, 8:49:59 PM | Anurag Srivastava
Feb 9, 2019, 12:06:18 PM | Anurag Srivastava
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Apr 6, 2019, 8:41:41 PM | Anurag Srivastava
Apr 14, 2018, 1:18:05 PM | Anurag Srivastava
Jan 31, 2018, 6:11:29 AM | Anurag Srivastava
Feb 9, 2019, 6:34:22 PM | Anurag Srivastava
Jun 21, 2018, 4:58:11 AM | Anurag Srivastava
Jun 26, 2018, 4:50:18 PM | Anurag Srivastava
May 12, 2018, 4:47:07 PM | Anurag Srivastava
Jul 16, 2018, 5:00:02 PM | Anurag Srivastava
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
Jun 2, 2018, 10:49:54 AM | Anurag Srivastava
May 26, 2018, 6:42:02 PM | Anurag Srivastava
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava