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 java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  import org.apache.maven.artifact.Artifact;
34  import org.apache.maven.artifact.repository.ArtifactRepository;
35  import org.apache.maven.doxia.site.decoration.DecorationModel;
36  import org.apache.maven.doxia.site.decoration.Menu;
37  import org.apache.maven.doxia.site.decoration.MenuItem;
38  import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler;
39  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
40  import org.apache.maven.doxia.siterenderer.Renderer;
41  import org.apache.maven.doxia.siterenderer.RendererException;
42  import org.apache.maven.doxia.siterenderer.RenderingContext;
43  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
44  import org.apache.maven.doxia.tools.SiteToolException;
45  import org.apache.maven.execution.MavenSession;
46  import org.apache.maven.plugin.MojoExecutionException;
47  import org.apache.maven.plugin.MojoFailureException;
48  import org.apache.maven.plugins.annotations.Component;
49  import org.apache.maven.plugins.annotations.Parameter;
50  import org.apache.maven.plugins.site.AbstractSiteMojo;
51  import org.apache.maven.reporting.MavenReport;
52  import org.apache.maven.reporting.exec.MavenReportExecution;
53  import org.apache.maven.reporting.exec.MavenReportExecutor;
54  import org.apache.maven.reporting.exec.MavenReportExecutorRequest;
55  import org.apache.maven.reporting.exec.ReportPlugin;
56  import org.codehaus.plexus.PlexusConstants;
57  import org.codehaus.plexus.PlexusContainer;
58  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
59  import org.codehaus.plexus.context.Context;
60  import org.codehaus.plexus.context.ContextException;
61  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
62  
63  /**
64   * Base class for site rendering mojos.
65   *
66   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
67   * @version $Id: AbstractSiteRenderingMojo.html 914946 2014-07-03 23:00:36Z hboutemy $
68   */
69  public abstract class AbstractSiteRenderingMojo
70      extends AbstractSiteMojo implements Contextualizable
71  {
72      /**
73       * Module type exclusion mappings
74       * ex: <code>fml  -> **&#47;*-m1.fml</code>  (excludes fml files ending in '-m1.fml' recursively)
75       * <p/>
76       * The configuration looks like this:
77       * <pre>
78       *   &lt;moduleExcludes&gt;
79       *     &lt;moduleType&gt;filename1.ext,**&#47;*sample.ext&lt;/moduleType&gt;
80       *     &lt;!-- moduleType can be one of 'apt', 'fml' or 'xdoc'. --&gt;
81       *     &lt;!-- The value is a comma separated list of           --&gt;
82       *     &lt;!-- filenames or fileset patterns.                   --&gt;
83       *     &lt;!-- Here's an example:                               --&gt;
84       *     &lt;xdoc&gt;changes.xml,navigation.xml&lt;/xdoc&gt;
85       *   &lt;/moduleExcludes&gt;
86       * </pre>
87       */
88      @Parameter
89      private Map<String, String> moduleExcludes;
90  
91      /**
92       * The component for assembling inheritance.
93       */
94      @Component
95      private DecorationModelInheritanceAssembler assembler;
96  
97      /**
98       * Remote repositories used for the project.
99       *
100      * @todo this is used for site descriptor resolution - it should relate to the actual project but for some reason they are not always filled in
101      */
102     @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true )
103     private List<ArtifactRepository> repositories;
104 
105     /**
106      * Directory containing the template page.
107      *
108      * @deprecated use templateFile or skinning instead
109      */
110     @Parameter( property = "templateDirectory", defaultValue = "src/site" )
111     private File templateDirectory;
112 
113     /**
114      * Default template page.
115      *
116      * @deprecated use templateFile or skinning instead
117      */
118     @Parameter( property = "template" )
119     private String template;
120 
121     /**
122      * The location of a Velocity template file to use. When used, skins and the default templates, CSS and images
123      * are disabled. It is highly recommended that you package this as a skin instead.
124      *
125      * @since 2.0-beta-5
126      */
127     @Parameter( property = "templateFile" )
128     private File templateFile;
129 
130     /**
131      * Additional template properties for rendering the site. See
132      * <a href="/doxia/doxia-sitetools/doxia-site-renderer/">Doxia Site Renderer</a>.
133      */
134     @Parameter
135     private Map<String, Object> attributes;
136 
137     /**
138      * Site renderer.
139      */
140     @Component
141     protected Renderer siteRenderer;
142 
143     /**
144      * Reports (Maven 2).
145      */
146     @Parameter( defaultValue = "${reports}", required = true, readonly = true )
147     protected List<MavenReport> reports;
148 
149     /**
150      * Alternative directory for xdoc source, useful for m1 to m2 migration
151      *
152      * @deprecated use the standard m2 directory layout
153      */
154     @Parameter( defaultValue = "${basedir}/xdocs" )
155     private File xdocDirectory;
156 
157     /**
158      * Directory containing generated documentation.
159      * This is used to pick up other source docs that might have been generated at build time.
160      *
161      * @todo should we deprecate in favour of reports?
162      */
163     @Parameter( alias = "workingDirectory", defaultValue = "${project.build.directory}/generated-site" )
164     protected File generatedSiteDirectory;
165 
166     /**
167      * The current Maven session.
168      */
169     @Parameter( defaultValue = "${session}" )
170     protected MavenSession mavenSession;
171 
172     /**
173      * <p>Configuration section <b>used internally</b> by Maven 3.</p>
174      * <p>More details available here:
175      * <a href="http://maven.apache.org/plugins/maven-site-plugin/maven-3.html#Configuration_formats" target="_blank">
176      * http://maven.apache.org/plugins/maven-site-plugin/maven-3.html#Configuration_formats</a>
177      * </p>
178      * <p><b>Note:</b> using this field is not mandatory with Maven 3 as Maven core injects usual
179      * <code>&lt;reporting&gt;</code> section into this field.</p>
180      *
181      * @since 3.0-beta-1 (and read-only since 3.3)
182      */
183     @Parameter( readonly = true )
184     private ReportPlugin[] reportPlugins;
185 
186     private PlexusContainer container;
187 
188     /**
189      * Make links in the site descriptor relative to the project URL.
190      * By default, any absolute links that appear in the site descriptor,
191      * e.g. banner hrefs, breadcrumbs, menu links, etc., will be made relative to project.url.
192      * <p/>
193      * Links will not be changed if this is set to false, or if the project has no URL defined.
194      *
195      * @since 2.3
196      */
197     @Parameter( property = "relativizeDecorationLinks", defaultValue = "true" )
198     private boolean relativizeDecorationLinks;
199 
200     /**
201      * Whether to generate the summary page for project reports: project-info.html.
202      *
203      * @since 2.3
204      */
205     @Parameter( property = "generateProjectInfo", defaultValue = "true" )
206     private boolean generateProjectInfo;
207 
208     /** {@inheritDoc} */
209     public void contextualize( Context context )
210         throws ContextException
211     {
212         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
213     }
214 
215     protected List<MavenReportExecution> getReports()
216         throws MojoExecutionException
217     {
218         List<MavenReportExecution> allReports;
219 
220         if ( isMaven3OrMore() )
221         {
222             // Maven 3
223             MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest();
224             mavenReportExecutorRequest.setLocalRepository( localRepository );
225             mavenReportExecutorRequest.setMavenSession( mavenSession );
226             mavenReportExecutorRequest.setProject( project );
227             mavenReportExecutorRequest.setReportPlugins( reportPlugins );
228 
229             MavenReportExecutor mavenReportExecutor;
230             try
231             {
232                 mavenReportExecutor = (MavenReportExecutor) container.lookup( MavenReportExecutor.class.getName() );
233             }
234             catch ( ComponentLookupException e )
235             {
236                 throw new MojoExecutionException( "could not get MavenReportExecutor component", e );
237             }
238 
239             allReports = mavenReportExecutor.buildMavenReports( mavenReportExecutorRequest );
240         }
241         else
242         {
243             // Maven 2
244             allReports = new ArrayList<MavenReportExecution>( reports.size() );
245             for ( MavenReport report : reports )
246             {
247                 allReports.add( new MavenReportExecution( report ) );
248             }
249         }
250 
251         // filter out reports that can't be generated
252         List<MavenReportExecution> reportExecutions = new ArrayList<MavenReportExecution>( allReports.size() );
253         for ( MavenReportExecution exec : allReports )
254         {
255             if ( exec.canGenerateReport() )
256             {
257                 reportExecutions.add( exec );
258             }
259         }
260 
261         return reportExecutions;
262     }
263 
264     protected SiteRenderingContext createSiteRenderingContext( Locale locale )
265         throws MojoExecutionException, IOException, MojoFailureException
266     {
267         if ( attributes == null )
268         {
269             attributes = new HashMap<String, Object>();
270         }
271 
272         if ( attributes.get( "project" ) == null )
273         {
274             attributes.put( "project", project );
275         }
276 
277         if ( attributes.get( "inputEncoding" ) == null )
278         {
279             attributes.put( "inputEncoding", getInputEncoding() );
280         }
281 
282         if ( attributes.get( "outputEncoding" ) == null )
283         {
284             attributes.put( "outputEncoding", getOutputEncoding() );
285         }
286 
287         // Put any of the properties in directly into the Velocity context
288         for ( Map.Entry<Object, Object> entry : project.getProperties().entrySet() )
289         {
290             attributes.put( (String) entry.getKey(), entry.getValue() );
291         }
292 
293         DecorationModel decorationModel;
294         try
295         {
296             decorationModel = siteTool.getDecorationModel( project, reactorProjects, localRepository, repositories,
297                                                            siteTool.getRelativePath( siteDirectory.getAbsolutePath(),
298                                                            project.getBasedir().getAbsolutePath() ),
299                                                            locale );
300         }
301         catch ( SiteToolException e )
302         {
303             throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
304         }
305 
306         if ( relativizeDecorationLinks )
307         {
308             final String url = project.getUrl();
309 
310             if ( url == null )
311             {
312                 getLog().warn( "No project URL defined - decoration links will not be relativized!" );
313             }
314             else
315             {
316                 // MSITE-658
317                 final String localeUrl =
318                     locale.equals( Locale.getDefault() ) ? url : url + "/" + locale.getLanguage();
319                 getLog().info( "Relativizing decoration links with respect to project URL: " + localeUrl );
320                 assembler.resolvePaths( decorationModel, localeUrl );
321             }
322         }
323 
324         if ( template != null )
325         {
326             if ( templateFile != null )
327             {
328                 getLog().warn( "'template' configuration is ignored when 'templateFile' is set" );
329             }
330             else
331             {
332                 templateFile = new File( templateDirectory, template );
333             }
334         }
335 
336         File skinFile;
337         try
338         {
339             Artifact skinArtifact =
340                 siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel );
341             getLog().info( "Rendering site with " + skinArtifact.getId() + " skin." );
342 
343             skinFile = skinArtifact.getFile();
344         }
345         catch ( SiteToolException e )
346         {
347             throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
348         }
349         SiteRenderingContext context;
350         if ( templateFile != null )
351         {
352             if ( !templateFile.exists() )
353             {
354                 throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" );
355             }
356             context = siteRenderer.createContextForTemplate( templateFile, skinFile, attributes, decorationModel,
357                                                              project.getName(), locale );
358         }
359         else
360         {
361             context = siteRenderer.createContextForSkin( skinFile, attributes, decorationModel, project.getName(),
362                                                          locale );
363         }
364 
365         // Generate static site
366         if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
367         {
368             context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) );
369             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" );
370             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" );
371         }
372         else
373         {
374             context.addSiteDirectory( siteDirectory );
375             context.addModuleDirectory( xdocDirectory, "xdoc" );
376             context.addModuleDirectory( xdocDirectory, "fml" );
377         }
378 
379         if ( moduleExcludes != null )
380         {
381             context.setModuleExcludes( moduleExcludes );
382         }
383 
384         return context;
385     }
386 
387     /**
388      * Go through the list of reports and process each one like this:
389      * <ul>
390      * <li>Add the report to a map of reports keyed by filename having the report itself as value
391      * <li>If the report is not yet in the map of documents, add it together with a suitable renderer
392      * </ul>
393      *
394      * @param reports A List of MavenReports
395      * @param documents A Map of documents, keyed by filename
396      * @param locale the Locale the reports are processed for.
397      * @return A map with all reports keyed by filename having the report itself as value.
398      * The map will be used to populate a menu.
399      */
400     protected Map<String, MavenReport> locateReports( List<MavenReportExecution> reports,
401                                                       Map<String, DocumentRenderer> documents, Locale locale )
402     {
403         // copy Collection to prevent ConcurrentModificationException
404         List<MavenReportExecution> filtered = new ArrayList<MavenReportExecution>( reports );
405 
406         Map<String, MavenReport> reportsByOutputName = new LinkedHashMap<String, MavenReport>();
407         for ( MavenReportExecution mavenReportExecution : filtered )
408         {
409             MavenReport report = mavenReportExecution.getMavenReport();
410 
411             String outputName = report.getOutputName() + ".html";
412 
413             // Always add the report to the menu, see MSITE-150
414             reportsByOutputName.put( report.getOutputName(), report );
415 
416             if ( documents.containsKey( outputName ) )
417             {
418                 String displayLanguage = locale.getDisplayLanguage( Locale.ENGLISH );
419 
420                 String reportMojoInfo =
421                     ( mavenReportExecution.getGoal() == null ) ? "" : ( " ("
422                         + mavenReportExecution.getPlugin().getArtifactId() + ':'
423                         + mavenReportExecution.getPlugin().getVersion() + ':' + mavenReportExecution.getGoal() + ')' );
424 
425                 getLog().info( "Skipped \"" + report.getName( locale ) + "\" report" + reportMojoInfo + ", file \""
426                                    + outputName + "\" already exists for the " + displayLanguage + " version." );
427 
428                 reports.remove( mavenReportExecution );
429             }
430             else
431             {
432                 RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName );
433                 DocumentRenderer renderer = new ReportDocumentRenderer( mavenReportExecution, renderingContext, getLog() );
434                 documents.put( outputName, renderer );
435             }
436         }
437         return reportsByOutputName;
438     }
439 
440     /**
441      * Go through the collection of reports and put each report into a list for the appropriate category. The list is
442      * put into a map keyed by the name of the category.
443      *
444      * @param reports A Collection of MavenReports
445      * @return A map keyed category having the report itself as value
446      */
447     protected Map<String, List<MavenReport>> categoriseReports( Collection<MavenReport> reports )
448     {
449         Map<String, List<MavenReport>> categories = new LinkedHashMap<String, List<MavenReport>>();
450         for ( MavenReport report : reports )
451         {
452             List<MavenReport> categoryReports = categories.get( report.getCategoryName() );
453             if ( categoryReports == null )
454             {
455                 categoryReports = new ArrayList<MavenReport>();
456                 categories.put( report.getCategoryName(), categoryReports );
457             }
458             categoryReports.add( report );
459         }
460         return categories;
461     }
462 
463     /**
464      * Locate every document to be rendered for given locale:<ul>
465      * <li>handwritten content,</li>
466      * <li>reports,</li>
467      * <li>"Project Information" and "Project Reports" category summaries.</li>
468      * </ul>
469      */
470     protected Map<String, DocumentRenderer> locateDocuments( SiteRenderingContext context, List<MavenReportExecution> reports,
471                                                              Locale locale )
472         throws IOException, RendererException
473     {
474         Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles( context );
475 
476         Map<String, MavenReport> reportsByOutputName = locateReports( reports, documents, locale );
477 
478         // TODO: I want to get rid of categories eventually. There's no way to add your own in a fully i18n manner
479         Map<String, List<MavenReport>> categories = categoriseReports( reportsByOutputName.values() );
480 
481         siteTool.populateReportsMenu( context.getDecoration(), locale, categories );
482         populateReportItems( context.getDecoration(), locale, reportsByOutputName );
483 
484         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) && generateProjectInfo )
485         {
486             // add "Project Information" category summary document
487             List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION );
488 
489             RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-info.html" );
490             String title = i18n.getString( "site-plugin", locale, "report.information.title" );
491             String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" );
492             String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" );
493             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
494                                                                              i18n, categoryReports, getLog() );
495 
496             if ( !documents.containsKey( renderer.getOutputName() ) )
497             {
498                 documents.put( renderer.getOutputName(), renderer );
499             }
500             else
501             {
502                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
503             }
504         }
505 
506         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) )
507         {
508             // add "Project Reports" category summary document
509             List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_REPORTS );
510             RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-reports.html" );
511             String title = i18n.getString( "site-plugin", locale, "report.project.title" );
512             String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" );
513             String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" );
514             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
515                                                                              i18n, categoryReports, getLog() );
516 
517             if ( !documents.containsKey( renderer.getOutputName() ) )
518             {
519                 documents.put( renderer.getOutputName(), renderer );
520             }
521             else
522             {
523                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
524             }
525         }
526         return documents;
527     }
528 
529     protected void populateReportItems( DecorationModel decorationModel, Locale locale,
530                                         Map<String, MavenReport> reportsByOutputName )
531     {
532         for ( Menu menu : decorationModel.getMenus() )
533         {
534             populateItemRefs( menu.getItems(), locale, reportsByOutputName );
535         }
536     }
537 
538     private void populateItemRefs( List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName )
539     {
540         for ( Iterator<MenuItem> i = items.iterator(); i.hasNext(); )
541         {
542             MenuItem item = i.next();
543 
544             if ( item.getRef() != null )
545             {
546                 MavenReport report = reportsByOutputName.get( item.getRef() );
547 
548                 if ( report != null )
549                 {
550                     if ( item.getName() == null )
551                     {
552                         item.setName( report.getName( locale ) );
553                     }
554 
555                     if ( item.getHref() == null || item.getHref().length() == 0 )
556                     {
557                         item.setHref( report.getOutputName() + ".html" );
558                     }
559                 }
560                 else
561                 {
562                     getLog().warn( "Unrecognised reference: '" + item.getRef() + "'" );
563                     i.remove();
564                 }
565             }
566 
567             populateItemRefs( item.getItems(), locale, reportsByOutputName );
568         }
569     }
570 }