Menu

How to run a custom rule explained

KKA
2009-09-22
2012-10-07
  • KKA

    KKA - 2009-09-22

    Hi ,

    I'm new to PMD custom rule generation. I have written a small piece of code as
    explained in the PMD tutorial.

    I have the custom rules xml as well.

    When i am trying to run with the pmd.bat "pmd.bat D:\PMDTestSrc\src xml
    D:\A\bin\myPMD.xml" in wondows, it was throwing an error saying class not
    found.

    Rule set xml is as follows:

    <?xml version="1.0"?>
    <ruleset name="My custom rules"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-
    instance"

    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd&
    quot;>

    <rule name="NameList"
    message="Try extending PMD"
    class="com.kiran.sample.ASTNameFind">
    <description>
    Try extending PMD
    </description>
    <priority>3</priority>

    <example>
    <![CDATA]>
    </example>
    </rule>
    </ruleset>

    Please let me know where i have gone wrong.

    Thanks and Regards
    Kiran

     
  • Romain PELISSE

    Romain PELISSE - 2009-09-22

    Hi,

    As your class is not found this is most likely a classpath issue. Are you sure
    you added either the .class file with rule or the jar containing it to your
    classpath ?

     
  • KKA

    KKA - 2009-09-23

    Hi rpelisse ,

    Thank you. It was a classpath problem. Now i am able to run.

    Regards
    Kiran

     
  • Rohit Gopalan

    Rohit Gopalan - 2012-03-18

    I am having serious issues on how to run PMD. I am running it for the first
    time through command line.
    Just wondering if someone can help me out.

    Here is my ruleset xml file

    <root>
    <ruleset name="Project Rules" <br="">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"
    xsi:noNamespaceSchemaLocation="http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <rule name="AvoidDataClass" <br="">message ="Add more methods than the getter and setter methods"
    class="Project.DataClassRule" >
    <description>
    Add more methdos than the getter and setter methods
    </description>
    <example>
    <! [CDATA]>
    </example> </rule></ruleset></root>



    I was running PMD in the following manner:
    pmd C:\Users\Rohit Gopalan\Documents\2012\FinalYearProject\PMD
    Rules\project\src text C:\Users\Rohit
    Gopalan\Documents\2012\FinalYearProject\PMD
    Rules\project\src\projectRuleSet.xml

    The issue I'm getting is an IllegalArgumentException as 'text' was recognised
    as an Unknown option. I am trying to print a text file of violations.

     
  • Brian R

    Brian R - 2012-03-18

    It looks like you have spaces in your file paths. Any command-line tool will
    have problems parsing that effectively so i would suggest changing that for
    starters. Have you tried the other output options as well?

     
  • Rohit Gopalan

    Rohit Gopalan - 2012-03-18

    Thanks Brian
    The change of directories for the java and xml files worked.
    I had tried debug for the previous attempt, but your suggestion works wonders
    Cheers
    Rohit

     
  • Rohit Gopalan

    Rohit Gopalan - 2012-03-18

    I have a new problem and it could be within my XML ruleset file.
    I tried to run the following D:\FYP\PMD\build\classes\Project text
    D:\FYP\PMD\src\Project\projectRuleSet.xml
    the following error appeared: Cannot find class DataClassRule
    I fixed some bugs in my XML ruleset file.
    Here it is

    <ruleset name="Project Rules" <br="">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"
    xsi:noNamespaceSchemaLocation="http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <rule name="AvoidDataClass" <br="">message ="Add more methods than the getter and setter methods"
    class="DataClassRule">
    <description>
    Add more methdos than the getter and setter methods
    </description>
    <priority>
    3
    </priority>
    <example>
    <![CDATA]>
    </example>
    </rule>
    </ruleset>

    Please offer suggestions to help me out.

     
  • Brian R

    Brian R - 2012-03-18

    class="my.full.packagename.DataClassRule"

     
  • Rohit Gopalan

    Rohit Gopalan - 2012-03-18

    package name of DataClassRule class is Project.
    Brian, will you be able to elaborate the 'my.full.packagename' bit please?
    Such as give an example.

     

Log in to post a comment.