[maven-pmd-plugin] branch MPMD-289 created (now 14ad821)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[maven-pmd-plugin] branch MPMD-289 created (now 14ad821)

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

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


      at 14ad821  [MPMD-289] - check: unable to find pmd.xml

This branch includes the following new commits:

     new 14ad821  [MPMD-289] - check: unable to find pmd.xml

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Reply | Threaded
Open this post in threaded view
|

[maven-pmd-plugin] 01/01: [MPMD-289] - check: unable to find pmd.xml

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

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

commit 14ad82114d41af5bf70502669f10b0a45770317f
Author: Andreas Dangel <[hidden email]>
AuthorDate: Sun Jan 12 12:23:26 2020 +0100

    [MPMD-289] - check: unable to find pmd.xml
   
    XML report is now created always, regardless of the format
    configured.
---
 .../MPMD-289-format-html-check/invoker.properties  | 18 +++++
 src/it/MPMD-289-format-html-check/pom.xml          | 69 +++++++++++++++++
 .../src/main/java/com/mycompany/app/App.java       | 25 ++++++
 src/it/MPMD-289-format-html-check/verify.groovy    | 34 +++++++++
 .../org/apache/maven/plugins/pmd/CpdReport.java    | 68 ++++++++++-------
 .../org/apache/maven/plugins/pmd/PmdReport.java    | 89 ++++++++++++++--------
 .../apache/maven/plugins/pmd/CpdReportTest.java    |  4 +-
 7 files changed, 247 insertions(+), 60 deletions(-)

