...">

How to add custom rule in pmd and use it from terminal

I wrote my own rule in pmd and wrote a rule class.

User rule:

<?xml version="1.0"?>
<ruleset name="My custom rules"
 xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0              http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <rule name="WhileLoopsMustUseBracesRule"
     message="Avoid using 'while' statements without curly braces"
     class="WhileLoopsMustUseBracesRule">
        <description>
            Avoid using 'while' statements without using curly braces
        </description>
        <priority>3</priority>
        <example>
            <![CDATA[
                public void doSomething() {
                    while (true)
                    x++;
                }
            ]]>
        </example>
    </rule>
</ruleset>

and class:

import net.sourceforge.pmd.lang.java.rule.*;
import net.sourceforge.pmd.lang.java.ast.*;
public class WhileLoopsMustUseBracesRule extends AbstractJavaRule {
    public Object visit(ASTWhileStatement node, Object data) {
        System.out.println("hello world");
        return data;
    }
}

Now when I type the command from the terminal

sh run.sh pmd -d /Users/sree/Documents/learning/programs/java xml /Users/sree/Desktop/customrule.xml 

I get this error:

The main parameter "xml" was passed, but the main parameter was not defined

Now add a custom rule so that I can access it from the terminal and after linking both the rule and the ruleset.xml file in the jar file, how to include it in the class path

NOTE. I am using a Mac, not a Windows operating system.

+3
source share
1 answer

I use this:

./run.sh pmd -d /Users/ardaaslan/Documents/workspace/PMDRulesTest/src/AttributeTypeAndNameIsInconsistentTest.java -f text -R /Users/ardaaslan/Downloads/pmd-src-5.5.1/pmd-java/src/main/resources/rulesets/java/customrules.xml -version 1.7

-d . -f, . rule.xml.

- pmd-bin classpath

javac -cp /Users/ardaaslan/Downloads/pmd-bin-5.5.0/lib/\* AttributeTypeAndNameIsInconsistentRule.java

.class jar

pmd-bin-5.5.0/lib

pmd,

run.sh .

0

All Articles