Sonar (codehaus) Custom PMD Rules

It took some time to resolve this.

Problem Statement: How to add custom PMD rules to Sonar?

Solution:

Note: ABCD is project name or company name

Step 1:

ABCD-PMD-RULES-ECLIPSE.XML: This file can be used in Eclipse. Also we need to use same in Sonar to have consistent results. Zip/jar this file as ABCD-PMD-Rules.jar.

All .xml files must be under root folder.
Drop this .jar file under path sonar-1.8extensionsrulespmd (Respective sonar folder.)
If using custom java classes, they need to be placed in jar file.

Important Change: Make rule name unique. Add project name or company name as prefix. Example: name=”pro_rulename”. Other wise it will create problem. Because these names are already in database and fails to import them. You can see this mysql exception in sonar.log file.




 ABCD Ruleset
 
 
If you have a class that has nothing but static methods, consider making it a Singleton.
Note that this doesn't apply to abstract classes, since their subclasses may
well include non-static methods.  Also, if you want this class to be a Singleton,
remember to add a private constructor to prevent instantiation.
 
 
3

 



Step 2:

Create ABCD_sonar_rules.xml file
Drop this .jar file under path sonar-1.8extensionsrulespmd (Respective sonar folder.)
This file is specific to Sonar.
Read http://docs.codehaus.org/display/SONAR/Manage+quality+profiles for more information.



 
 ABCD Use Singleton
 ABCD-PMD-RULES-ECLIPSE.XML/ABCD_UseSingleton
 <!-- available categories : Reliability, Portability, Maintainability, Efficiency, Usability -->
 
 
 


Step 3:

Login to Sonar Admin Console
You can see this rules under Sonar way
Copy this profile to Profile B
In profile B, remove all existing rules and keep only ABCD rules.
Make them mandatory
You can assign project to this rule set or we can make this as default rule set.

Step 4:

mvn sonar:sonar

This will generate new reports against new rules. Check the information on sonar site.

Notes: See this http://svn.codehaus.org/sonar/tags/1.4/sonar-rules-extensions/pmd-rules-extensions/pmd-rules-extensions.xml for more information

<pre>
  
    Maximum Methods Count Check
    rulesets/extensions.xml/MaximumMethodsCountCheck
    
    Maximum number of methods authorised

        Maximum number of methods authorised
        2
      
  
  
    Avoid if without using brace
    rulesets/extensions.xml/AvoidIfWithoutBrace
    
    Avoid if without using brace
  
 </pre>

Note: If there is any errors, Sonar wont display exact error on screen. Check logs for exact information.

sonar-1.8logssonar.log

sonar-1.8logswrapper.log

Important Links: http://docs.codehaus.org/display/SONAR/Collect+data

-o-

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s