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:
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:
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-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 the export command to set up the path:
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:
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:
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
# 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 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.
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
Dec 28, 2020, 5:32:31 AM
Thanks for the comment Ravi, I have modified the URL as it was modified
Leave a comment
Sep 23, 2020, 2:38:00 PM | Anurag Srivastava
Sep 6, 2020, 5:20:33 PM | Anurag Srivastava
Jan 16, 2020, 11:43:29 AM | Anurag Srivastava
Jan 14, 2020, 10:22:34 AM | Anurag Srivastava
Jan 7, 2020, 7:15:34 PM | Anurag Srivastava
Jan 6, 2020, 7:30:13 PM | Anurag Srivastava
Jun 21, 2018, 4:58:11 AM | Anurag Srivastava
Jul 16, 2018, 5:00:02 PM | Anurag Srivastava
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Feb 9, 2019, 12:06:18 PM | Anurag Srivastava
Jun 13, 2018, 3:44:01 PM | Anurag Srivastava
Jun 26, 2018, 4:50:18 PM | Anurag Srivastava