diff --git a/src/it/MPMD-289-format-html-check/invoker.properties b/src/it/MPMD-289-format-html-check/invoker.properties
new file mode 100644
index 0000000..a5ed592
--- /dev/null
+++ b/src/it/MPMD-289-format-html-check/invoker.properties
@@ -0,0 +1,18 @@
+# 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 verify
diff --git a/src/it/MPMD-289-format-html-check/pom.xml b/src/it/MPMD-289-format-html-check/pom.xml
new file mode 100644
index 0000000..9d44fe0
--- /dev/null
+++ b/src/it/MPMD-289-format-html-check/pom.xml
@@ -0,0 +1,69 @@
+<?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-289-format-html-check</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <description>
+    Then report format is html, xml format should still be created so that the check goal still works.
+  </description>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-pmd-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <skipPmdError>false</skipPmdError>
+          <skip>false</skip>
+          <failOnViolation>true</failOnViolation>
+          <failurePriority>4</failurePriority>
+          <sourceEncoding>UTF-8</sourceEncoding>
+          <minimumTokens>100</minimumTokens>
+          <skipEmptyReport>false</skipEmptyReport>
+          <format>html</format>
+          <excludes>
+            <exclude>**/*Bean.java</exclude>
+            <exclude>**/generated/*.java</exclude>
+          </excludes>
+          <excludeRoots>
+            <excludeRoot>target/generated-sources/stubs</excludeRoot>
+          </excludeRoots>
+          <rulesets/>
+        </configuration>
+        <executions>
+          <execution>
+            <goals>
+              <goal>check</goal>
+              <goal>cpd-check</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
diff --git a/src/it/MPMD-289-format-html-check/src/main/java/com/mycompany/app/App.java b/src/it/MPMD-289-format-html-check/src/main/java/com/mycompany/app/App.java
new file mode 100644
index 0000000..8e4972d
--- /dev/null
+++ b/src/it/MPMD-289-format-html-check/src/main/java/com/mycompany/app/App.java
@@ -0,0 +1,25 @@
+package com.mycompany.app;
+
+/*
+ * 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 App
+{
+
+}
diff --git a/src/it/MPMD-289-format-html-check/verify.groovy b/src/it/MPMD-289-format-html-check/verify.groovy
new file mode 100644
index 0000000..04afaae
--- /dev/null
+++ b/src/it/MPMD-289-format-html-check/verify.groovy
@@ -0,0 +1,34 @@
+
+/*
+ * 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()
+
+File pmdXml = new File( basedir, 'target/pmd.xml' )
+assert pmdXml.exists()
+
+File pmdHtmlReport = new File ( basedir, 'target/site/pmd.html' )
+assert pmdHtmlReport.exists()
+
+File cpdXml = new File( basedir, 'target/cpd.xml' )
+assert cpdXml.exists()
+
+File cpdHtmlReport = new File ( basedir, 'target/site/cpd.html' )
+assert cpdHtmlReport.exists()
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 9a1037c..d9fb606 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
@@ -51,8 +51,8 @@ import net.sourceforge.pmd.cpd.JavaTokenizer;
 import net.sourceforge.pmd.cpd.Language;
 import net.sourceforge.pmd.cpd.LanguageFactory;
 import net.sourceforge.pmd.cpd.Match;
-import net.sourceforge.pmd.cpd.Renderer;
 import net.sourceforge.pmd.cpd.XMLRenderer;
+import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
 
 /**
  * Creates a report for PMD's CPD tool. See
@@ -168,12 +168,7 @@ public class CpdReport
             {
                 Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
 
-                generateReport( locale );
-
-                if ( !isHtml() && !isXml() )
-                {
-                    writeNonHtml( cpd );
-                }
+                generateMavenSiteReport( locale );
             }
             finally
             {
@@ -313,11 +308,14 @@ public class CpdReport
         cpd.go();
         getLog().debug( "CPD finished." );
 
-        // if format is XML, we need to output it even if the file list is empty or we have no duplications
+        // always create XML format. we need to output it even if the file list is empty or we have no duplications
         // so the "check" goals can check for violations
-        if ( isXml() )
+        writeXmlReport( cpd );
+
+        // html format is handled by maven site report, xml format as already bean rendered
+        if ( !isHtml() && !isXml() )
         {
-            writeNonHtml( cpd );
+            writeFormattedReport( cpd );
         }
     }
 
@@ -345,7 +343,7 @@ public class CpdReport
         return filteredMatches.iterator();
     }
 
-    private void generateReport( Locale locale )
+    private void generateMavenSiteReport( Locale locale )
     {
         CpdReportGenerator gen = new CpdReportGenerator( getSink(), filesToProcess, getBundle( locale ), aggregate );
         Iterator<Match> matches = cpd.getMatches();
@@ -373,35 +371,51 @@ public class CpdReport
         return encoding;
     }
 
-    void writeNonHtml( CPD cpd )
+    private void writeFormattedReport( CPD cpd )
         throws MavenReportException
     {
-        Renderer r = createRenderer();
+        CPDRenderer r = createRenderer();
+        writeReport( cpd, r, format );
+
+    }
 
+    void writeXmlReport( CPD cpd ) throws MavenReportException
+    {
+        File targetFile = writeReport( cpd, new XMLRenderer( getOutputEncoding() ), "xml" );
+        if ( includeXmlInSite )
+        {
+            File siteDir = getReportOutputDirectory();
+            siteDir.mkdirs();
+            try
+            {
+                FileUtils.copyFile( targetFile, new File( siteDir, "cpd.xml" ) );
+            }
+            catch ( IOException e )
+            {
+                throw new MavenReportException( e.getMessage(), e );
+            }
+        }
+    }
+
+    private File writeReport( CPD cpd, CPDRenderer r, String extension ) throws MavenReportException
+    {
         if ( r == null )
         {
-            return;
+            return null;
         }
 
-        String buffer = r.render( filterMatches( cpd.getMatches() ) );
-        File targetFile = new File( targetDirectory, "cpd." + format );
+        File targetFile = new File( targetDirectory, "cpd." + extension );
         targetDirectory.mkdirs();
         try ( Writer writer = new OutputStreamWriter( new FileOutputStream( targetFile ), getOutputEncoding() ) )
         {
-            writer.write( buffer );
+            r.render( filterMatches( cpd.getMatches() ), writer );
             writer.flush();
-
-            if ( includeXmlInSite )
-            {
-                File siteDir = getReportOutputDirectory();
-                siteDir.mkdirs();
-                FileUtils.copyFile( targetFile, new File( siteDir, "cpd." + format ) );
-            }
         }
         catch ( IOException ioe )
         {
             throw new MavenReportException( ioe.getMessage(), ioe );
         }
+        return targetFile;
     }
 
     /**
@@ -423,10 +437,10 @@ public class CpdReport
      * @return the renderer based on the configured output
      * @throws org.apache.maven.reporting.MavenReportException if no renderer found for the output type
      */
