[MPMD-244] Maven PMD plugin fails but no reason is given for the failure

Added new parameter "showPmdLog", which helps in analyzing issues
within PMD. This integrates the PMD log into the maven log.
diff --git a/pom.xml b/pom.xml
index 499a099..2813959 100644
--- a/pom.xml
+++ b/pom.xml
@@ -142,6 +142,13 @@
       <version>${pmdVersion}</version>
     </dependency>
 
+    <!-- Support logging of PMD through slf4j. See also MPMD-244 -->
+    <dependency>
+        <groupId>org.slf4j</groupId>
+        <artifactId>jul-to-slf4j</artifactId>
+        <version>1.7.25</version>
+    </dependency>
+
     <!-- doxia -->
     <dependency>
       <groupId>org.apache.maven.doxia</groupId>
diff --git a/src/it/MPMD-244-logging/invoker.properties b/src/it/MPMD-244-logging/invoker.properties
new file mode 100644
index 0000000..1e595ef
--- /dev/null
+++ b/src/it/MPMD-244-logging/invoker.properties
@@ -0,0 +1,19 @@
+# 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.
+
+invoker.goals = clean pmd:check
+invoker.maven.version = 3+
diff --git a/src/it/MPMD-244-logging/logging-disabled/pom.xml b/src/it/MPMD-244-logging/logging-disabled/pom.xml
new file mode 100644
index 0000000..46f0e2b
--- /dev/null
+++ b/src/it/MPMD-244-logging/logging-disabled/pom.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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>
+
+  <parent>
+    <groupId>org.apache.maven.plugins.pmd.its</groupId>
+    <artifactId>MPMD-244-logging</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>MPMD-244-logging-disabled</artifactId>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+  </dependencies>
+
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>@project.groupId@</groupId>
+        <artifactId>@project.artifactId@</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <skipPmdError>true</skipPmdError>
+          <showPmdLog>false</showPmdLog>
+        </configuration>
+        <executions>
+          <execution>
+            <phase>test-compile</phase>
+            <goals>
+              <goal>check</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MPMD-244-logging/logging-disabled/src/main/java/BrokenFile.java b/src/it/MPMD-244-logging/logging-disabled/src/main/java/BrokenFile.java
new file mode 100644
index 0000000..1c5335a
--- /dev/null
+++ b/src/it/MPMD-244-logging/logging-disabled/src/main/java/BrokenFile.java
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+
+public class BrokenFile
+{
+
+    // This file has a parse error, so PMD will fail to parse it
+    broken!!
+
+}
diff --git a/src/it/MPMD-244-logging/logging-enabled/pom.xml b/src/it/MPMD-244-logging/logging-enabled/pom.xml
new file mode 100644
index 0000000..355992c
--- /dev/null
+++ b/src/it/MPMD-244-logging/logging-enabled/pom.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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>
+
+  <parent>
+    <groupId>org.apache.maven.plugins.pmd.its</groupId>
+    <artifactId>MPMD-244-logging</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>MPMD-244-logging-enabled</artifactId>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+  </dependencies>
+
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>@project.groupId@</groupId>
+        <artifactId>@project.artifactId@</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <skipPmdError>true</skipPmdError>
+          <showPmdLog>true</showPmdLog>
+        </configuration>
+        <executions>
+          <execution>
+            <phase>test-compile</phase>
+            <goals>
+              <goal>check</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MPMD-244-logging/logging-enabled/src/main/java/BrokenFile.java b/src/it/MPMD-244-logging/logging-enabled/src/main/java/BrokenFile.java
new file mode 100644
index 0000000..1c5335a
--- /dev/null
+++ b/src/it/MPMD-244-logging/logging-enabled/src/main/java/BrokenFile.java
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+
+public class BrokenFile
+{
+
+    // This file has a parse error, so PMD will fail to parse it
+    broken!!
+
+}
diff --git a/src/it/MPMD-244-logging/pom.xml b/src/it/MPMD-244-logging/pom.xml
new file mode 100644
index 0000000..6d9d3c1
--- /dev/null
+++ b/src/it/MPMD-244-logging/pom.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.plugins.pmd.its</groupId>
+  <artifactId>MPMD-244-logging</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <description>
+    Verify PMDs log output is present
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <modules>
+    <module>logging-disabled</module>
+    <module>logging-enabled</module>
+  </modules>
+</project>
diff --git a/src/it/MPMD-244-logging/verify.groovy b/src/it/MPMD-244-logging/verify.groovy
new file mode 100644
index 0000000..f213605
--- /dev/null
+++ b/src/it/MPMD-244-logging/verify.groovy
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+
+File buildLog = new File( basedir, 'build.log' )
+assert buildLog.exists()
+assert buildLog.text.contains( "PMD processing errors" )
+assert buildLog.text.contains( "Error while parsing" )
+String currentDir = basedir.getCanonicalPath()
+assert buildLog.text.contains( "net.sourceforge.pmd.PMDException: Error while parsing ${currentDir}/logging-enabled/src/main/java/BrokenFile.java" )
+assert ! buildLog.text.contains( "net.sourceforge.pmd.PMDException: Error while parsing ${currentDir}/logging-disabled/src/main/java/BrokenFile.java" )
diff --git a/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java b/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java
index 67a7761..ec2b056 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java
@@ -29,6 +29,9 @@
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.logging.SimpleFormatter;
 
 import net.sourceforge.pmd.PMD;
 
