no

How to Use Pmd With Eclipse

This tutorial will explain how to use PMD (http://pmd.sourceforge.net/) with eclipse. What you need: 1.) eclipse-java-helios 2.) PMD (do...

This tutorial will explain how to use PMD (http://pmd.sourceforge.net/) with eclipse.

What you need:
1.) eclipse-java-helios
2.) PMD (download the latest version here: http://pmd.sourceforge.net/). Extract in c:\java
a.) From here you can download a zipped update site, install that in eclipse by:
Help->Install New Software->Add->Archive->Then just follow the steps.
3.) After eclipse update, you can immediately run PMD by: right click project->PMD->Generate reports. You can refer on the pmd link for more explanations.
4.) For example this code that I copied from PMD website:
package com.kbs.testng;

public class TestPMDDemo {
	public static void main(String args[]) {
		try {
			// do something
		} catch (Throwable th) { // Should not catch throwable
			th.printStackTrace();
		}
	}
}
Will output:

5.) If you want to run PMD via ant task, here's the script (add this to your build.xml):

<?xml version="1.0" encoding="UTF-8"?>
<project name="JavaHelpers" default="" basedir=".">
	<!-- set global properties for this build -->
	<property name="program_name" value="JavaHelpers" />
	<property name="package_name" value="JavaHelpers" />
	<property name="src" value="src" />
	<property name="build" value="build" />
	<property name="pmd.home" value="C:/java/pmd-4.2.5" />
	<property name="pmd.jar" value="pmd-4.2.5.jar" />

	<path id="compile.boot.path">
		<fileset dir="${testng.home}">
			<include name="${testng.jar}" />
		</fileset>
		<fileset dir="${java.home}/lib">
			<include name="rt.jar" />
		</fileset>
	</path>

	<target name="pmd">
		<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
			<classpath location="${pmd.home}/lib/${pmd.jar}" />
		</taskdef>
		<pmd shortFilenames="true">
			<ruleset>rulesets/favorites.xml</ruleset>
			<ruleset>basic</ruleset>
			<formatter type="html" toFile="pmd_report.html" linkPrefix="http://pmd.sourceforge.net/xref/" />
			<fileset dir="${src}">
				<include name="**/*.java" />
			</fileset>
		</pmd>
	</target>	

	<!-- Compile Source -->
	<target name="compile">
		<mkdir dir="${build}/classes" />
		<javac destdir="build/classes" target="1.6" source="1.6">
			<bootclasspath refid="compile.boot.path" />
			<src path="${src}" />
			<include name="**/*.java" />
		</javac>
	</target>
</project>
It will create a file: pmd_report.html in the project's root directory.

Related

java-getting-started 8341421437017895538

Post a Comment Default Comments

1 comment

John Ortiz Ordoñez said...

Thanks for this how-to about PMD in Eclipse. It was useful and easy-to-follow.

Can you recommend me books about PMD? Thanks in advance.

item