Go, Get, Run SonarQube

 

SonarQube

                             In the last article we introduced SonarQube. In this article we will setup the SonarQube and start analyzing the code. Follow the below steps to setup.

Step 1: Download the latest version of SonarQube. As of now the latest version is 5.0.1. After downloading, unzip it and the folder structure will be as shown below.

SonarQube Folder Structure

Step 2: SonarQube uses database to store the settings, rules, metrics, issues etc… It supports MSSQL Server, MySql, Oracle and PostgreSQL. In this article we are going to use MySql. We will create a database and assign the privileges to it.

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar123';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar123';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar123';

Step 3: Goto the “conf” folder (see Step 1) and configure the database details as part of sonar.properties file as shown below.

sonar.jdbc.username=sonar
sonar.jdbc.password=sonar123
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

Step 4: Goto the “bin” folder (see Step 1) and choose the right operating system folder to start the Sonar. I have chosen “windows-x86-64” and clicked on “StartSonar.bat”. First time it will take some time to start as it is going to create tables in the configured database and setting up the default data. The sonar server runs on 9000 port by default and the sonar host URL will be http://localhost:9000

Step 5: Download the SonarQube Runner to analyze the code, unzip it and  set the SONAR_RUNNER_HOME environment variable to the root folder of Sonar Runner.

Step 6: Set the PATH variable to SONAR_RUNNER_HOME/bin and ensure the JAVA_HOME set properly.

Step 7: Move to your projects root folder and create sonar.properties file and add the below properties.

sonar.projectKey=techDetectorArch
sonar.projectName=techDetectorArch
sonar.projectVersion=1.0
sonar.sources=src
sonar.host.url=http://localhost:9000
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar123
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

Step 8: Run the below command to start analyse the code from the projects root folder.

sonar-runner -Dproject.settings=sonar.properties

If it goes successful, you will see the below log.

21:05:17.039 INFO - ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/techDetectorArch
21:05:17.039 INFO - Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report.
21:05:17.055 INFO - Executing post-job class org.sonar.plugins.core.issue.notification.SendIssueNotificationsPostJob
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
Total time: 18.283s
Final Memory: 17M/716M
INFO: ------------------------------------------------------------------------

Step 8: Goto http: //localhost:9000 to see the sonar’s dashboard, where all the metrics will be available.

SonarQube Dashboard

 

That’s it. You are having Sonar up & running.

Advertisement

Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

Tagged with: , ,
Posted in Code Quality

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dzone.com
DZone

DZone MVB

Java Code Geeks
Java Code Geeks
OpenSourceForYou
%d bloggers like this: