Home » Automated Code Review » SonarQube for Automated Code Review

SonarQube for Automated Code Review

SonarQube is a fantastic piece of software which lets you code review in an automated way. As per wikipedia SonarQube (formerly Sonar[1]) is an open source platform for continuous inspection of code quality.

Its a really nice tool, a single developer can use it to perform automated code review on his code prior to code review by his lead. Additionally it can also be used to review the code with statistics saved in database by usine SonarQube Runner which will be demonstrated later in this tutorial as well.

First go to http://www.sonarqube.org/downloads/ & download the latest edition, in my case it was sonarqube-5.1.2.zip.

sonarqube01_2015091319:19:41_Selection

extract it to some location, I extracted it to /optsonarqube01_2015091319:23:05_linux-x86-64I will add sonar binary to the path so that I can run it from anywhere. Choose the right executable file, I selected executable file in bin/linux-x86-64 because I am running a Ubuntu OS 64 bit. Open the .bashrc file
sudo gedit /home/asad/.bashrc
sonarqube01_2015091319:29:28_asad@asad-HP-Pavilion-dv6-Notebook-PC: ~Add following 2 lines at the end
export SONARQUBE_HOME=/opt/sonarqube-5.1.2/
export PATH=$SONARQUBE_HOME/bin/linux-x86-64:$PATH
Save the contents and then start SonarQube by issuing command
sonar.sh start
sonarqube01_2015091319:31:51_asad@asad-HP-Pavilion-dv6-Notebook-PC: ~Verify that SonarQube started properly by accessing http://localhost:9000 sonarqube01_2015091319:33:29_SelectionLogin and browse different areas of the interface. More importantly go to Settings> System > Update Center and then Plugins, here you can install different plugins which will be helpful in code reviewsonarqube01_2015091319:37:01_SelectionNext you need to Install SonarQube in eclipse as well. Open Market and search for it then press Installsonarqube01_2015091320:34:31_Eclipse MarketplaceI have selected following components/features of the plugin which suits me for Java code reviewsonarqube01_2015091320:35:10_Eclipse MarketplacePress confirm and then eclipse will start installing the pluginsonarqube01_2015091320:36:13_Installing SoftwareA security warning will be shown, Press Ok to ignore itsonarqube01_2015091320:36:22_Security WarningIt will ask for the restart, then restart eclipse. If you don’t see views related to Sonar as shown below then you need to select themsonarqube01_2015091321:04:33_SelectionIn eclipse go to Window > Show View > Other ..

sonarqube01_2015091321:06:04_Java - P2LogicAPI-src-main-java-com-p2logicAPI-controller-AccountSettingsController.java - EclipseSelect following 2 views under SonarQubesonarqube01_2015091321:06:20_Show ViewNext Go to Window > Preferences > SonarQube and edit the server theresonarqube01_2015091321:50:22_PreferencesInsert the right credentials, in my case they were default admin/admin as I have not changed them in SonarQubesonarqube01_2015091321:51:15_Edit SonarQube Server

sonarqube01_2015091321:51:47_Edit SonarQube Server

sonarqube01_2015091321:52:39_Secure StorageNow you need to add SonarQube nature to your project. You can do so by Right clicking on project and selecting Configure > Associate with SonarQubesonarqube01_2015091322:00:15_Java - EclipseYou will be presented by following window. It means that you need to define a remote Project in SonarQube then this local project will be associated with that remote project

sonarqube01_2015091322:00:16_Java - EclipseLogin as Admin in SonarQube and Go to Settings > System > Provisioningsonarqube01_2015091322:00:17_Java - EclipseClick on Create linksonarqube01_2015091322:00:18- Mozilla FirefoxMention Key and Name. Click Create Project

sonarqube01_2015091322:00:19- Mozilla Firefox

sonarqube01_2015091322:00:20- Mozilla FirefoxNow type in the Name of the SonarQube Project (If it does not work then restart eclipse and try to associate it again)sonarqube01_2015091322:00:21- Mozilla FirefoxOnce Project is associated with SonarQube, you can Analyze the project as follows

sonarqube01_2015091322:02:15_Java - EclipseAfter Analysis Issues will be listedsonarqube01_2015091322:02:49_SelectionClicking on any issue will take you to the line of the file where this problem is reported.sonarqube01_2015091322:03:20_Java - hw1-3-src-main-java-com-tengen-Week1Homework3.java - EclipseNow you can fix these problems.

A better approach is to analyze your project with SonarRunner which dumps the statistics in the database. We will download SonarRunner and configure it to use MySql database, after analysis it will dump the results in database. We will configure our Sonar web interface to point to the MySql database so it can fetch results from there and present the user with these issues in a presentable view. First we will stop the Sonar and create the database and configure sonar to use our created database.
Stop Sonar by issuing command
sonar.sh stop
sonarqube01_2015091322:16:22_asad@asad-HP-Pavilion-dv6-Notebook-PC: ~

Next Create a database sonar_analysis_db in MySql sonarqube01_2015091322:17:36_Selection

