Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Quickly Get Started with Sonar

DZone 's Guide to

How to Quickly Get Started with Sonar

Jump into Sonar with this tutorial that provides installation instructions for SonarQube and the Code Analyzer, followed by a Java example.

· Agile Zone ·
Free Resource
This article represents steps required to quickly get up and running with Sonar and keep it handy for your personal Java/.NET/PHP etc related projects. Please feel free to comment/suggest if I failed to mention one or more important points. Also, sorry for the typos.
SonarQube Scanning in 15 Minutes
Following are the key points described later in this article:
  • Sonar setup instructions
  • Configure HelloWorld Project for Code Analysis

Sonar Setup Instructions

Following are two key aspects of getting Sonar and dependencies installed, configured and setup for usage.

Installation of SonarQube

Following is instruction-set to get set up with SonarQube:

  • Go to Sonar community edition page and download.
  • Unzip the downloaded file
  • Go to <install_directory>/bin folder. You would find different folders related with OS platforms. As I have 64 bit Win system, I further went inside “windows-x86-64″ folder.
  • You will find different shell scripts files. For my win platform, I found different *.bat file such as InstallNTService.bat, StartNTService.bat, StartSonar.bat etc.
  • Copy the path of this folder which may look like S<install_director>/bin/Windows-x86-64 (in case of my laptop), and append it to “Path” environment variable.
  • Open a command prompt, type “StartSonar” command and execute. This would start a web server at default port of 9000.
  • Open a web browser and access the page, http://localhost:9000. You will see the page such as that shown in the screenshot below. This means that you have been able to successfully start your SonarQube server. Job, well done! Lets move further to analyze out HelloWorld project. I coded a HelloWorld Java project using Eclipse IDE.sonar hello world
  • Sonar comes with an embedded H2 database, by default. For quick setup and testing purpose, you may live with an embedded database. However, for production and real usage, one may want to use production-read databases such as MySQL, Oracle etc. For configuration instructions, edit <install_directory>/conf/sonar.properties to configure the database settings. Templates are available for every supported database. Just uncomment and configure the template you need and comment out the lines dedicated to H2 database.

Installation of Code Analyzer

Following is instruction-set to get set up with Code Analyzer (SonarQube Runner). The SonarQube Runner is recommended as the default launcher to analyze a project with SonarQube.

  • Goto Installing & configuring Sonar Runner page and download the sonar-runner-dist-versionno.zip file
  • Unzip the file to appropriate location
  • Setup the SONAR_RUNNER_HOME environment variable and assign it to SonarRunner installation directory
  • Add the <sonarrunner_install_directory>/bin directory to your path.
  • Check the basic installation by opening a new shell and executing the command sonar-runner -h (on Windows platform the command is sonar-runner.bat -h).
  • If above get executed, you are all set to analyze your first project with SonarQube runner.

Analysis of HelloWorld Java Project

  • Create a configuration file in the root directory of the project, namely, sonar-project.properties
  • You may choose to copy and paste the code given on this page, Analyzing with SonarQube Runner, in sonar-project.properties file. For setup purpose, just change the value of sonar.projectName to the project name you created.
  • Open a command prompt, and go to project root folder.
  • Execute “sonar-runner” command to run the analysis. You would see the analysis run.
  • Goto the browser and access the page, http://localhost:9000
  • You would find your project listed under “PROJECTS”. Click on your project listing and you would land up on the project dashboard.sonar code analysis

That is it!

Further reading

Code Analysis Part 1: What You Should Know About Technical Debt

Quick Start Guide to SonarQube for Static Code Analysis

Topics:
java ,enterprise-integration ,architecture ,sonarqube ,.net ,php ,static code analysis ,code analyzer

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}