svn commit: r1811151 - in /maven/plugins/trunk/maven-pdf-plugin: pom.xml src/it/pdf-with-reporting-section/pom.xml src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1811151 - in /maven/plugins/trunk/maven-pdf-plugin: pom.xml src/it/pdf-with-reporting-section/pom.xml src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java

hboutemy
Author: hboutemy
Date: Wed Oct  4 21:48:51 2017
New Revision: 1811151

URL: http://svn.apache.org/viewvc?rev=1811151&view=rev
Log:
[MPDF-48] Make PDF plugin run reports when used with Maven 3
Submitted by: S├ębastien Garelle

Modified:
    maven/plugins/trunk/maven-pdf-plugin/pom.xml
    maven/plugins/trunk/maven-pdf-plugin/src/it/pdf-with-reporting-section/pom.xml
    maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java

Modified: maven/plugins/trunk/maven-pdf-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pdf-plugin/pom.xml?rev=1811151&r1=1811150&r2=1811151&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pdf-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-pdf-plugin/pom.xml Wed Oct  4 21:48:51 2017
@@ -96,7 +96,7 @@ under the License.
     <dependency>
       <groupId>org.apache.maven.reporting</groupId>
       <artifactId>maven-reporting-exec</artifactId>
-      <version>1.2</version>
+      <version>1.4-SNAPSHOT</version>
     </dependency>
   
     <!-- Maven -->
@@ -256,7 +256,7 @@ under the License.
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-pdf-plugin</artifactId>
-          <version>1.2</version>
+          <version>1.3</version>
         </plugin>
       </plugins>
     </pluginManagement>

Modified: maven/plugins/trunk/maven-pdf-plugin/src/it/pdf-with-reporting-section/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pdf-plugin/src/it/pdf-with-reporting-section/pom.xml?rev=1811151&r1=1811150&r2=1811151&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pdf-plugin/src/it/pdf-with-reporting-section/pom.xml (original)
+++ maven/plugins/trunk/maven-pdf-plugin/src/it/pdf-with-reporting-section/pom.xml Wed Oct  4 21:48:51 2017
@@ -61,11 +61,20 @@ under the License.
       </plugin>
       <plugin>
         <artifactId>maven-project-info-reports-plugin</artifactId>
-        <version>2.2</version>
+        <version>2.9</version>
         <reportSets>
           <reportSet>
             <reports>
               <report>cim</report>
+              <report>dependencies</report>
+              <report>dependency-info</report>
+              <report>dependency-management</report>
+              <report>help</report>
+              <report>issue-tracking</report>
+              <report>license</report>
+              <report>mailing-list</report>
+              <report>plugins</report>
+              <report>scm</report>
               <report>summary</report>
             </reports>
           </reportSet>

Modified: maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java?rev=1811151&r1=1811150&r2=1811151&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java (original)
+++ maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java Wed Oct  4 21:48:51 2017
@@ -41,8 +41,6 @@ import javax.swing.text.AttributeSet;
 import org.apache.commons.io.input.XmlStreamReader;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
-import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
@@ -75,21 +73,10 @@ import org.apache.maven.doxia.tools.Site
 import org.apache.maven.doxia.tools.SiteToolException;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.model.MailingList;
-import org.apache.maven.model.ReportPlugin;
-import org.apache.maven.model.ReportSet;
 import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.plugin.InvalidPluginException;
-import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugin.PluginConfigurationException;
 import org.apache.maven.plugin.PluginManager;
