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