@@ -42,6 +45,7 @@
 import org.codehaus.plexus.util.PathTool;
 import org.codehaus.plexus.util.ReaderFactory;
 import org.codehaus.plexus.util.StringUtils;
+import org.slf4j.bridge.SLF4JBridgeHandler;
 
 /**
  * Base class for the PMD reports.
@@ -215,6 +219,17 @@
     @Parameter( property = "pmd.excludeFromFailureFile", defaultValue = "" )
     protected String excludeFromFailureFile;
 
+    /**
+     * Redirect PMD log into maven log out.
+     * When enabled, the PMD log output is redirected to maven, so that
+     * it is visible in the console together with all the other log output.
+     * Also, if maven is started with the debug flag (<code>-X</code> or <code>--debug</code>),
+     * the PMD logger is also configured for debug.
+     *
+     * @since 3.9.0
+     */
+    @Parameter( defaultValue = "true", property = "pmd.showPmdLog" )
+    protected boolean showPmdLog = true;
 
     /** The files that are being analyzed. */
     protected Map<File, PmdFileInfo> filesToProcess;
@@ -535,6 +550,25 @@
         return ( outputEncoding != null ) ? outputEncoding : ReaderFactory.UTF_8;
     }
 
+    protected void setupPmdLogging()
+    {
+        if ( !showPmdLog )
+        {
+            return;
+        }
+
+        Logger logger = Logger.getLogger( "net.sourceforge.pmd" );
+
+        SLF4JBridgeHandler handler = new SLF4JBridgeHandler();
+        SimpleFormatter formatter = new SimpleFormatter();
+        handler.setFormatter( formatter );
+        logger.setUseParentHandlers( false );
+        logger.addHandler( handler );
+        handler.setLevel( Level.ALL );
+        logger.setLevel( Level.ALL );
+        getLog().debug( "Configured jul-to-slf4j bridge for " + logger.getName() );
+    }
+
     static String getPmdVersion()
     {
         try
diff --git a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
index 92b9766..e943b24 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
@@ -230,6 +230,8 @@
             return;
         }
 
+        setupPmdLogging();
+
         Properties p = new Properties();
         if ( ignoreLiterals )
         {
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 9db12c4..0351988 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
@@ -320,6 +320,8 @@
     private void executePmd()
         throws MavenReportException
     {
+        setupPmdLogging();
+
         if ( renderer != null )
         {
             // PMD has already been run