-import org.apache.maven.plugin.PluginManagerException;
-import org.apache.maven.plugin.PluginNotFoundException;
-import org.apache.maven.plugin.descriptor.MojoDescriptor;
-import org.apache.maven.plugin.descriptor.PluginDescriptor;
-import org.apache.maven.plugin.version.PluginVersionNotFoundException;
-import org.apache.maven.plugin.version.PluginVersionResolutionException;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
@@ -128,6 +115,14 @@ import org.codehaus.plexus.util.xml.pull
 public class PdfMojo
     extends AbstractMojo implements Contextualizable
 {
+
+    /**
+     * Workaround for reports that fail report generation
+     * @see MavenReport#generate(org.codehaus.doxia.sink.Sink, java.util.Locale)
+     */
+    private final String[] failingReportClassName =
+            { "DependenciesReport", "TeamListReport", "DependencyConvergenceReport" };
+
     /**
      * The vm line separator
      */
@@ -322,24 +317,19 @@ public class PdfMojo
     
     /**
      * Reports (Maven 2).
+     *
      * @since 1.3
      */
     @Parameter( defaultValue = "${reports}", required = true, readonly = true )
     private MavenReport[] reports;
     
     /**
-     * <p>Configuration section <b>used internally</b> by Maven 3.</p>
-     * <p>More details available here:
-     * <a href="http://maven.apache.org/plugins/maven-site-plugin/maven-3.html#Configuration_formats" target="_blank">
-     * http://maven.apache.org/plugins/maven-site-plugin/maven-3.html#Configuration_formats</a>
-     * </p>
-     * <p><b>Note:</b> using this field is not mandatory with Maven 3 as Maven core injects usual
-     * <code>&lt;reporting&gt;</code> section into this field.</p>
+     * Reports (Maven 3).
      *
-     * @since 1.3
+     * @since 1.4
      */
-    @Parameter( readonly = true )
-    private org.apache.maven.reporting.exec.ReportPlugin[] reportPlugins;
+    @Parameter( defaultValue = "${project.reporting.plugins}", readonly = true )
+    private org.apache.maven.model.ReportPlugin[] reportingPlugins;
 
     // ----------------------------------------------------------------------
     // Instance fields
@@ -808,7 +798,6 @@ public class PdfMojo
 
     /**
      * @return the available locales from <code>siteTool</code>.
-     * @see SiteTool#getAvailableLocales(String)
      */
     private List<Locale> getAvailableLocales()
     {
@@ -1006,57 +995,11 @@ public class PdfMojo
             getLog().info( "No report was specified." );
             return;
         }
-        
-        for ( final ReportPlugin reportPlugin : project.getReporting().getPlugins() )
-        {
-            final PluginDescriptor pluginDescriptor = getPluginDescriptor( reportPlugin );
 
-            if ( pluginDescriptor != null )
-            {
-                List<String> goals = new ArrayList<String>( 8 );
-                for ( final ReportSet reportSet : reportPlugin.getReportSets() )
-                {
-                    for ( String goal : reportSet.getReports() )
-                    {
-                        goals.add( goal );
-                    }
-                }
-    
-                List mojoDescriptors = pluginDescriptor.getMojos();
-                for ( Object mojoDescriptor1 : mojoDescriptors )
-                {
-                    final MojoDescriptor mojoDescriptor = (MojoDescriptor) mojoDescriptor1;
-
-                    if ( goals.isEmpty() || ( !goals.isEmpty() && goals.contains( mojoDescriptor.getGoal() ) ) )
-                    {
-                        MavenReport report = getMavenReport( mojoDescriptor );
-
-                        generateMavenReport( report, mojoDescriptor.getPluginDescriptor().getPluginArtifact(),
-                                             locale );
-                    }
-                }
-            }
-        }
-
-        // generate project-info report
-        if ( !getGeneratedMavenReports( locale ).isEmpty() )
+        List<MavenReportExecution> reports = getReports();
+        for ( MavenReportExecution report : reports )
         {
-            File outDir = new File( getGeneratedSiteDirectoryTmp(), "xdoc" );
-            if ( !locale.getLanguage().equals( defaultLocale.getLanguage() ) )
-            {
-                outDir = new File( new File( getGeneratedSiteDirectoryTmp(), locale.getLanguage() ), "xdoc" );
-            }
-            outDir.mkdirs();
-
-            File piReport = new File( outDir, "project-info.xml" );
-
-            StringWriter sw = new StringWriter();
-
-            PdfSink sink = new PdfSink( sw );
-            ProjectInfoRenderer r = new ProjectInfoRenderer( sink, getGeneratedMavenReports( locale ), i18n, locale );
-            r.render();
-
-            writeGeneratedReport( sw.toString(), piReport );
+            generateMavenReport( report.getMavenReport(), locale );
         }
 
         // copy generated site
@@ -1065,112 +1008,15 @@ public class PdfMojo
     }
 
     /**
-     * TODO olamy : remove when maven 3 will be the de facto standard :-)
-     * @param reportPlugin not null
-     * @return the PluginDescriptor instance for the given reportPlugin.
-     * @throws MojoExecutionException if any
-     * @since 1.1
-     */
-    private PluginDescriptor getPluginDescriptor( ReportPlugin reportPlugin )
-        throws MojoExecutionException
-    {
-        try
-        {
-            return pluginManager.verifyReportPlugin( reportPlugin, project, session );
-        }
-        catch ( ArtifactResolutionException e )
-        {
-            throw new MojoExecutionException( "ArtifactResolutionException: " + e.getMessage(), e );
-        }
-        catch ( ArtifactNotFoundException e )
-        {
-            throw new MojoExecutionException( "ArtifactNotFoundException: " + e.getMessage(), e );
-        }
-        catch ( PluginNotFoundException e )
-        {
-            throw new MojoExecutionException( "PluginNotFoundException: " + e.getMessage(), e );
-        }
-        catch ( PluginVersionResolutionException e )
-        {
-            throw new MojoExecutionException( "PluginVersionResolutionException: " + e.getMessage(), e );
-        }
-        catch ( InvalidVersionSpecificationException e )
-        {
-            throw new MojoExecutionException( "InvalidVersionSpecificationException: " + e.getMessage(), e );
-        }
-        catch ( InvalidPluginException e )
-        {
-            throw new MojoExecutionException( "InvalidPluginException: " + e.getMessage(), e );
-        }
-        catch ( PluginManagerException e )
-        {
-            throw new MojoExecutionException( "PluginManagerException: " + e.getMessage(), e );
-        }
-        catch ( PluginVersionNotFoundException e )
-        {
-            throw new MojoExecutionException( "PluginVersionNotFoundException: " + e.getMessage(), e );
-        }
-        catch ( NoSuchMethodError e )
-        {
-            getLog().info( "Ignoring api call removed in maven 3, no reports are generated!" );
-            getLog().debug( e );
-            return null;
-        }
-    }
-
-    /**
-     * @param mojoDescriptor not null
-     * @return the MavenReport instance for the given mojoDescriptor.
-     * @throws MojoExecutionException if any
-     * @since 1.1
-     */
-    private MavenReport getMavenReport( MojoDescriptor mojoDescriptor )
-        throws MojoExecutionException
-    {
-        ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
-        try
-        {
-            Thread.currentThread()
-                  .setContextClassLoader( mojoDescriptor.getPluginDescriptor().getClassRealm().getClassLoader() );
-
-            MojoExecution mojoExecution = new MojoExecution( mojoDescriptor );
-
-            return pluginManager.getReport( project, mojoExecution, session );
-        }
-        catch ( ArtifactNotFoundException e )
-        {
-            throw new MojoExecutionException( "ArtifactNotFoundException: " + e.getMessage(), e );
-        }
-        catch ( ArtifactResolutionException e )
-        {
-            throw new MojoExecutionException( "ArtifactResolutionException: " + e.getMessage(), e );
-        }
-        catch ( PluginConfigurationException e )
-        {
-            throw new MojoExecutionException( "PluginConfigurationException: " + e.getMessage(), e );
-        }
-        catch ( PluginManagerException e )
-        {
-            throw new MojoExecutionException( "PluginManagerException: " + e.getMessage(), e );
-        }
-        finally
-        {
-            Thread.currentThread().setContextClassLoader( oldClassLoader );
-        }
-    }
-
-    /**
      * Generate the given Maven report only if it is not an external report and the report could be generated.
      *
-     * @param mojoDescriptor not null, to catch linkage error
      * @param report could be null
      * @param locale not null
      * @throws IOException if any
      * @throws MojoExecutionException if any
-     * @see #isValidGeneratedReport(MojoDescriptor, File, String)
      * @since 1.1
      */
