Sonar is a static code analysis tool that covers below aspects of code quality.
- Architecture & design
- Code comments
- Coding rules for mainly using checkstyle
- Potential bugs
- Code complexity
- Unit tests and code coverage
- Code Duplications
Download and install Sonar
Assuming you have java already installed
-
Download the latest version from this page - http://www.sonarsource.org/downloads/
-
Unzip the package to a folder. Thats it
Starting Sonar
-
Go to sonar installation directory’s bin folder
-
Based on your platform and architecture go to the corresponding directory under bin and run the
sonar.sh start (for *nix platform)
or
StartSonar.bat start (for windows platform) -
For windows there are also scripts to make Sonar as a windows service.
-
Once Sonar is started it will be accessible at this ulr http://localhost:9000 . For remote access replace localhost with the address of the sonar server. Also 9000 is the default port and can be changed from conf/sonar.properties
Analyzing project using Sonar
Until recently it was only possible to use maven along with Sonar. From sonar 2.6 an ANT Task is available to use Sonar in ANT based projects. Below are the steps to use Sonar from ANT.
-
Download the Sonar ANT Task and copy in your ANT_HOME/lib directory
-
Open your project’s build.xml and add the below xml to it
Once the above is done run
ant sonar
This will run sonar on your code and push the metrics to the sonar server. Once the ant task is completed launch your sonar server (http://localhost:9000 or whatever) and view the reports.
Sonar Configuration
Sonar can be customized by changing settings in conf/sonar.properties file. Things you may typically want to change.
-
Port, ip at which sonar should run
-
Database settings to make Sonar run on a database like MySQL, Postgres , etc instead of the default derby database
More..
Sonar has some interesting plugins to enhance the features of Sonar and to facilitate analysis of code written in languages other than java. The complete plugin list is available here - http://docs.codehaus.org/display/SONAR/Sonar+Plugin+Library
For more reading visit -http://docs.codehaus.org/display/SONAR/Use+Sonar
