javaantpmdantbuilder

Fail build on PMD errors/warnings


I have a JAVA ANT project and I am trying to integrate PMD scripts with it so that I can check for all errors and warnings in my project.
Below is snippet of ANT script which I have added in my build.xml:

<property name="pmd.dir" value="buildconfig/build/pmd/" />
<path id="pmd.lib" >
   <fileset dir="${pmd.dir}">
     <include name="*.jar"/>
     <exclude name="/rulesets" />
   </fileset>
</path>
<target name="pmd" depends="init">
   <echo message="PMD Starting-----" />
   <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.lib"/>
   <pmd shortFilenames="true">
      <ruleset>unusedcode</ruleset>
      <formatter type="text" toFile="${pmd.dir}/pmd-ant-results.txt"/>
      <fileset dir="modules/app/">
         <include name="**/*.java"/>
      </fileset>    
   </pmd>
</target>  

ANT build is working fine with PMD giving proper error reports but I need to abort the build as failure when PMD encounters any errors in code.
I tried adding failOnRuleViolation="yes" but that did not stop build.
Is there anything else I need to add in script?


Solution

  • Try failOnRuleViolation="true" instead of failOnRuleViolation="yes"...

    <pmd shortFilenames="true" failOnRuleViolation="true">
        ...
    

    Some Ant tasks treat true and yes as equivalent, but many tasks don't know how to handle yes at all. It is possible that <pmd> is one of those tasks that doesn't handle yes.

    As a rule of thumb, avoid this gotcha in the future by always using true and false instead of yes and no.