Modified PMD Ant task to use built in Ant logging
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1849 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -60,7 +60,7 @@
|
||||
|
||||
<target name="pmd">
|
||||
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
|
||||
<pmd rulesetfiles="rulesets/favorites.xml" verbose="true">
|
||||
<pmd rulesetfiles="rulesets/favorites.xml">
|
||||
<formatter type="net.sourceforge.pmd.renderers.HTMLRenderer" toFile="foo.html"/>
|
||||
<fileset dir="/usr/local/java/src/java/lang/ref">
|
||||
<include name="**/*.java"/>
|
||||
|
@ -1,5 +1,5 @@
|
||||
????? - 1.06:
|
||||
Removed "verbose" attribute from CPD Ant task; now it uses built in logging so you can do a "ant -verbose cpd". Thanks to Philippe T'Seyen for the code.
|
||||
Removed "verbose" attribute from PMD and CPD Ant tasks; now they use built in logging so you can do a "ant -verbose cpd" or "ant -verbose pmd". Thanks to Philippe T'Seyen for the code.
|
||||
TODO - fix it so tests and rules don't duplicate the xpath expressions
|
||||
|
||||
April 17 - 1.05:
|
||||
|
@ -39,16 +39,4 @@ public class PMDTaskTest extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testBogusRuleSet() {
|
||||
PMDTask task = new PMDTask();
|
||||
task.setPrintToConsole(true);
|
||||
task.setRuleSetFiles("fiddlesticks");
|
||||
try {
|
||||
task.execute();
|
||||
throw new RuntimeException("Should have thrown a BuildException - bogus rulesets");
|
||||
} catch (BuildException be) {
|
||||
// cool
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import org.apache.tools.ant.BuildException;
|
||||
import org.apache.tools.ant.DirectoryScanner;
|
||||
import org.apache.tools.ant.Task;
|
||||
import org.apache.tools.ant.AntClassLoader;
|
||||
import org.apache.tools.ant.Project;
|
||||
import org.apache.tools.ant.types.FileSet;
|
||||
import org.apache.tools.ant.types.Path;
|
||||
import org.apache.tools.ant.types.Reference;
|
||||
@ -33,7 +34,6 @@ public class PMDTask extends Task {
|
||||
private List formatters = new ArrayList();
|
||||
private List filesets = new ArrayList();
|
||||
private boolean shortFilenames;
|
||||
private boolean verbose;
|
||||
private boolean printToConsole;
|
||||
private String ruleSetFiles;
|
||||
private boolean failOnError;
|
||||
@ -56,10 +56,6 @@ public class PMDTask extends Task {
|
||||
this.failOnRuleViolation = fail;
|
||||
}
|
||||
|
||||
public void setVerbose(boolean verbose) {
|
||||
this.verbose = verbose;
|
||||
}
|
||||
|
||||
public void setPrintToConsole(boolean printToConsole) {
|
||||
this.printToConsole = printToConsole;
|
||||
}
|
||||
@ -91,21 +87,17 @@ public class PMDTask extends Task {
|
||||
try {
|
||||
RuleSetFactory ruleSetFactory = new RuleSetFactory();
|
||||
if (classpath == null) {
|
||||
printIfVerbose("Using the normal ClassLoader");
|
||||
log("Using the normal ClassLoader", Project.MSG_VERBOSE);
|
||||
rules = ruleSetFactory.createRuleSet(ruleSetFiles);
|
||||
} else {
|
||||
printIfVerbose("Using the AntClassLoader");
|
||||
log("Using the AntClassLoader", Project.MSG_VERBOSE);
|
||||
rules = ruleSetFactory.createRuleSet(ruleSetFiles, new AntClassLoader(project, classpath));
|
||||
}
|
||||
} catch (RuleSetNotFoundException rsnfe) {
|
||||
throw new BuildException(rsnfe.getMessage());
|
||||
} catch (RuleSetNotFoundException e) {
|
||||
throw new BuildException(e.getMessage());
|
||||
}
|
||||
|
||||
printIfVerbose("Using these rulesets: " + ruleSetFiles);
|
||||
for (Iterator i = rules.getRules().iterator();i.hasNext();) {
|
||||
Rule rule = (Rule)i.next();
|
||||
printIfVerbose("Using rule " + rule.getName());
|
||||
}
|
||||
logRulesUsed(rules);
|
||||
|
||||
PMD pmd = new PMD();
|
||||
RuleContext ctx = new RuleContext();
|
||||
@ -116,7 +108,7 @@ public class PMDTask extends Task {
|
||||
String[] srcFiles = ds.getIncludedFiles();
|
||||
for (int j = 0; j < srcFiles.length; j++) {
|
||||
File file = new File(ds.getBasedir() + System.getProperty("file.separator") + srcFiles[j]);
|
||||
printIfVerbose("Processing file " + file.getAbsoluteFile().toString());
|
||||
log("Processing file " + file.getAbsoluteFile().toString(), Project.MSG_VERBOSE);
|
||||
ctx.setSourceCodeFilename(shortFilenames ? srcFiles[j] : file.getAbsolutePath());
|
||||
try {
|
||||
pmd.processFile(new FileInputStream(file), rules, ctx);
|
||||
@ -125,9 +117,7 @@ public class PMDTask extends Task {
|
||||
throw new BuildException(fnfe);
|
||||
}
|
||||
} catch (PMDException pmde) {
|
||||
if (verbose) {
|
||||
pmde.printStackTrace();
|
||||
}
|
||||
log(pmde.toString(), Project.MSG_VERBOSE);
|
||||
if (failOnError) {
|
||||
throw new BuildException(pmde);
|
||||
}
|
||||
@ -136,7 +126,7 @@ public class PMDTask extends Task {
|
||||
}
|
||||
}
|
||||
|
||||
printIfVerbose("Problems found: " + ctx.getReport().size());
|
||||
log(ctx.getReport().size() + " problems found", Project.MSG_VERBOSE);
|
||||
|
||||
if (!ctx.getReport().isEmpty()) {
|
||||
for (Iterator i = formatters.iterator(); i.hasNext();) {
|
||||
@ -153,7 +143,7 @@ public class PMDTask extends Task {
|
||||
|
||||
if (printToConsole) {
|
||||
Renderer r = new TextRenderer();
|
||||
log(r.render(ctx.getReport()));
|
||||
log(r.render(ctx.getReport()), Project.MSG_INFO);
|
||||
}
|
||||
|
||||
if (failOnRuleViolation) {
|
||||
@ -162,6 +152,14 @@ public class PMDTask extends Task {
|
||||
}
|
||||
}
|
||||
|
||||
private void logRulesUsed(RuleSet rules) {
|
||||
log("Using these rulesets: " + ruleSetFiles, Project.MSG_VERBOSE);
|
||||
for (Iterator i = rules.getRules().iterator();i.hasNext();) {
|
||||
Rule rule = (Rule)i.next();
|
||||
log("Using rule " + rule.getName(), Project.MSG_VERBOSE);
|
||||
}
|
||||
}
|
||||
|
||||
private void validate() throws BuildException {
|
||||
if (formatters.isEmpty() && !printToConsole) {
|
||||
throw new BuildException("No formatter specified; and printToConsole was false");
|
||||
@ -179,12 +177,6 @@ public class PMDTask extends Task {
|
||||
}
|
||||
}
|
||||
|
||||
private void printIfVerbose(String in) {
|
||||
if (verbose) {
|
||||
log(in);
|
||||
}
|
||||
}
|
||||
|
||||
private Path createClasspath() {
|
||||
if (classpath == null) {
|
||||
classpath = new Path(project);
|
||||
|
Reference in New Issue
Block a user