You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ad...@apache.org on 2017/12/18 19:14:40 UTC

[maven-pmd-plugin] 02/05: [MPMD-246] Output details of processing errors

This is an automated email from the ASF dual-hosted git repository.

adangel pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-pmd-plugin.git

commit f9c4942d2a49441b13bc5df83c632bad78fcb73f
Author: Andreas Dangel <ad...@apache.org>
AuthorDate: Fri Dec 15 19:57:09 2017 +0100

    [MPMD-246] Output details of processing errors
    
    Output detail error message when debug logging is enabled.
    In any case, add the processing errors to the xml report.
---
 .../maven/plugins/pmd/PmdCollectingRenderer.java   | 11 ++++-
 .../org/apache/maven/plugins/pmd/PmdReport.java    |  4 +-
 .../apache/maven/plugins/pmd/PmdReportTest.java    | 31 ++++++++++++++
 .../pmd-processing-error-skip-plugin-config.xml    | 49 ++++++++++++++++++++++
 4 files changed, 92 insertions(+), 3 deletions(-)

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 9402639..e55b779 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/PmdCollectingRenderer.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/PmdCollectingRenderer.java
@@ -104,14 +104,19 @@ public class PmdCollectingRenderer extends AbstractRenderer
 
     /**
      * Gets the errors as a single string. Each error is in its own line.
+     * @param withDetails if <code>true</code> then add the error details additionally (contains e.g. the stacktrace)
      * @return the errors as string
      */
-    public String getErrorsAsString()
+    public String getErrorsAsString( boolean withDetails )
     {
         List<String> errorsAsString = new ArrayList<>( errors.size() );
         for ( ProcessingError error : errors )
         {
             errorsAsString.add( error.getFile() + ": " + error.getMsg() );
+            if ( withDetails )
+            {
+                errorsAsString.add( error.getDetail() );
+            }
         }
         return StringUtils.join( errorsAsString.toArray(), System.getProperty( "line.separator" ) );
     }
@@ -127,6 +132,10 @@ public class PmdCollectingRenderer extends AbstractRenderer
         {
             report.addRuleViolation( v );
         }
+        for ( ProcessingError e : errors )
+        {
+            report.addError( e );
+        }
         return report;
     }
 
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 0351988..9b63b9d 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
@@ -422,11 +422,11 @@ public class PmdReport
             if ( !skipPmdError )
             {
                 getLog().error( "PMD processing errors:" );
-                getLog().error( renderer.getErrorsAsString() );
+                getLog().error( renderer.getErrorsAsString( getLog().isDebugEnabled() ) );
                 throw new MavenReportException( "Found " + renderer.getErrors().size() + " PMD processing errors" );
             }
             getLog().warn( "There are " + renderer.getErrors().size() + " PMD processing errors:" );
-            getLog().warn( renderer.getErrorsAsString() );
+            getLog().warn( renderer.getErrorsAsString( getLog().isDebugEnabled() ) );
         }
 
         removeExcludedViolations( renderer.getViolations() );
diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
index 2ac342a..ee3bd1e 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
@@ -20,9 +20,11 @@ package org.apache.maven.plugins.pmd;
  */
 
 import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.PrintStream;
 import java.net.ServerSocket;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
@@ -467,6 +469,35 @@ public class PmdReportTest
         }
     }
 
+    public void testPMDProcessingErrorWithDetailsSkipped()
+            throws Exception
+    {
+        File testPom = new File( getBasedir(),
+                "src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml" );
+        PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
+
+        PrintStream originalOut = System.out;
+        ByteArrayOutputStream logging = new ByteArrayOutputStream();
+        System.setOut( new PrintStream( logging ) );
+
+        try {
+            mojo.execute();
+            String output = logging.toString();
+            assertTrue ( output.contains( "There are 1 PMD processing errors:" ) );
+
+            File generatedFile = new File( getBasedir(), "target/test/unit/parse-error/target/pmd.xml" );
+            assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+            String str = readFile( new File( getBasedir(), "target/test/unit/parse-error/target/pmd.xml" ) );
+
+            assertTrue( str.contains( "Error while parsing" ) );
+            assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) );
+
+        } finally {
+            System.setOut( originalOut );
+            System.out.println( logging.toString() );
+        }
+    }
+
     public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception {
         File testPom = new File(getBasedir(), "src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml");
         PmdReport mojo = (PmdReport) lookupMojo ("pmd", testPom);
diff --git a/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml b/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml
new file mode 100644
index 0000000..faa30c1
--- /dev/null
+++ b/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml
@@ -0,0 +1,49 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>parse.error.configuration</groupId>
+  <artifactId>parse-error-configuration</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <inceptionYear>2006</inceptionYear>
+  <name>Maven PMD Plugin Parse Error Test</name>
+  <url>http://maven.apache.org</url>
+  <build>
+    <finalName>parse-error</finalName>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-pmd-plugin</artifactId>
+        <configuration>
+          <project implementation="org.apache.maven.plugins.pmd.stubs.DefaultConfigurationMavenProjectStub"/>
+          <outputDirectory>${basedir}/target/test/unit/parse-error/target/site</outputDirectory>
+          <targetDirectory>${basedir}/target/test/unit/parse-error/target</targetDirectory>
+          <format>xml</format>
+          <sourceEncoding>UTF-8</sourceEncoding>
+          <skipPmdError>true</skipPmdError>
+          <compileSourceRoots>
+            <compileSourceRoot>${basedir}/src/test/resources/unit/processing-error/src</compileSourceRoot>
+          </compileSourceRoots>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

-- 
To stop receiving notification emails like this one, please contact
"commits@maven.apache.org" <co...@maven.apache.org>.