PMD Integration with JDeveloper through Ant External Tools

Posted by Torsten Kleiber on June 26, 2013 Tags: Oracle JDeveloper PMD External-Tools Static-Code-Analysis

After little code change in pmd the integration like described in some blogs from Shay Shmeltzer is now possible.

I use here JDeveloper 11.2.0.4, with other versions this should be very similar. Now let’s start:

  1. Create a new folder

  2. Download PMD 5.0.4

  3. Extract the zip file into the created folder

  4. Create a file pmd.xml into the created folder

  5. Enter following code into the file pmd.xml:

    <?xml version="1.0" encoding="windows-1252" ?>
    <project xmlns="antlib:org.apache.tools.ant" name="Project1" default="all" basedir=".">
      <path id="classpathForPMD">
        <fileset dir="pmd-bin-5.0.4\lib">
          <include name="**/**.jar"/>
        </fileset>
      </path>
      <taskdef name="pmd" classpathref="classpathForPMD" classname="net.sourceforge.pmd.ant.PMDTask"/>
      <target name="pmdAll">
        <echo message="PMD is running on directory ${dir}" level="info"/>
        <pmd rulesetfiles="rulesets/internal/dogfood.xml" failOnRuleViolation="true" minimumPriority="1">
          <fileset dir="${dir}">
            <include name="**/src/**/*.java"/>
          </fileset>
        </pmd>
      </target>
      <target name="pmd">
        <echo message="PMD is running on file ${dir}\${file}" level="info"/>
        <pmd rulesetfiles="rulesets/internal/dogfood.xml" failOnRuleViolation="true">
          <fileset dir="${dir}">
            <include name="${file}"/>
          </fileset>
        </pmd>
      </target>
    </project>
  6. Select Tools > External Tools from the JDeveloper menu

  7. Press Button New

  8. Select Tool Type: Apache Ant

  9. Select your created pmd.xml file in Ant Buildfile:

    PMD Integration with JDeveloper through Ant External Tools   Shuttle target pmdAll to SelectedTargets
  10. Shuttle target pmdAll to SelectedTargets

    PMD Integration with JDeveloper through Ant External Tools   Select your created pmd xml file in Ant Buildfile
  11. Add Property dir with value ${file.dir}

    PMD Integration with JDeveloper through Ant External Tools   Add Property dir with value file dir
  12. Skip the Options and Process Step to leave the defaults

  13. Add the lib directory of pmd as additional classpath entry

    PMD Integration with JDeveloper through Ant External Tools   Add the lib directory of pmd as additional classpath entry
  14. Enter Caption and Icon as you want

    PMD Integration with JDeveloper through Ant External Tools   Enter Caption and Icon as you want
  15. Add the tool to the Navigator Context Menu and Main Toolbar and do not forget to set Log Output to Messages Log

    PMD Integration with JDeveloper through Ant External Tools   Add the tool
  16. Press Next and Finish.

Now you can call pmd e.g. from the fusion order demo application:

PMD Integration with JDeveloper through Ant External Tools   call pmd e.g. from the fusion order demo application

You see the results of the ant process in the message pane and you can click on the link to open the file and jump to the rule violation:

PMD Integration with JDeveloper through Ant External Tools   results of the ant process in the message pane

In upcoming blogs I will show you how you can define your own rulesets (replacing the ones in pmd.xml) and how to use it with any kind of xml files, eg. jws, jpr, xcfg etc.