-    public Renderer createRenderer()
+    public CPDRenderer createRenderer()
         throws MavenReportException
     {
-        Renderer renderer = null;
+        CPDRenderer renderer = null;
         if ( "xml".equals( format ) )
         {
             renderer = new XMLRenderer( getOutputEncoding() );
@@ -439,7 +453,7 @@ public class CpdReport
         {
             try
             {
-                renderer = (Renderer) Class.forName( format ).newInstance();
+                renderer = (CPDRenderer) Class.forName( format ).getConstructor().newInstance();
             }
             catch ( Exception e )
             {
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 3ab36ef..0c59b3d 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
@@ -316,12 +316,7 @@ public class PmdReport
             {
                 Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
 
-                Report report = generateReport( locale );
-
-                if ( !isHtml() && !isXml() )
-                {
-                    writeNonHtml( report );
-                }
+                generateMavenSiteReport( locale );
             }
             finally
             {
@@ -491,11 +486,22 @@ public class PmdReport
 
         removeExcludedViolations( renderer.getViolations() );
 
-        // if format is XML, we need to output it even if the file list is empty or we have no violations
+        // 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
-        if ( isXml() && renderer != null )
+        if ( renderer != null )
         {
-            writeNonHtml( renderer.asReport() );
+            Report report = renderer.asReport();
+            writeXmlReport( report );
+
+            // write any other format except for xml and html. xml as been just produced.
+            // html format is produced by the maven site formatter. Excluding html here
+            // avoids usind PMD's own html formatter, which doesn't fit into the maven site
+            // considering the html/css styling
+            if ( !isHtml() && !isXml() )
+            {
+                writeFormattedReport( report );
+            }
         }
 
         if ( benchmark )
@@ -569,7 +575,7 @@ public class PmdReport
         }
     }
 
-    private Report generateReport( Locale locale )
+    private void generateMavenSiteReport( Locale locale )
         throws MavenReportException
     {
         Sink sink = getSink();
@@ -593,8 +599,6 @@ public class PmdReport
         {
             getLog().warn( "Failure creating the report: " + e.getLocalizedMessage(), e );
         }
-
-        return renderer.asReport();
     }
 
     /**
@@ -630,23 +634,14 @@ public class PmdReport
         return loc;
     }
 
-    /**
-     * Use the PMD renderers to render in any format aside from HTML.
-     *
-     * @param report
-     * @throws MavenReportException
-     */
-    private void writeNonHtml( Report report )
-        throws MavenReportException
+    private File writeReport( Report report, Renderer r, String extension ) throws MavenReportException
     {
-        Renderer r = createRenderer();
-
         if ( r == null )
         {
-            return;
+            return null;
         }
 
-        File targetFile = new File( targetDirectory, "pmd." + format );
+        File targetFile = new File( targetDirectory, "pmd." + extension );
         try ( Writer writer = new OutputStreamWriter( new FileOutputStream( targetFile ), getOutputEncoding() ) )
         {
             targetDirectory.mkdirs();
@@ -656,18 +651,50 @@ public class PmdReport
             r.renderFileReport( report );
             r.end();
             r.flush();
-
-            if ( includeXmlInSite )
-            {
-                File siteDir = getReportOutputDirectory();
-                siteDir.mkdirs();
-                FileUtils.copyFile( targetFile, new File( siteDir, "pmd." + format ) );
-            }
         }
         catch ( IOException ioe )
         {
             throw new MavenReportException( ioe.getMessage(), ioe );
         }
+
+        return targetFile;
+    }
+
+    /**
+     * Use the PMD renderers to render in any format aside from HTML and XML.
+     *
+     * @param report
+     * @throws MavenReportException
+     */
+    private void writeFormattedReport( Report report )
+        throws MavenReportException
+    {
+        Renderer r = createRenderer();
+        writeReport( report, r, format );
+    }
+
+    /**
+     * Use the PMD XML renderer to create the XML report format used by the check mojo later on.
+     *
+     * @param report
+     * @throws MavenReportException
+     */
+    private void writeXmlReport( Report report ) throws MavenReportException
+    {
+        File targetFile = writeReport( report, new XMLRenderer( getOutputEncoding() ), "xml" );
+        if ( includeXmlInSite )
+        {
+            File siteDir = getReportOutputDirectory();
+            siteDir.mkdirs();
+            try
+            {
+                FileUtils.copyFile( targetFile, new File( siteDir, "pmd.xml" ) );
+            }
+            catch ( IOException e )
+            {
+                throw new MavenReportException( e.getMessage(), e );
+            }
+        }
     }
 
     /**
diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
index 2dac659..e64d532 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
@@ -228,7 +228,7 @@ public class CpdReportTest
 
         String duplicatedCodeFragment = "// ----- duplicated code example -----";
         CPD tCpd = prepareMockCpd( duplicatedCodeFragment );
-        mojo.writeNonHtml( tCpd );
+        mojo.writeXmlReport( tCpd );
 
         File tReport = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
 
@@ -257,7 +257,7 @@ public class CpdReportTest
 
         String duplicatedCodeFragment = "// ----- duplicated code example -----";
         CPD tCpd = prepareMockCpd( duplicatedCodeFragment );
-        mojo.writeNonHtml( tCpd );
+        mojo.writeXmlReport( tCpd );
 
         File tReport = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
         assertTrue( FileUtils.fileExists( tReport.getAbsolutePath() ) );