-    private void generateMavenReport( MavenReport report, Artifact pluginArtifact, Locale locale )
+    private void generateMavenReport( MavenReport report, Locale locale )
         throws IOException, MojoExecutionException
     {
         if ( report == null )
@@ -1179,6 +1025,16 @@ public class PdfMojo
         }
 
         String localReportName = report.getName( locale );
+
+        // Workaround for reporters that fail report generation
+        if ( skipFailingReport( report ) )
+        {
+            getLog().info( "Skipped \"" + localReportName + "\" report (not supported by pdf plugin)." );
+            getLog().debug( "Skipped report simple class name: " + report.getClass().getSimpleName() );
+
+            return;
+        }
+
         if ( !report.canGenerateReport() )
         {
             getLog().info( "Skipped \"" + localReportName + "\" report." );
@@ -1270,18 +1126,24 @@ public class PdfMojo
         }
 
         writeGeneratedReport( sw.toString(), generatedReport );
+        getGeneratedMavenReports( locale ).add( report );
+    }
 
-        if ( isValidGeneratedReport( pluginArtifact, generatedReport, localReportName ) )
+    private boolean skipFailingReport( MavenReport report )
+    {
+        for ( String className : failingReportClassName )
         {
-            getGeneratedMavenReports( locale ).add( report );
+            if ( className.equals( report.getClass().getSimpleName() ) )
+            {
+                return true;
+            }
         }
+        return false;
     }
 
     /**
      * @param locale not null
      * @return the generated reports
-     * @see #generateMavenReport(MojoDescriptor, MavenReport, Locale)
-     * @see #isValidGeneratedReport(MojoDescriptor, File, String)
      * @since 1.1
      */
     private List<MavenReport> getGeneratedMavenReports( Locale locale )
