View Javadoc
1   package org.apache.maven.plugins.site.render;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.doxia.site.decoration.DecorationModel;
24  import org.apache.maven.doxia.site.decoration.Menu;
25  import org.apache.maven.doxia.site.decoration.MenuItem;
26  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
27  import org.apache.maven.doxia.siterenderer.Renderer;
28  import org.apache.maven.doxia.siterenderer.RendererException;
29  import org.apache.maven.doxia.siterenderer.RenderingContext;
30  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
31  import org.apache.maven.doxia.tools.SiteToolException;
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.model.ReportPlugin;
34  import org.apache.maven.model.Reporting;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugin.MojoFailureException;
37  import org.apache.maven.plugin.descriptor.PluginDescriptor;
38  import org.apache.maven.plugins.annotations.Component;
39  import org.apache.maven.plugins.annotations.Parameter;
40  import org.apache.maven.plugins.site.descriptor.AbstractSiteDescriptorMojo;
41  import org.apache.maven.reporting.MavenReport;
42  import org.apache.maven.reporting.exec.MavenReportExecution;
43  import org.apache.maven.reporting.exec.MavenReportExecutor;
44  import org.apache.maven.reporting.exec.MavenReportExecutorRequest;
45  import org.codehaus.plexus.util.ReaderFactory;
46  import org.codehaus.plexus.util.StringUtils;
47  
48  import java.io.File;
49  import java.io.IOException;
50  import java.util.ArrayList;
51  import java.util.Collection;
52  import java.util.HashMap;
53  import java.util.Iterator;
54  import java.util.LinkedHashMap;
55  import java.util.List;
56  import java.util.Locale;
57  import java.util.Map;
58  
59  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
60  
61  /**
62   * Base class for site rendering mojos.
63   *
64   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
65   *
66   */
67  public abstract class AbstractSiteRenderingMojo extends AbstractSiteDescriptorMojo
68  {
69      /**
70       * Module type exclusion mappings
71       * ex: <code>fml  -> **&#47;*-m1.fml</code>  (excludes fml files ending in '-m1.fml' recursively)
72       * <p/>
73       * The configuration looks like this:
74       * <pre>
75       *   &lt;moduleExcludes&gt;
76       *     &lt;moduleType&gt;filename1.ext,**&#47;*sample.ext&lt;/moduleType&gt;
77       *     &lt;!-- moduleType can be one of 'apt', 'fml' or 'xdoc'. --&gt;
78       *     &lt;!-- The value is a comma separated list of           --&gt;
79       *     &lt;!-- filenames or fileset patterns.                   --&gt;
80       *     &lt;!-- Here's an example:                               --&gt;
81       *     &lt;xdoc&gt;changes.xml,navigation.xml&lt;/xdoc&gt;
82       *   &lt;/moduleExcludes&gt;
83       * </pre>
84       */
85      @Parameter
86      private Map<String, String> moduleExcludes;
87  
88      /**
89       * The location of a Velocity template file to use. When used, skins and the default templates, CSS and images
90       * are disabled. It is highly recommended that you package this as a skin instead.
91       *
92       * @since 2.0-beta-5
93       */
94      @Parameter( property = "templateFile" )
95      private File templateFile;
96  
97      /**
98       * Additional template properties for rendering the site. See
99       * <a href="/doxia/doxia-sitetools/doxia-site-renderer/">Doxia Site Renderer</a>.
100      */
101     @Parameter
102     private Map<String, Object> attributes;
103 
104     /**
105      * Site renderer.
106      */
107     @Component
108     protected Renderer siteRenderer;
109 
110     /**
111      * Reports (Maven 2).
112      */
113     @Parameter( defaultValue = "${reports}", required = true, readonly = true )
114     protected List<MavenReport> reports;
115 
116     /**
117      * Alternative directory for xdoc source, useful for m1 to m2 migration
118      *
119      * @deprecated use the standard m2 directory layout
120      */
121     @Parameter( defaultValue = "${basedir}/xdocs" )
122     private File xdocDirectory;
123 
124     /**
125      * Directory containing generated documentation in source format (Doxia supported markup).
126      * This is used to pick up other source docs that might have been generated at build time (by reports or any other
127      * build time mean).
128      * This directory is expected to have the same structure as <code>siteDirectory</code>
129      * (ie. one directory per Doxia-source-supported markup types).
130      *
131      * todo should we deprecate in favour of reports directly using Doxia Sink API, without this Doxia source
132      * intermediate step?
133      */
134     @Parameter( alias = "workingDirectory", defaultValue = "${project.build.directory}/generated-site" )
135     protected File generatedSiteDirectory;
136 
137     /**
138      * The current Maven session.
139      */
140     @Parameter( defaultValue = "${session}", readonly = true, required = true )
141     protected MavenSession mavenSession;
142 
143     /**
144      * replaces previous reportPlugins parameter, that was injected by Maven core from
145      * reporting section: but this new configuration format has been abandoned.
146      *
147      * @since 3.7.1
148      */
149     @Parameter( defaultValue = "${project.reporting}", readonly = true )
150     private Reporting reporting;
151 
152     /**
153      * Whether to generate the summary page for project reports: project-info.html.
154      *
155      * @since 2.3
156      */
157     @Parameter( property = "generateProjectInfo", defaultValue = "true" )
158     private boolean generateProjectInfo;
159 
160     /**
161      * Specifies the input encoding.
162      *
163      * @since 2.3
164      */
165     @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
166     private String inputEncoding;
167 
168     /**
169      * Specifies the output encoding.
170      *
171      * @since 2.3
172      */
173     @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
174     private String outputEncoding;
175 
176     @Component
177     protected MavenReportExecutor mavenReportExecutor;
178 
179     /**
180      * Gets the input files encoding.
181      *
182      * @return The input files encoding, never <code>null</code>.
183      */
184     protected String getInputEncoding()
185     {
186         return ( StringUtils.isEmpty( inputEncoding ) ) ? ReaderFactory.FILE_ENCODING : inputEncoding;
187     }
188 
189     /**
190      * Gets the effective reporting output files encoding.
191      *
192      * @return The effective reporting output file encoding, never <code>null</code>.
193      */
194     protected String getOutputEncoding()
195     {
196         return ( outputEncoding == null ) ? ReaderFactory.UTF_8 : outputEncoding;
197     }
198 
199     /**
200      * Whether to save Velocity processed Doxia content (<code>*.&lt;ext&gt;.vm</code>)
201      * to <code>${generatedSiteDirectory}/processed</code>.
202      *
203      * @since 3.5
204      */
205     @Parameter
206     private boolean saveProcessedContent;
207 
208     protected void checkInputEncoding()
209     {
210         if ( StringUtils.isEmpty( inputEncoding ) )
211         {
212             getLog().warn( "Input file encoding has not been set, using platform encoding "
213                 + ReaderFactory.FILE_ENCODING + ", i.e. build is platform dependent!" );
214         }
215     }
216 
217     protected List<MavenReportExecution> getReports()
218         throws MojoExecutionException
219     {
220         MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest();
221         mavenReportExecutorRequest.setLocalRepository( localRepository );
222         mavenReportExecutorRequest.setMavenSession( mavenSession );
223         mavenReportExecutorRequest.setProject( project );
224         mavenReportExecutorRequest.setReportPlugins( getReportingPlugins() );
225 
226         List<MavenReportExecution> allReports = mavenReportExecutor.buildMavenReports( mavenReportExecutorRequest );
227 
228         // filter out reports that can't be generated
229         List<MavenReportExecution> reportExecutions = new ArrayList<>( allReports.size() );
230         for ( MavenReportExecution exec : allReports )
231         {
232             if ( exec.canGenerateReport() )
233             {
234                 reportExecutions.add( exec );
235             }
236         }
237         return reportExecutions;
238     }
239 
240     /**
241      * Get the report plugins from reporting section, adding if necessary (i.e. not excluded)
242      * default reports (i.e. maven-project-info-reports)
243      *
244      * @return the effective list of reports
245      * @since 3.7.1
246      */
247     private ReportPlugin[] getReportingPlugins()
248     {
249         List<ReportPlugin> reportingPlugins = reporting.getPlugins();
250 
251         // MSITE-806: add default report plugin like done in maven-model-builder DefaultReportingConverter
252         boolean hasMavenProjectInfoReportsPlugin = false;
253         for ( ReportPlugin plugin : reportingPlugins )
254         {
255             if ( "org.apache.maven.plugins".equals( plugin.getGroupId() )
256                 && "maven-project-info-reports-plugin".equals( plugin.getArtifactId() ) )
257             {
258                 hasMavenProjectInfoReportsPlugin = true;
259                 break;
260             }
261         }
262 
263         if ( !reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin )
264         {
265             ReportPlugin mpir = new ReportPlugin();
266             mpir.setArtifactId( "maven-project-info-reports-plugin" );
267             reportingPlugins.add( mpir );
268         }
269         return reportingPlugins.toArray( new ReportPlugin[reportingPlugins.size()] );
270     }
271 
272     protected SiteRenderingContext createSiteRenderingContext( Locale locale )
273         throws MojoExecutionException, IOException, MojoFailureException
274     {
275         DecorationModel decorationModel = prepareDecorationModel( locale );
276         if ( attributes == null )
277         {
278             attributes = new HashMap<>();
279         }
280 
281         if ( attributes.get( "project" ) == null )
282         {
283             attributes.put( "project", project );
284         }
285 
286         if ( attributes.get( "inputEncoding" ) == null )
287         {
288             attributes.put( "inputEncoding", getInputEncoding() );
289         }
290 
291         if ( attributes.get( "outputEncoding" ) == null )
292         {
293             attributes.put( "outputEncoding", getOutputEncoding() );
294         }
295 
296         // Put any of the properties in directly into the Velocity context
297         for ( Map.Entry<Object, Object> entry : project.getProperties().entrySet() )
298         {
299             attributes.put( (String) entry.getKey(), entry.getValue() );
300         }
301 
302         SiteRenderingContext context;
303         if ( templateFile != null )
304         {
305             getLog().info( buffer().a( "Rendering content with " ).strong( templateFile
306                 + " template file" ).a( '.' ).toString() );
307 
308             if ( !templateFile.exists() )
309             {
310                 throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" );
311             }
312             context = siteRenderer.createContextForTemplate( templateFile, attributes, decorationModel,
313                                                              project.getName(), locale );
314         }
315         else
316         {
317             try
318             {
319                 Artifact skinArtifact =
320                     siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel );
321 
322                 getLog().info( buffer().a( "Rendering content with " ).strong( skinArtifact.getId()
323                     + " skin" ).a( '.' ).toString() );
324 
325                 context = siteRenderer.createContextForSkin( skinArtifact, attributes, decorationModel,
326                                                              project.getName(), locale );
327             }
328             catch ( SiteToolException e )
329             {
330                 throw new MojoExecutionException( "SiteToolException while preparing skin: " + e.getMessage(), e );
331             }
332             catch ( RendererException e )
333             {
334                 throw new MojoExecutionException( "RendererException while preparing context for skin: "
335                     + e.getMessage(), e );
336             }
337         }
338 
339         // Generate static site
340         context.setRootDirectory( project.getBasedir() );
341         if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
342         {
343             context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) );
344             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" );
345             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" );
346         }
347         else
348         {
349             context.addSiteDirectory( siteDirectory );
350             context.addModuleDirectory( xdocDirectory, "xdoc" );
351             context.addModuleDirectory( xdocDirectory, "fml" );
352         }
353 
354         if ( moduleExcludes != null )
355         {
356             context.setModuleExcludes( moduleExcludes );
357         }
358 
359         if ( saveProcessedContent )
360         {
361             context.setProcessedContentOutput( new File( generatedSiteDirectory, "processed" ) );
362         }
363 
364         return context;
365     }
366 
367     /**
368      * Go through the list of reports and process each one like this:
369      * <ul>
370      * <li>Add the report to a map of reports keyed by filename having the report itself as value
371      * <li>If the report is not yet in the map of documents, add it together with a suitable renderer
372      * </ul>
373      *
374      * @param reports A List of MavenReports
375      * @param documents A Map of documents, keyed by filename
376      * @param locale the Locale the reports are processed for.
377      * @return A map with all reports keyed by filename having the report itself as value.
378      * The map will be used to populate a menu.
379      */
380     protected Map<String, MavenReport> locateReports( List<MavenReportExecution> reports,
381                                                       Map<String, DocumentRenderer> documents, Locale locale )
382     {
383         // copy Collection to prevent ConcurrentModificationException
384         List<MavenReportExecution> filtered = new ArrayList<>( reports );
385 
386         Map<String, MavenReport> reportsByOutputName = new LinkedHashMap<>();
387         for ( MavenReportExecution mavenReportExecution : filtered )
388         {
389             MavenReport report = mavenReportExecution.getMavenReport();
390 
391             String outputName = report.getOutputName() + ".html";
392 
393             // Always add the report to the menu, see MSITE-150
394             reportsByOutputName.put( report.getOutputName(), report );
395 
396             if ( documents.containsKey( outputName ) )
397             {
398                 String reportMojoInfo =
399                     ( mavenReportExecution.getGoal() == null ) ? "" : ( " ("
400                         + mavenReportExecution.getPlugin().getArtifactId() + ':'
401                         + mavenReportExecution.getPlugin().getVersion() + ':' + mavenReportExecution.getGoal() + ')' );
402 
403                 getLog().info( "Skipped \"" + report.getName( locale ) + "\" report" + reportMojoInfo + ", file \""
404                                    + outputName + "\" already exists." );
405 
406                 reports.remove( mavenReportExecution );
407             }
408             else
409             {
410                 String reportMojoInfo = mavenReportExecution.getPlugin().getGroupId() + ':'
411                     + mavenReportExecution.getPlugin().getArtifactId() + ':'
412                     + mavenReportExecution.getPlugin().getVersion() + ':' + mavenReportExecution.getGoal();
413                 RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName, reportMojoInfo );
414                 DocumentRenderer renderer =
415                     new ReportDocumentRenderer( mavenReportExecution, renderingContext, getLog() );
416                 documents.put( outputName, renderer );
417             }
418         }
419         return reportsByOutputName;
420     }
421 
422     /**
423      * Go through the collection of reports and put each report into a list for the appropriate category. The list is
424      * put into a map keyed by the name of the category.
425      *
426      * @param reports A Collection of MavenReports
427      * @return A map keyed category having the report itself as value
428      */
429     protected Map<String, List<MavenReport>> categoriseReports( Collection<MavenReport> reports )
430     {
431         Map<String, List<MavenReport>> categories = new LinkedHashMap<>();
432         for ( MavenReport report : reports )
433         {
434             List<MavenReport> categoryReports = categories.get( report.getCategoryName() );
435             if ( categoryReports == null )
436             {
437                 categoryReports = new ArrayList<>();
438                 categories.put( report.getCategoryName(), categoryReports );
439             }
440             categoryReports.add( report );
441         }
442         return categories;
443     }
444 
445     /**
446      * Locate every document to be rendered for given locale:<ul>
447      * <li>handwritten content, ie Doxia files,</li>
448      * <li>reports,</li>
449      * <li>"Project Information" and "Project Reports" category summaries.</li>
450      * </ul>
451      *
452      * @param context the site context
453      * @param reports the documents
454      * @param locale the locale
455      * @return the documents and their renderers
456      * @throws IOException in case of file reading issue
457      * @throws RendererException in case of Doxia rendering issue
458      * @see CategorySummaryDocumentRenderer
459      */
460     protected Map<String, DocumentRenderer> locateDocuments( SiteRenderingContext context,
461                                                              List<MavenReportExecution> reports, Locale locale )
462         throws IOException, RendererException
463     {
464         Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles( context, true );
465 
466         Map<String, MavenReport> reportsByOutputName = locateReports( reports, documents, locale );
467 
468         // TODO: I want to get rid of categories eventually. There's no way to add your own in a fully i18n manner
469         Map<String, List<MavenReport>> categories = categoriseReports( reportsByOutputName.values() );
470 
471         siteTool.populateReportsMenu( context.getDecoration(), locale, categories );
472         populateReportItems( context.getDecoration(), locale, reportsByOutputName );
473 
474         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) && generateProjectInfo )
475         {
476             // add "Project Information" category summary document
477             List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION );
478 
479             RenderingContext renderingContext =
480                 new RenderingContext( siteDirectory, "project-info.html",
481                                       getSitePluginInfo() + ":CategorySummaryDocumentRenderer" );
482             String title = i18n.getString( "site-plugin", locale, "report.information.title" );
483             String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" );
484             String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" );
485             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
486                                                                              i18n, categoryReports, getLog() );
487 
488             if ( !documents.containsKey( renderer.getOutputName() ) )
489             {
490                 documents.put( renderer.getOutputName(), renderer );
491             }
492             else
493             {
494                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
495             }
496         }
497 
498         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) )
499         {
500             // add "Project Reports" category summary document
501             List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_REPORTS );
502             RenderingContext renderingContext =
503                 new RenderingContext( siteDirectory, "project-reports.html",
504                                       getSitePluginInfo() + ":CategorySummaryDocumentRenderer" );
505             String title = i18n.getString( "site-plugin", locale, "report.project.title" );
506             String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" );
507             String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" );
508             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
509                                                                              i18n, categoryReports, getLog() );
510 
511             if ( !documents.containsKey( renderer.getOutputName() ) )
512             {
513                 documents.put( renderer.getOutputName(), renderer );
514             }
515             else
516             {
517                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
518             }
519         }
520         return documents;
521     }
522 
523     private String getSitePluginInfo()
524     {
525         PluginDescriptor pluginDescriptor = (PluginDescriptor) getPluginContext().get( "pluginDescriptor" );
526         return pluginDescriptor.getId();
527     }
528     protected void populateReportItems( DecorationModel decorationModel, Locale locale,
529                                         Map<String, MavenReport> reportsByOutputName )
530     {
531         for ( Menu menu : decorationModel.getMenus() )
532         {
533             populateItemRefs( menu.getItems(), locale, reportsByOutputName );
534         }
535     }
536 
537     private void populateItemRefs( List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName )
538     {
539         for ( Iterator<MenuItem> i = items.iterator(); i.hasNext(); )
540         {
541             MenuItem item = i.next();
542 
543             if ( item.getRef() != null )
544             {
545                 MavenReport report = reportsByOutputName.get( item.getRef() );
546 
547                 if ( report != null )
548                 {
549                     if ( item.getName() == null )
550                     {
551                         item.setName( report.getName( locale ) );
552                     }
553 
554                     if ( item.getHref() == null || item.getHref().length() == 0 )
555                     {
556                         item.setHref( report.getOutputName() + ".html" );
557                     }
558                 }
559                 else
560                 {
561                     getLog().warn( "Unrecognised reference: '" + item.getRef() + "'" );
562                     i.remove();
563                 }
564             }
565 
566             populateItemRefs( item.getItems(), locale, reportsByOutputName );
567         }
568     }
569 }