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.8\extensions\rules\pmd (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.
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="pmd-rules">
<description>ABCD Ruleset</description>
<rule class="net.sourceforge.pmd.rules.design.UseSingleton"
message="All methods are static. Consider using Singleton instead. Alternatively, you could add a private constructor or make the class abstract to silence this warning."
name="ABCD_UseSingleton">
<description>
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.
</description>
<example><![CDATA[
public class MaybeASingleton {
public static void foo() {}
public static void bar() {}
}
]]></example>
<priority>3</priority>
<properties/>
</rule>
</ruleset>
Step 2:
Create ABCD_sonar_rules.xml file
Drop this .jar file under path \sonar-1.8\extensions\rules\pmd (Respective sonar folder.)
This file is specific to Sonar.
Read http://docs.codehaus.org/display/SONAR/Manage+quality+profiles for more information.
<rules>
<rule key="ABCD_UseSingleton">
<name>ABCD Use Singleton</name>
<configKey>ABCD-PMD-RULES-ECLIPSE.XML/ABCD_UseSingleton</configKey>
<!-- available categories : Reliability, Portability, Maintainability, Efficiency, Usability -->
<category name="Reliability"/>
<description><![CDATA[Test Description................ ]]></description>
</rule>
</rules>
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><rules>
<rule key="MaximumMethodsCountCheck">
<name>Maximum Methods Count Check</name>
<configKey>rulesets/extensions.xml/MaximumMethodsCountCheck</configKey>
<category name="Usability"/>
<description>Maximum number of methods authorised</description>
<param key="maxAuthorisedMethodsCount" type="i">
<description>Maximum number of methods authorised</description>
<defaultValue>2</defaultValue>
</param>
</rule>
<rule key="AvoidIfWithoutBrace">
<name>Avoid if without using brace</name>
<configKey>rulesets/extensions.xml/AvoidIfWithoutBrace</configKey>
<category name="Usability"/>
<description>Avoid if without using brace</description>
</rule>
</rules></pre>
Note: If there is any errors, Sonar wont display exact error on screen. Check logs for exact information.
\sonar-1.8\logs\sonar.log
\sonar-1.8\logs\wrapper.log
Important Links: http://docs.codehaus.org/display/SONAR/Collect+data
-o-