Menu

Creating a custom rulest JAR file

Developers
SjaakH
2007-07-06
2012-10-07
  • SjaakH

    SjaakH - 2007-07-06

    How do i correctly create my custom ruleset and how do i add them to my classpath?

    I am using Maven 1.0.2. and PMD-3.5.jar. Maven has scripting for calling the goal "pmd" and the pmd.

    Maven calls the PMD from maven.xml:
    <attainGoal name="pmd">
    <j:set var="maven.pmd.rulesetfiles"
    value="${basedir}/${maven.superproject.path}/rulesets/pmd_general_code_ruleset.xml"/>

    ....
    

    My pmd_general_code_ruleset states:
    <?xml version="1.0"?>

    <ruleset name="pmd_general_code_ruleset">
    <description> The ruleset to generate the pmd_general_code.xml </description>
    <rule ref="myrulesets/basic.xml" />
    <rule ref="myrulesets/braces.xml" />
    <rule ref="myrulesets/clone.xml" />
    <rule ref="myrulesets/codesize.xml" />
    <rule ref="myrulesets/finalizers.xml" />
    <rule ref="myrulesets/imports.xml" />
    <rule ref="myrulesets/javabeans.xml" />
    <rule ref="myrulesets/naming.xml" />
    <rule ref="myrulesets/strictexception.xml" />
    <rule ref="myrulesets/strings.xml" />
    </ruleset>

    Maven comes with an error
    BUILD FAILED
    java.lang.RuntimeException: Couldn't find that class Can't find resource basic.xml. Make sure the resource is a valid file or URL or is on the CLASSPATH

    How can i get my custom ruleset correctly in a JAR (including folder structure or manifest or..) and is it enough to then put the JAR file on my project dependency list?

    I have tried lots of things, but I cannot find any good info or example how my JAR file should look and if the dependency neede any extra properties. I can only find information that i should put custom rulests in a project dependency so it is put on the classpath, but i cannot get this to work nor find any useful examples.

    My apologies if this question has been answered before but I could not find the correct info with my searches.

    With regards,

    SjaakH

     
    • Jeff Jensen

      Jeff Jensen - 2007-07-06

      Your question is probably better off on the Maven user list, as this PMD project doesn't maintain them. But, while we are at it... ;-)

      First, as you suggested, make a jar of your ruleset info "as normal" - put the ruleset xml files in a dir in it (e.g. /rulesets) and any custom XPath and Java rules in their respective locations... For us, I made a Maven project to build this and install into the repo. This Maven project needs the PMD jar as a dependency.

      Second, as you also suggested for your product's build, add your custom PMD rules jar as a dependency.

      Third, and this may be the missing piece in your configuration, configure the Maven PMD plugin properties to use the rulesets you want, e.g.: maven.pmd.rulesetfiles=rulesets/pmdruleset.xml,rulesets/customrulesrulest.xml
      Specify the paths to the ruleset files just as the appear in their respective jar. You can use rulesets in the PMD release or in your custom jar.

      That should be it...try those steps and see how much further it works, if not completely.

      Note that my examples are from Maven 1.1 experience. I'm not sure anymore if 1.0 has the same behavior, or if the latest PMD plugin works with Maven 1.0. I strongly suggest upgrading to Maven 1.1 ASAP. It is far superior...

       

Log in to post a comment.