diff --git a/pmd/src/net/sourceforge/pmd/SimpleRuleSetNameMapper.java b/pmd/src/net/sourceforge/pmd/SimpleRuleSetNameMapper.java index 42140ee038..6e90cca107 100644 --- a/pmd/src/net/sourceforge/pmd/SimpleRuleSetNameMapper.java +++ b/pmd/src/net/sourceforge/pmd/SimpleRuleSetNameMapper.java @@ -7,7 +7,7 @@ import java.util.StringTokenizer; public class SimpleRuleSetNameMapper { private StringBuffer rulesets = new StringBuffer(); - private Map nameMap = new HashMap(); + protected Map nameMap = new HashMap(); public SimpleRuleSetNameMapper(String ruleString) { populateNameMap(); @@ -25,7 +25,7 @@ public class SimpleRuleSetNameMapper { return rulesets.toString(); } - private void check(String name) { + protected void check(String name) { if (name.indexOf("rulesets") == -1 && nameMap.containsKey(name)) { append(nameMap.get(name)); } else { @@ -33,7 +33,7 @@ public class SimpleRuleSetNameMapper { } } - private void append(String name) { + protected void append(String name) { if (rulesets.length() > 0) { rulesets.append(','); } diff --git a/pmd/src/net/sourceforge/pmd/ant/PMDTask.java b/pmd/src/net/sourceforge/pmd/ant/PMDTask.java index 2cdc748a16..63dd5c68e7 100644 --- a/pmd/src/net/sourceforge/pmd/ant/PMDTask.java +++ b/pmd/src/net/sourceforge/pmd/ant/PMDTask.java @@ -157,8 +157,24 @@ public class PMDTask extends Task { createLongAuxClasspath().setRefid(r); } + private class AntTaskNameMapper extends SimpleRuleSetNameMapper { + public AntTaskNameMapper(String s) { + super(s); + } + + protected void check(String name) { + if (name.indexOf("rulesets") == -1 && nameMap.containsKey(name)) { + append(nameMap.get(name)); + } else { + // substitute env variables/properties + append(getProject().replaceProperties(name)); + } + } + + } + private void doTask(){ - ruleSetFiles = new SimpleRuleSetNameMapper(ruleSetFiles).getRuleSets(); + ruleSetFiles = new AntTaskNameMapper(ruleSetFiles).getRuleSets(); ClassLoader cl; if (classpath == null) { @@ -168,13 +184,22 @@ public class PMDTask extends Task { log("Using the AntClassLoader", Project.MSG_VERBOSE); cl = new AntClassLoader(getProject(), classpath); } + /* + * 'basedir' is added to the path to make sure that relative paths + * such as "resources/custom_ruleset.xml" still + * work when ant is invoked from a different directory using "-f" + */ + String extraPath = getProject().getBaseDir().toString(); + if (auxClasspath != null) { log("Using auxclasspath: " + auxClasspath, Project.MSG_VERBOSE); - try { - cl = new ClasspathClassLoader(auxClasspath.toString(), cl); - } catch (IOException ioe) { - throw new BuildException(ioe.getMessage()); - } + extraPath = auxClasspath.toString() + File.pathSeparator + extraPath; + } + + try { + cl = new ClasspathClassLoader(extraPath, cl); + } catch (IOException ioe) { + throw new BuildException(ioe.getMessage()); } final ClassLoader classLoader = cl; @@ -340,18 +365,6 @@ public class PMDTask extends Task { ruleSetFiles = getNestedRuleSetFiles(); } - // convert relative paths and substitute env variables/properties - final StringBuffer sb = new StringBuffer(); - for(String s: ruleSetFiles.split(",")) { - Path p = new Path(getProject()); - p.setPath(getProject().replaceProperties(s)); - if (sb.length() > 0) { - sb.append(','); - } - sb.append(p); - } - ruleSetFiles = sb.toString(); - if (!targetJDK.equals("1.3") && !targetJDK.equals("1.4") && !targetJDK.equals("1.5") && !targetJDK.equals("1.6") && !targetJDK.equals("1.7") && !targetJDK.equals("jsp")) { throw new BuildException("The targetjdk attribute, if used, must be set to either '1.3', '1.4', '1.5', '1.6', '1.7' or 'jsp'"); }