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