Sonar
Sonar [1] is an open source project hosted at Codehaus [2]. Sonar is well known in the Java Development, since it is a based on the old Maven plugins. Also Sonar no longer is dependent from maven as build tool.
Introduction
Sonar is an open platform to manage code quality. Is looks at 7 axes of code quality:
- Architecture and design
- Coding rules
- Comments
- Complexity
- Duplications
- Potential bugs
- Unit tests
Sonar is based on the ability of extending by plug-ins principle for covering new language, adding riles engines, computing advanced metrics. In summary:
- More than 50 plug-ing [3] are already available.
- It not only covers Java anymore but also PHP and much more languages (C++, C#, Cobol, Delphi, Drools, Flex, JavaScript, Natural, PHP, Visual Basic, and more...).
Sonar is a web-based application. Rules, alerts, thresholds, exclusions settings cab all be configured online. It is:
- Database storage of (previous) results
- Historical measures can be compared
Requirements
The only pre-requisite is Java 1.5 or higher.
Installation
A description of the installation can be found on the Sonar Website [4]. However this installation focusses on the installation on Apple OS X running MAMP.
- Download the latest code from the SonarSource downloads page.
- Uncompress the downloaded file into a directory (i.e. /wwwroot/Sonar). This directory is named SONAR_WWW_HOME.
- Add the web-settings to $SONAR_WWW_HOME/conf/sonar.properties. Please note that you may have to change this in case you have already used these settings.
- sonar.web.host: 0.0.0.0
- sonar.web.port: 8080
- sonar.web.contect: /sonar
- Add the database settings to $SONAR_WWW_HOME/conf/sonar.properties.
- sonar.jdbc.user: sonar
- sonar.jdbc.password: sonar
- In MySQL of MAMP do:
- Create new schema sonar
- Add user named 'sonar'
- Add password for user sonar sonar.
- Very important, uncheck the settings 'Allow local access only in the MySQL-Database settings of MAMP.
- Execute the following script to start the server:
- Go to the location where sonar is installed (SONAR_RUNNER_HOME/bin/<YOUR-OS>
- $ ./sonar.sh start
- Check the logger of Sonar:
- $ tail -f -n 50 $SONAR_WWW_HOME/logs/sonar.log
- In case not errors can be found you can still check:
- $ ./sonar.sh status
- Now check sonar by going to the web-url:
Installing the client
The possible clients are:
- Sonar runner
- Maven
- Ant
- CI Engine.
If the previous steps are successful you can add the recommended client: Sonar Runner to this installation.
- Download the runner from Sonar-Runner download webpage.
- Uncompress the downloaded file into a directory named $SONAR_RUNNER_HOME in the next steps (i.e. /wwwroot/sonar/runner).
- Update the global settings in file $SONAR_RUNNER_HOME/conf/sonar-runner.properties:
- Database connection (i.e. MySql, sonar.jdbc.url, sonar.jdbc.driver)
- Server URL (i.e. sonar.host.url).
- Create a new SONAR_RUNNER_HOME environment variable set the $SONAR_RUNNER_HOME.
- Check the basic installation.
- Open a new shell
- Execute the command:
$ sonar-runner -h usage: sonar-runner [options] Options: -h,--help Display help information -X,--debug Produce execution debug output -D,--define <arg> Define property
Adding PHP
For successful sonar-running a PHP projects:
- Downlaod the PHP jar from sonar
- Install in the extensions/plugins directory.
- Stop Sonar
- Start Sonar
However you need to have installed:
- PHPUnit (PHP Unit testing)
- pear channel-discover pear.phpunit.de
- pear channel-discover pear.symfony-project.com
- pear install phpunit/PHPUnit-3.5.5
- XDebug (for code coverage computation) [5]. Since Apple OS X does not have apt-get utility you need an alternative such as home-brew, but installing with this tools is also no 100%.
- apt-get install make
- apt-get install php5-dev
- pecl install xdebug
- The best and easy way is to follow Kubishkin's installation method, (Installing PHP Xdebug on Mac OS X 10.7 Lion in the See also).
- pdepended [6].
- Download pdepend using pear (you may have to sudo the commands):
- pear channel-discover pear.pdepend.org
- pear install pdepend/PHP_Depend-beta
- phpmd or PHP Mess Detector [7] installable using pear:
- pear channel-discover pear.phpmd.org
- pear channel-discover pear.pdepend.org
- pear install --alldeps phpmd/PHP_PMD
- phpcs PHP Code Sniffer [8] using pear:
- pear install PHP_CodeSniffer-1.5.0RC1
Look at the http://docs.codehaus.org/display/SONAR/PHP+Plugin for more information.
See also
top Apple OS X does not have the apt-get utility. An alternative is homebrew [9].
- HomeBrew Wiki, Documentation on install, faq and usage.
It is more easy to read the following instruction for installing debug
- Kubishkin, Installing PHP Xdebug on Mac OS X 10.7 Lion.
PHP CodeSniffer
- CodeSniffer, Technical Documentation of the PHP CodeSniffer Core Code and the CodeSniffer sniffs.
Reference
- ↑ sonarsource.org Sonar Website on Codehaus.
- ↑ Codehaus.org, website. Codehaus is a collaborative environment for building opensource projects with a strong emphasis on modern languages, focussed on quality components that meet real world needs.
- ↑ Sonar plug-ins library and languages.
- ↑ Sonar Install + Configuration, Installing and Configuring Sonar Runner for different OS.
- ↑ debug.org, The Xdebug extension helps you debugging your script by providing a lot of valuable debug information.
- ↑ pdepend, Website. Static analysis and software metrics computation (i.e. Cyclomatic Complexity, Coupling, Class interface Size).
- ↑ PHP Mess Detector, a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly and easy to configure frontend for the raw metrics measured by PHP Depend.
- ↑ PHP CS, PHP Code Sniffer webpage
- ↑ homebrew, The missing package manager for OS X