在Ant中使用自定义PMD规则集文件

时间:2010-06-21 14:20:53

标签: ant classpath pmd

我想为我的构建使用自定义PMD规则集文件。基本上,我想使用许多内置规则集包,并关闭一些规则。

例如,假设我只想要字符串和基本规则,我有一个名为ruleset.xml的规则集文件:

<?xml version="1.0"?>
   <ruleset name="Custom ruleset"
       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">
     <description>
     This ruleset checks my code for bad stuff
  </description>
  <rule ref="rulesets/strings.xml"/>
  <rule ref="rulesets/basics.xml"/>

</ruleset>

然后我在我的Ant任务中包含对该文件的引用,如下所示:

<target name="pmd"
           description="Analyzes our source code for violations of good Java">
      <pmd shortFilenames="true" failuresPropertyName="failures.count" 
           rulesetfiles="ruleset.xml">

         <formatter type="xml" toFile="${build.home}/pmd.xml" />
         <fileset dir="src">
            <include name="**/*.java" />
         </fileset>
      </pmd>
      <echo message="PMD detected ${failures.count} violations" />
      <xslt in="${build.home}/pmd.xml" 
         style="tools/pmd/etc/xslt/pmd-report.xslt" 
         out="${build.home}/pmd.html" /> 

   </target>

此操作失败,但出现以下异常:

建立失败 E:\ build.xml:120:java.lang.RuntimeException:找不到该类无法找到资源rulesets / basics.xml。确保资源是有效的文件或URL,或者位于CLASSPATH上         at net.sourceforge.pmd.RuleSetFactory.parseRuleSetNode(RuleSetFactory.java:229)         at net.sourceforge.pmd.RuleSetFactory.createSingleRuleSet(RuleSetFactory.java:135)         at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:85) ...

基本上,规则集xml文件会破坏任务的类路径。我已经尝试将类路径元素添加到pmd任务,但这不起作用。

我需要添加什么才能使我的自定义规则集文件生效?我更喜欢在ant文件中添加一些东西,而不是规则集文件。

1 个答案:

答案 0 :(得分:0)

卫生署!在SO上写这个有助于调试我的问题。问题是没有basics.xml,它叫做basic.xml。修复拼写错误,一切运行得很好。