sonarqube01_2015091322:18:08_SelectionCreate a user by command
GRANT ALL ON sonar_analysis_db.* TO 'sonar_user' IDENTIFIED BY 'sonar_userpassword' WITH GRANT OPTION;
sonarqube01_2015091322:21:21_SelectionNow modify SonarQube web-application to use this databasesonarqube01_2015091322:24:22_sonar.properties (-opt-sonarqube-5.1.2-conf) - geditEdit pointed out properties.

Next download the Sonar Runner by going to http://docs.sonarqube.org/display/SONAR/Installing+and+Configuring+SonarQube+Runner and download the latest version, in my case it is sonar-runner-dist-2.4.zip. I extracted it to /optsonarqube01_2015091322:31:38_Installing and Configuring SonarQube Runner - SonarQube - SonarQube - Mozilla FirefoxNext open the sonar-runner.properties file in conf folder and point it to the above created database so that Sonar webapp and Sonar-Runner both point to the same databasesonarqube01_2015091322:35:22_confEdit pointed out properties.

Save the file. Then again edit .bashrc to add SonarRunner to the path. Issue command

sudo gedit /home/asad/.bashrc
add following 2 lines
export SONAR_RUNNER_HOME=/opt/sonar-runner-2.4
export PATH=$SONAR_RUNNER_HOME/bin:$PATH
sonarqube01_2015091322:39:24_.bashrc (~) - gedit
Next we need to create a properties file in eclipse project. sonarqube01_2015091322:45:34_Java - EclipseSelect File under General Categorysonarqube01_2015091322:45:49_NewName it sonar.propertiessonarqube01_2015091322:46:43_SelectionEdit the contents like shown below
# required metadata
sonar.projectKey=hw1-3
sonar.projectName=My ProjectName
sonar.projectVersion=1.0
# Comma-separated list of library directories
#sonar.libraries=lib/*.jar
# comma-delimited list of paths to source directories (required)
sonar.sources=main/java
# comma-delimited list of paths to test source directories (optional)
#sonar.tests=testDir1,testDir2
# path to project binaries (optional)
#sonar.binaries=bin
sonar.language=java

sonarqube01_2015091322:59:44_Selection
Next we will run Sonar-Runner by moving into the directory of above created file and issuing command
sonar-runner -Dproject.settings=sonar.properties
sonarqube01_2015091323:07:02_asad@asad-HP-Pavilion-dv6-Notebook-PC: ~-workspace-java-hw1-3-srcIt will take a little time and finally it will show the status of the execution.sonarqube01_2015091323:07:32_asad@asad-HP-Pavilion-dv6-Notebook-PC: ~-workspace-java-hw1-3-srcNext check SonarQube by going to its Dashboard and you will see the project

sonarqube01_2015091323:08:02_SonarQube - Mozilla FirefoxClick on the Project will take you to the Issues pointed outsonarqube01_2015091323:09:06_SonarQube - My ProjectName - Mozilla FirefoxFurther you can install any plugin you need by login as admin and going to Settings > System > Update Center > Available Plugins

sonarqube01_2015091323:17:07_Selection sonarqube01_2015091323:17:27_Selection

sonarqube01_2015091323:18:30_Selection sonarqube01_2015091323:19:27_Selection

We are done here 🙂

18 thoughts on “SonarQube for Automated Code Review

  1. Hi there! I could have sworn I’ve been to this website before but after looking at a few of the posts I realized it’s new to me. Regardless, I’m certainly happy I found it and I’ll be book-marking it and checking back regularly!

    Like

  2. I really love your blog.. Pleasant colors & theme. Did you make this site yourself? Please reply back as I’m attempting to create my own personal site and would like to find out where you got this from or what the theme is called. Kudos!

    Like

  3. I would like to thank you for the efforts you’ve put in writing this site. I really hope to view the same high-grade blog posts by you in the future as well. In truth, your creative writing abilities has inspired me to get my very own website now 😉

    Like

  4. Right here is the perfect blog for anybody who wishes to understand this topic. You know so much its almost tough to argue with you (not that I actually will need to…HaHa). You definitely put a new spin on a topic that’s been written about for ages. Excellent stuff, just wonderful!

    Like

  5. Can I simply just say what a relief to find someone who truly knows what they are discussing on the web. You certainly realize how to bring a problem to light and make it important. More people should look at this and understand this side of the story. I was surprised you are not more popular given that you most certainly have the gift.

    Like

  6. Nice post. I learn something new and challenging on blogs I stumbleupon every day. It will always be helpful to read through content from other authors and use a little something from other websites.

    Like

  7. The next time I read a blog, Hopefully it doesn’t disappoint me just as much as this particular one. I mean, I know it was my choice to read, nonetheless I truly believed you’d have something useful to talk about. All I hear is a bunch of moaning about something that you could fix if you weren’t too busy seeking attention.

    Like

  8. Hi, I do think this is an excellent web site. I stumbledupon it 😉 I’m going to come back once again since i have bookmarked it. Money and freedom is the best way to change, may you be rich and continue to guide others.

    Like

Leave a comment