Author: mperham Date: Wed Jul 5 11:28:03 2006 New Revision: 419306 URL: http://svn.apache.org/viewvc?rev=419306&view=rev Log: PR: MPMD-33 Submitted by: Doug Douglass Do not fail build on PMD failures, include them in the report
Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java?rev=419306&r1=419305&r2=419306&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java (original) +++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java Wed Jul 5 11:28:03 2006 @@ -16,15 +16,14 @@ * limitations under the License. */ +import net.sourceforge.pmd.IRuleViolation; import net.sourceforge.pmd.PMD; -import net.sourceforge.pmd.PMDException; import net.sourceforge.pmd.Report; +import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleContext; import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSetFactory; -import net.sourceforge.pmd.TargetJDK1_3; -import net.sourceforge.pmd.TargetJDK1_4; -import net.sourceforge.pmd.TargetJDK1_5; +import net.sourceforge.pmd.SourceType; import net.sourceforge.pmd.renderers.CSVRenderer; import net.sourceforge.pmd.renderers.HTMLRenderer; import net.sourceforge.pmd.renderers.Renderer; @@ -188,44 +187,41 @@ { File file = (File) i.next(); - try - { - // TODO: lazily call beginFile in case there are no rules + + // TODO: lazily call beginFile in case there are no rules - reportSink.beginFile( file ); - ruleContext.setSourceCodeFilename( file.getAbsolutePath() ); - for ( int idx = 0; idx < rulesets.length; idx++ ) + reportSink.beginFile( file ); + ruleContext.setSourceCodeFilename( file.getAbsolutePath() ); + for ( int idx = 0; idx < rulesets.length; idx++ ) + { + try { - try - { - // PMD closes this Reader even though it did not open it so we have - // to open a new one with every call to processFile(). - Reader reader = hasEncoding ? new InputStreamReader( new FileInputStream( file ), - sourceEncoding ) - : new FileReader( file ); - pmd.processFile( reader, sets[idx], ruleContext ); - } - catch ( UnsupportedEncodingException e1 ) - { - throw new MavenReportException( "Encoding '" + sourceEncoding + "' is not supported.", e1 ); - } + // PMD closes this Reader even though it did not open it so we have + // to open a new one with every call to processFile(). + Reader reader = hasEncoding ? new InputStreamReader( new FileInputStream( file ), + sourceEncoding ) + : new FileReader( file ); + pmd.processFile( reader, sets[idx], ruleContext ); } - reportSink.endFile( file ); - } - catch ( PMDException e ) - { - Exception ex = e; - if ( e.getReason() != null ) + catch ( UnsupportedEncodingException e1 ) { - ex = e.getReason(); + throw new MavenReportException( "Encoding '" + sourceEncoding + "' is not supported.", e1 ); } - throw new MavenReportException( "Failure executing PMD for: " + file, ex ); - } - catch ( FileNotFoundException e ) - { - throw new MavenReportException( "Error opening source file: " + file, e ); + catch ( FileNotFoundException e2 ) + { + getLog().warn("Error opening source file: " + file); + reportSink.ruleViolationAdded(new ProcessingErrorRuleViolation(file, e2.getLocalizedMessage()) ); + } + catch ( Exception e3 ) + { + getLog().warn( "Failure executing PMD for: " + file, e3 ); + reportSink.ruleViolationAdded(new ProcessingErrorRuleViolation(file, e3.getLocalizedMessage()) ); + } + } + reportSink.endFile( file ); } + reportSink.endDocument(); if ( !isHtml() ) @@ -276,23 +272,21 @@ */ public PMD getPMD() { - PMD pmd; + PMD pmd = new PMD(); + if ( "1.5".equals( targetJdk ) ) { - pmd = new PMD( new TargetJDK1_5() ); + pmd.setJavaVersion(SourceType.JAVA_15); } else if ( "1.4".equals( targetJdk ) ) { - pmd = new PMD( new TargetJDK1_4() ); + pmd.setJavaVersion(SourceType.JAVA_14); } else if ( "1.3".equals( targetJdk ) ) { - pmd = new PMD( new TargetJDK1_3() ); - } - else - { - pmd = new PMD(); + pmd.setJavaVersion(SourceType.JAVA_13); } + return pmd; } @@ -417,5 +411,68 @@ } return renderer; + } + + /** + * @author <a href="mailto:[EMAIL PROTECTED]">Doug Douglass</a> + */ + private static class ProcessingErrorRuleViolation implements IRuleViolation { + + private String filename; + + private String description; + + public ProcessingErrorRuleViolation(File file, String description) { + filename = file.getPath(); + this.description = description; + } + + public String getFilename() { + return this.filename; + } + + public int getBeginLine() { + return 0; + } + + public int getBeginColumn() { + return 0; + } + + public int getEndLine() { + return 0; + } + + public int getEndColumn() { + return 0; + } + + public Rule getRule() { + return null; + } + + public String getDescription() { + return this.description; + } + + public String getPackageName() { + return null; + } + + public String getMethodName() { + return null; + } + + public String getClassName() { + return null; + } + + public boolean isSuppressed() { + return false; + } + + public String getVariableName() { + return null; + } } } Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java?rev=419306&r1=419305&r2=419306&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java (original) +++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java Wed Jul 5 11:28:03 2006 @@ -16,13 +16,6 @@ * limitations under the License. */ -import net.sourceforge.pmd.ReportListener; -import net.sourceforge.pmd.RuleViolation; -import net.sourceforge.pmd.IRuleViolation; -import net.sourceforge.pmd.stat.Metric; -import org.codehaus.doxia.sink.Sink; -import org.codehaus.plexus.util.StringUtils; - import java.io.File; import java.util.ArrayList; import java.util.Collections; @@ -31,6 +24,13 @@ import java.util.List; import java.util.ResourceBundle; +import net.sourceforge.pmd.IRuleViolation; +import net.sourceforge.pmd.ReportListener; +import net.sourceforge.pmd.stat.Metric; + +import org.codehaus.doxia.sink.Sink; +import org.codehaus.plexus.util.StringUtils; + /** * Handle events from PMD, converting them into Doxia events. * @@ -100,13 +100,14 @@ { public int compare( Object o1, Object o2 ) { - return ( (RuleViolation) o1 ).getBeginLine() - ( (RuleViolation) o2 ).getBeginLine(); + return ( (IRuleViolation) o1 ).getBeginLine() - + ( (IRuleViolation) o2 ).getBeginLine(); } } ); for ( Iterator it = violations.iterator(); it.hasNext(); ) { - RuleViolation ruleViolation = (RuleViolation) it.next(); + IRuleViolation ruleViolation = (IRuleViolation) it.next(); sink.tableRow(); sink.tableCell();