DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Configuring Sonar in Ubuntu and Analyzing Project

Configuring Sonar in Ubuntu and Analyzing Project

Shan Arshad user avatar by
Shan Arshad
·
May. 10, 14 · Interview
Like (0)
Save
Tweet
Share
17.48K Views

Join the DZone community and get the full member experience.

Join For Free

Static code analyzer now become very common and useful while teams will be working specially using RAD. As to meet close deadlines and sprint dates developers usually get out of the right path and use some narrow tactics to accomplish some task.

I have been facing the same situation in my project in which we couldn’t been able to do peer reviews just to meet close deadlines. So we have configured Sonar just to remove any potential issue which we have missed while developing or reviewing the code.

  • Download latest version of Sonar. e.g 4.3 Stable Release.
  • Unzip the package using command: unzip sonarPakageName.zip
  • Open to the file which is in folder: /conf/sonar.properties

sonar.properties File:

In that file you can configure sonar settings related to Database and HTTP.
Just to make it easy and time saving we just set the database settings and rest of it will be on default settings.

# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username={userName}
sonar.jdbc.password={password}
# Comment the following line to deactivate the default embedded database.
#sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
#----- MySQL 5.x
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true

In case if you find any issue while connecting to database then change the localhost to ipAddress of your system.

Create a DB which you have mentioned in “sonar.jdbc.url”. For example above we have used “sonar” as our database name.

SonarQube runner:

Download SonarQube runner latest version. It is responsible for analyzing the project and it automatically submit the results in database. Which sonar use for reporting purposes.

  • Unzip the package using command: unzip sonar-runner
  • Open the file which is in folder: /conf/sonar-runner.properties

sonar-runner.properties File:

In this file you will find Database and security related settings of sonar qube.
Uncomment any driver of your choice.
#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
#----- Global database settings
sonar.jdbc.username={userName}
sonar.jdbc.password={password}


Sonar-project.properties File:

sonar-project.properties file contains information about project and source folders. This file persists at root of your project directory. Below is the content which we can define in that file.

# Required metadata
sonar.projectKey=com.javapitshopt
sonar.projectName=javapitshop
sonar.projectVersion=1.1.6

# Paths to source directories.
# Paths are relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Do not put the "sonar-project.properties" file in the same directory with the source code.
# (i.e. never set the "sonar.sources" property to ".")
sonar.sources=src,WebContent,build/classes

# The value of the property must be the key of the language.
sonar.language=java

# Encoding of the source code
sonar.sourceEncoding=UTF-8

# Additional parameters
#sonar.my.property=value

Analyzer Execution:

All the configurations are completed. Now we need to run sonar and analyze our project with sonar-qube runner. 

To execute sonar move to the bin directory in sonar base folder and execute command.

  • ./sonar.sh start                [in case of any issue try doing that with root permission or with sudo.]

Hit URL localhost:9000 in browser and you will find sonar web page in front of you.
Now move to your project root directory where sonar-project.properties file is being saved and execute command.

  • /pathToSonarRunnerDirectory/bin/sonar-runner

It will start analyzing the project and populate the database so that sonar can use it to display the statistics.
After its completion we can check the results at sonar portal by refreshing the portal. 
ubuntu

Published at DZone with permission of Shan Arshad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • SAST: How Code Analysis Tools Look for Security Flaws
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Top 12 Technical Skills Every Software Tester Must Have

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: