diff --git a/src/main/java/org/apache/maven/plugins/pmd/PmdCollectingRenderer.java b/src/main/java/org/apache/maven/plugins/pmd/PmdCollectingRenderer.java index 12ada600..d95f54cf 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/PmdCollectingRenderer.java +++ b/src/main/java/org/apache/maven/plugins/pmd/PmdCollectingRenderer.java @@ -41,7 +41,9 @@ * from a pmd execution. * * @author Andreas Dangel + * @deprecated not used anymore */ +@Deprecated public class PmdCollectingRenderer extends AbstractRenderer { private List errors = Collections.synchronizedList( new ArrayList<>() ); diff --git a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java index 8ccffd0c..3966421e 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java +++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java @@ -434,7 +434,7 @@ protected String getSourceEncoding() * @return comma separated list of absolute file paths of ruleset files * @throws MavenReportException if a ruleset could not be found */ - private String resolveRulesets() throws MavenReportException + private List resolveRulesets() throws MavenReportException { // configure ResourceManager - will search for urls (URLResourceLoader) and files in various directories: // in the directory of the current project's pom file - note: extensions might replace the pom file on the fly @@ -466,7 +466,7 @@ private String resolveRulesets() throws MavenReportException { throw new MavenReportException( e.getMessage(), e ); } - return StringUtils.join( sets, "," ); + return Arrays.asList( sets ); } private String determineRulesetFilename( String ruleset ) diff --git a/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java b/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java index 10a09c3c..cdd12e7d 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java +++ b/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java @@ -29,25 +29,20 @@ import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; import java.util.List; import java.util.Objects; -import org.apache.commons.lang3.StringUtils; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.pmd.ExcludeViolationsFromFile; -import org.apache.maven.plugins.pmd.PmdCollectingRenderer; import org.apache.maven.reporting.MavenReportException; import org.codehaus.plexus.util.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.PmdAnalysis; import net.sourceforge.pmd.PMDConfiguration; import net.sourceforge.pmd.Report; import net.sourceforge.pmd.RulePriority; -import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSetLoadException; import net.sourceforge.pmd.RuleSetLoader; import net.sourceforge.pmd.RuleViolation; @@ -63,8 +58,7 @@ import net.sourceforge.pmd.renderers.Renderer; import net.sourceforge.pmd.renderers.TextRenderer; import net.sourceforge.pmd.renderers.XMLRenderer; -import net.sourceforge.pmd.util.datasource.DataSource; -import net.sourceforge.pmd.util.datasource.FileDataSource; +import net.sourceforge.pmd.util.Predicate; /** * Executes PMD with the configuration provided via {@link PmdRequest}. @@ -146,13 +140,13 @@ private static PmdResult fork( PmdRequest request ) /** * Execute PMD analysis from CLI. - * + * *

* Single arg with the filename to the serialized {@link PmdRequest}. - * + * *

* Exit-code: 0 = success, 1 = failure in executing - * + * * @param args */ public static void main( String[] args ) @@ -211,14 +205,9 @@ private PmdResult run() throws MavenReportException { configuration.setSourceEncoding( request.getSourceEncoding() ); } - try - { - configuration.prependClasspath( request.getAuxClasspath() ); - } - catch ( IOException e ) - { - throw new MavenReportException( e.getMessage(), e ); - } + + configuration.prependAuxClasspath( request.getAuxClasspath() ); + if ( request.getSuppressMarker() != null ) { configuration.setSuppressMarker( request.getSuppressMarker() ); @@ -240,15 +229,10 @@ private PmdResult run() throws MavenReportException configuration.setBenchmark( true ); } List files = request.getFiles(); - List dataSources = new ArrayList<>( files.size() ); - for ( File f : files ) - { - dataSources.add( new FileDataSource( f ) ); - } - PmdCollectingRenderer renderer = new PmdCollectingRenderer(); + Report report = null; - if ( StringUtils.isBlank( request.getRulesets() ) ) + if ( request.getRulesets().isEmpty() ) { LOG.debug( "Skipping PMD execution as no rulesets are defined." ); } @@ -261,7 +245,7 @@ private PmdResult run() throws MavenReportException try { - processFilesWithPMD( configuration, dataSources, renderer ); + report = processFilesWithPMD( configuration, files ); } finally { @@ -290,21 +274,21 @@ private PmdResult run() throws MavenReportException } } - if ( renderer.hasErrors() ) + if ( report != null && !report.getProcessingErrors().isEmpty() ) { + List errors = report.getProcessingErrors(); if ( !request.isSkipPmdError() ) { LOG.error( "PMD processing errors:" ); - LOG.error( renderer.getErrorsAsString( request.isDebugEnabled() ) ); - throw new MavenReportException( "Found " + renderer.getErrors().size() + " PMD processing errors" ); + LOG.error( getErrorsAsString( errors, request.isDebugEnabled() ) ); + throw new MavenReportException( "Found " + errors.size() + + " PMD processing errors" ); } - LOG.warn( "There are {} PMD processing errors:", renderer.getErrors().size() ); - LOG.warn( renderer.getErrorsAsString( request.isDebugEnabled() ) ); + LOG.warn( "There are {} PMD processing errors:", errors.size() ); + LOG.warn( getErrorsAsString( errors, request.isDebugEnabled() ) ); } - removeExcludedViolations( renderer.getViolations() ); - - Report report = renderer.asReport(); + report = removeExcludedViolations( report ); // always write XML report, as this might be needed by the check mojo // we need to output it even if the file list is empty or we have no violations // so the "check" goals can check for violations @@ -323,6 +307,25 @@ private PmdResult run() throws MavenReportException return new PmdResult( new File( request.getTargetDirectory(), "pmd.xml" ), request.getOutputEncoding() ); } + /** + * Gets the errors as a single string. Each error is in its own line. + * @param withDetails if true then add the error details additionally (contains e.g. the stacktrace) + * @return the errors as string + */ + private String getErrorsAsString( List errors, boolean withDetails ) + { + List errorsAsString = new ArrayList<>( errors.size() ); + for ( Report.ProcessingError error : errors ) + { + errorsAsString.add( error.getFile() + ": " + error.getMsg() ); + if ( withDetails ) + { + errorsAsString.add( error.getDetail() ); + } + } + return String.join( System.lineSeparator(), errorsAsString ); + } + private void writeBenchmarkReport( TimingReport timingReport, String benchmarkOutputLocation, String encoding ) { try ( Writer writer = new OutputStreamWriter( new FileOutputStream( benchmarkOutputLocation ), encoding ) ) @@ -336,28 +339,31 @@ private void writeBenchmarkReport( TimingReport timingReport, String benchmarkOu } } - private void processFilesWithPMD( PMDConfiguration pmdConfiguration, List dataSources, - PmdCollectingRenderer renderer ) throws MavenReportException + private Report processFilesWithPMD( PMDConfiguration pmdConfiguration, List files ) + throws MavenReportException { + Report report = null; RuleSetLoader rulesetLoader = RuleSetLoader.fromPmdConfig( pmdConfiguration ) .warnDeprecated( true ); - List rulesets; try { // load the ruleset once to log out any deprecated rules as warnings - rulesets = rulesetLoader.loadFromResources( - Arrays.asList( pmdConfiguration.getRuleSets().split( "\\s*,\\s*" ) ) ); + rulesetLoader.loadFromResources( pmdConfiguration.getRuleSetPaths() ); } catch ( RuleSetLoadException e1 ) { throw new MavenReportException( "The ruleset could not be loaded", e1 ); } - try + try ( PmdAnalysis pmdAnalysis = PmdAnalysis.create( pmdConfiguration ) ) { + for ( File file : files ) + { + pmdAnalysis.files().addFile( file.toPath() ); + } LOG.debug( "Executing PMD..." ); - PMD.processFiles( pmdConfiguration, rulesets, dataSources, Arrays.asList( renderer ) ); - LOG.debug( "PMD finished. Found {} violations.", renderer.getViolations().size() ); + report = pmdAnalysis.performAnalysisAndCollectReport(); + LOG.debug( "PMD finished. Found {} violations.", report.getViolations().size() ); } catch ( Exception e ) { @@ -367,7 +373,9 @@ private void processFilesWithPMD( PMDConfiguration pmdConfiguration, List violations ) + private Report removeExcludedViolations( Report report ) throws MavenReportException { + if ( report == null ) + { + return null; + } + ExcludeViolationsFromFile excludeFromFile = new ExcludeViolationsFromFile(); try @@ -496,19 +512,19 @@ private void removeExcludedViolations( List violations ) LOG.debug( "Removing excluded violations. Using {} configured exclusions.", excludeFromFile.countExclusions() ); - int violationsBefore = violations.size(); + int violationsBefore = report.getViolations().size(); - Iterator iterator = violations.iterator(); - while ( iterator.hasNext() ) + Report filtered = report.filterViolations( new Predicate() { - RuleViolation rv = iterator.next(); - if ( excludeFromFile.isExcludedFromFailure( rv ) ) + @Override + public boolean test( RuleViolation ruleViolation ) { - iterator.remove(); + return !excludeFromFile.isExcludedFromFailure( ruleViolation ); } - } + } ); - int numberOfExcludedViolations = violationsBefore - violations.size(); + int numberOfExcludedViolations = violationsBefore - filtered.getViolations().size(); LOG.debug( "Excluded {} violations.", numberOfExcludedViolations ); + return filtered; } } diff --git a/src/main/java/org/apache/maven/plugins/pmd/exec/PmdRequest.java b/src/main/java/org/apache/maven/plugins/pmd/exec/PmdRequest.java index 276f495e..09b4e27b 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/exec/PmdRequest.java +++ b/src/main/java/org/apache/maven/plugins/pmd/exec/PmdRequest.java @@ -45,7 +45,7 @@ public class PmdRequest implements Serializable private String auxClasspath; private String suppressMarker; private String analysisCacheLocation; - private String rulesets; + private List rulesets; private String sourceEncoding; private List files = new ArrayList<>(); @@ -114,7 +114,7 @@ public void setAnalysisCacheLocation( String analysisCacheLocation ) this.analysisCacheLocation = analysisCacheLocation; } - public void setRulesets( String rulesets ) + public void setRulesets( List rulesets ) { this.rulesets = rulesets; } @@ -223,7 +223,7 @@ public String getAnalysisCacheLocation() return analysisCacheLocation; } - public String getRulesets() + public List getRulesets() { return rulesets; } diff --git a/src/site/markdown/releasenotes.md b/src/site/markdown/releasenotes.md index dfb6169f..74f455ea 100644 --- a/src/site/markdown/releasenotes.md +++ b/src/site/markdown/releasenotes.md @@ -39,6 +39,9 @@ under the License. ### 📝 Documentation updates * [MPMD-333](https://issues.apache.org/jira/browse/MPMD-333) - Add release notes documentation +### 👻 Maintenance +* [MPMD-336](https://issues.apache.org/jira/browse/MPMD-336) - Replace deprecated calls to PMD + ### 📦 Dependency updates * [MPMD-329](https://issues.apache.org/jira/browse/MPMD-329) - Upgrade to PMD 6.45.0 * [MPMD-330](https://issues.apache.org/jira/browse/MPMD-330) - Upgrade Maven Parent to 35 @@ -50,7 +53,7 @@ under the License. **Release Date:** 2022-02-05 **JIRA:** [Release Notes - Maven PMD Plugin - Version 3.16.0](https://issues.apache.org/jira/projects/MPMD/versions/12350599) - + **GitHub:** ### 🐛 Bug Fixes @@ -70,7 +73,7 @@ under the License. ## Version 3.15.0 **Release Date:** 2021-09-06 - + **JIRA:** [Release Notes - Maven PMD Plugin - Version 3.15.0](https://issues.apache.org/jira/projects/MPMD/versions/12349432) ### 🐛 Bug Fixes @@ -98,7 +101,7 @@ under the License. ## Version 3.14.0 **Release Date:** 2020-10-24 - + **JIRA:** [Release Notes - Maven PMD Plugin - Version 3.14.0](https://issues.apache.org/jira/projects/MPMD/versions/12346940) ### 🐛 Bug Fixes @@ -119,7 +122,7 @@ under the License. ## Version 3.13.0 **Release Date:** 2020-01-25 - + **JIRA:** [Release Notes - Maven PMD Plugin - Version 3.13.0](https://issues.apache.org/jira/projects/MPMD/versions/12345409) ### 🐛 Bug Fixes @@ -147,7 +150,7 @@ under the License. ## Version 3.12.0 **Release Date:** 2019-04-11 - + **JIRA:** [Release Notes - Maven PMD Plugin - Version 3.12.0](https://issues.apache.org/jira/projects/MPMD/versions/12344380) ### 🐛 Bug Fixes