@@ -1446,7 +1308,7 @@ public class PdfMojo
     /**
      * Parsing the generated report to see if it is correct or not. Log the error for the user.
      *
-     * @param mojoDescriptor not null
+     * @param pluginArtifact not null
      * @param generatedReport not null
      * @param localReportName not null
      * @return <code>true</code> if Doxia is able to parse the generated report, <code>false</code> otherwise.
@@ -1564,7 +1426,7 @@ public class PdfMojo
     }
 
     /**
-     * @param pluginDescriptor not null
+     * @param pluginArtifact not null
      * @return the MavenProject for the current plugin descriptor or null if an error occurred.
      * @since 1.1
      */
@@ -1586,37 +1448,29 @@ public class PdfMojo
     protected List<MavenReportExecution> getReports()
         throws MojoExecutionException
     {
-        if ( isMaven3OrMore() )
+        if ( !isMaven3OrMore() )
         {
-            MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest();
-            mavenReportExecutorRequest.setLocalRepository( localRepository );
-            mavenReportExecutorRequest.setMavenSession( session );
-            mavenReportExecutorRequest.setProject( project );
-            mavenReportExecutorRequest.setReportPlugins( reportPlugins );
-
-            MavenReportExecutor mavenReportExecutor;
-            try
-            {
-                mavenReportExecutor = (MavenReportExecutor) container.lookup( MavenReportExecutor.class.getName() );
-            }
-            catch ( ComponentLookupException e )
-            {
-                throw new MojoExecutionException( "could not get MavenReportExecutor component", e );
-            }
-            return mavenReportExecutor.buildMavenReports( mavenReportExecutorRequest );
+            getLog().error( "Report generation is not supported with Maven <= 2.x" );
         }
 
-        List<MavenReportExecution> reportExecutions = new ArrayList<MavenReportExecution>( reports.length );
-        for ( MavenReport report : reports )
+        MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest();
+        mavenReportExecutorRequest.setLocalRepository( localRepository );
+        mavenReportExecutorRequest.setMavenSession( session );
+        mavenReportExecutorRequest.setProject( project );
+        mavenReportExecutorRequest.setReportPlugins( reportingPlugins );
+
+        MavenReportExecutor mavenReportExecutor;
+        try
         {
-            if ( report.canGenerateReport() )
-            {
-                reportExecutions.add( new MavenReportExecution( report ) );
-            }
+            mavenReportExecutor = (MavenReportExecutor) container.lookup( MavenReportExecutor.class.getName() );
+        }
+        catch ( ComponentLookupException e )
+        {
+            throw new MojoExecutionException( "could not get MavenReportExecutor component", e );
         }
-        return reportExecutions;
+        return mavenReportExecutor.buildMavenReports( mavenReportExecutorRequest );
     }
-    
+
     /**
      * Check the current Maven version to see if it's Maven 3.0 or newer.
      */