Brazilian, developer by profession, gamer for fun, amateour athletic and cyclist promising. Passionate for technology and inovation, Agile methodologies evangelist as SCRUM and Kanban, make continuous process improvements and sharing the knowledge. Especialist in Enterprise Java, web application and Open Source techonologies since 2007. Valdemar has posted 6 posts at DZone. View Full User Profile

Configuring Sonar with Maven

03.21.2011
| 47941 views |
  • submit to reddit

Sonar is an open source web-based application to manage code quality which covers seven axes of code quality as: Architecture and design, comments, duplications, unit tests, complexity, potential bugs and coding rules. Developed in Java and can cover projects in Java, FlexPHPPL/SQLCobol and Visual Basic 6.

It's very efficient to navigate, offering visual reporting and you can follow metrics evolution of your project and combine them.

There is an online project called Nemo dedicated to open source projects, as you can see projects like JettyApache Lucene and Apache Tomcat.

So, let's config Sonar to work together with Maven. First of all set up Sonar server and other configurations  in Maven's settings.xml file:

<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>jdbc:postgresql://localhost/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>org.postgresql.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>user</sonar.jdbc.username>
<sonar.jdbc.password>password</sonar.jdbc.password>
<!-- SERVER ON A REMOTE HOST -->
<sonar.host.url>http://localhost:9000</sonar.host.url>
</properties>
</profile>

You must to have a Sonar server running and define it in sonar.host.url parameter. In this example I'm using the default Sonar URL, http://localhost:9000 , and you must set user name and password for your database. To install local Sonar Server you can see this link.

After that, to execute the code analysers and save results in Sonar database just execute mvn sonar:sonar. You can config Sonar to execute in your CI (Continuous Integration) application.  Hudson is a  perfect match, because there is a Sonar plugin for it. See the final result below:

 

I prefer Teamcity CI, but there isn't a stable plugin as you can see in compatibility matrix. To resolve this plugin's problem, just add sonar:sonar in the maven command executed by Teamcity.

Sonar is an essential tool for your software project to help you evaluating how cohesive your classes are, warning for future problems as code complexity or duplication problems arise, and will help you to have a cleaner code. 

If you have any question or some difficulties, contact me.

4.333335
Your rating: None Average: 4.3 (3 votes)
Published at DZone with permission of its author, Valdemar Júnior.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

John Ericksen replied on Tue, 2011/03/22 - 11:11am

+1 for Sonar / Hudson/Jenkins integration.  Makes static code analysis effortless.

Carla Brian replied on Sun, 2012/07/22 - 6:10pm

This announce is a good opportunity to explain further the way the Sonar maven plugin operates. In fact, this plugin acts as a very light bootstrap mechanism whose only responsibility is to download all the business logic from the Sonar web server and execute it.  - Mercy Ministries

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.