View Javadoc

1   package org.apache.maven.plugins.site;
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  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Iterator;
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.sink.render.RenderingContext;
36  import org.apache.maven.doxia.site.decoration.DecorationModel;
37  import org.apache.maven.doxia.site.decoration.Menu;
38  import org.apache.maven.doxia.site.decoration.MenuItem;
39  import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler;
40  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
41  import org.apache.maven.doxia.siterenderer.Renderer;
42  import org.apache.maven.doxia.siterenderer.RendererException;
43  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
44  import org.apache.maven.doxia.tools.SiteToolException;
45  import org.apache.maven.plugin.MojoExecutionException;
46  import org.apache.maven.plugin.MojoFailureException;
47  import org.apache.maven.reporting.MavenReport;
48  
49  
50  /**
51   * Base class for site rendering mojos.
52   *
53   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
54   * @version $Id: AbstractSiteRenderingMojo.html 816558 2012-05-08 12:00:46Z hboutemy $
55   */
56  public abstract class AbstractSiteRenderingMojo
57      extends AbstractSiteMojo
58  {
59      /**
60       * Module type exclusion mappings
61       * ex: <code>fml  -> **&#47;*-m1.fml</code>  (excludes fml files ending in '-m1.fml' recursively)
62       * <p/>
63       * The configuration looks like this:
64       * <pre>
65       *   &lt;moduleExcludes&gt;
66       *     &lt;moduleType&gt;filename1.ext,**&#47;*sample.ext&lt;/moduleType&gt;
67       *     &lt;!-- moduleType can be one of 'apt', 'fml' or 'xdoc'. --&gt;
68       *     &lt;!-- The value is a comma separated list of           --&gt;
69       *     &lt;!-- filenames or fileset patterns.                   --&gt;
70       *     &lt;!-- Here's an example:                               --&gt;
71       *     &lt;xdoc&gt;changes.xml,navigation.xml&lt;/xdoc&gt;
72       *   &lt;/moduleExcludes&gt;
73       * </pre>
74       *
75       * @parameter
76       */
77      private Map<String, String> moduleExcludes;
78  
79      /**
80       * The component for assembling inheritance.
81       *
82       * @component
83       */
84      private DecorationModelInheritanceAssembler assembler;
85  
86      /**
87       * Remote repositories used for the project.
88       *
89       * @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
90       * @parameter default-value="${project.remoteArtifactRepositories}"
91       * @readonly
92       */
93      private List<ArtifactRepository> repositories;
94  
95      /**
96       * Directory containing the template page.
97       *
98       * @parameter expression="${templateDirectory}" default-value="src/site"
99       * @deprecated use templateFile or skinning instead
100      */
101     private File templateDirectory;
102 
103     /**
104      * Default template page.
105      *
106      * @parameter expression="${template}"
107      * @deprecated use templateFile or skinning instead
108      */
109     private String template;
110 
111     /**
112      * The location of a Velocity template file to use. When used, skins and the default templates, CSS and images
113      * are disabled. It is highly recommended that you package this as a skin instead.
114      *
115      * @parameter expression="${templateFile}"
116      * @since 2.0-beta-5
117      */
118     private File templateFile;
119 
120     /**
121      * The template properties for rendering the site.
122      *
123      * @parameter expression="${attributes}"
124      */
125     private Map<String, Object> attributes;
126 
127     /**
128      * Site renderer.
129      *
130      * @component
131      */
132     protected Renderer siteRenderer;
133 
134     /**
135      * @parameter expression="${reports}"
136      * @required
137      * @readonly
138      */
139     protected List<MavenReport> reports;
140 
141     /**
142      * Alternative directory for xdoc source, useful for m1 to m2 migration
143      *
144      * @parameter default-value="${basedir}/xdocs"
145      * @deprecated use the standard m2 directory layout
146      */
147     private File xdocDirectory;
148 
149     /**
150      * Directory containing generated documentation.
151      * This is used to pick up other source docs that might have been generated at build time.
152      *
153      * @parameter alias="workingDirectory" default-value="${project.build.directory}/generated-site"
154      *
155      * @todo should we deprecate in favour of reports?
156      */
157     protected File generatedSiteDirectory;
158 
159     /**
160      * Make links in the site descriptor relative to the project URL.
161      * By default, any absolute links that appear in the site descriptor,
162      * e.g. banner hrefs, breadcrumbs, menu links, etc., will be made relative to project.url.
163      *
164      * Links will not be changed if this is set to false, or if the project has no URL defined.
165      *
166      * @parameter expression="${relativizeDecorationLinks}" default-value="true"
167      *
168      * @since 2.3
169      */
170     private boolean relativizeDecorationLinks;
171 
172     /**
173      * Whether to generate the summary page for project reports: project-info.html.
174      *
175      * @parameter expression="${generateProjectInfo}" default-value="true"
176      *
177      * @since 2.3
178      */
179     private boolean generateProjectInfo;
180 
181     protected List<MavenReport> filterReports( List<MavenReport> reports )
182     {
183         List<MavenReport> filteredReports = new ArrayList<MavenReport>( reports.size() );
184         for ( MavenReport report : reports )
185         {
186             if ( report.canGenerateReport() )
187             {
188                 filteredReports.add( report );
189             }
190         }
191         return filteredReports;
192     }
193 
194     protected SiteRenderingContext createSiteRenderingContext( Locale locale )
195         throws MojoExecutionException, IOException, MojoFailureException
196     {
197         if ( attributes == null )
198         {
199             attributes = new HashMap<String, Object>();
200         }
201 
202         if ( attributes.get( "project" ) == null )
203         {
204             attributes.put( "project", project );
205         }
206 
207         if ( attributes.get( "inputEncoding" ) == null )
208         {
209             attributes.put( "inputEncoding", getInputEncoding() );
210         }
211 
212         if ( attributes.get( "outputEncoding" ) == null )
213         {
214             attributes.put( "outputEncoding", getOutputEncoding() );
215         }
216 
217         // Put any of the properties in directly into the Velocity context
218         for ( Map.Entry<Object, Object> entry : project.getProperties().entrySet() )
219         {
220             attributes.put( (String) entry.getKey(), entry.getValue() );
221         }
222 
223         DecorationModel decorationModel;
224         try
225         {
226             decorationModel = siteTool.getDecorationModel( project, reactorProjects, localRepository, repositories,
227                                                            siteTool.getRelativePath( siteDirectory.getAbsolutePath(),
228                                                            project.getBasedir().getAbsolutePath() ),
229                                                            locale, getInputEncoding(), getOutputEncoding() );
230         }
231         catch ( SiteToolException e )
232         {
233             throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
234         }
235 
236         if ( relativizeDecorationLinks )
237         {
238             final String url = project.getUrl();
239 
240             if ( url == null )
241             {
242                 getLog().warn( "No project URL defined - decoration links will not be relativized!" );
243             }
244             else
245             {
246                 getLog().info( "Relativizing decoration links with respect to project URL: " + url );
247                 assembler.resolvePaths( decorationModel, url );
248             }
249         }
250 
251         if ( template != null )
252         {
253             if ( templateFile != null )
254             {
255                 getLog().warn( "'template' configuration is ignored when 'templateFile' is set" );
256             }
257             else
258             {
259                 templateFile = new File( templateDirectory, template );
260             }
261         }
262 
263         File skinFile;
264         try
265         {
266             Artifact skinArtifact =
267                 siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel );
268             getLog().info( "Rendering site with " + skinArtifact.getId() + " skin." );
269 
270             skinFile = skinArtifact.getFile();
271         }
272         catch ( SiteToolException e )
273         {
274             throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
275         }
276         SiteRenderingContext context;
277         if ( templateFile != null )
278         {
279             if ( !templateFile.exists() )
280             {
281                 throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" );
282             }
283             context = siteRenderer.createContextForTemplate( templateFile, skinFile, attributes, decorationModel,
284                                                              project.getName(), locale );
285         }
286         else
287         {
288             context = siteRenderer.createContextForSkin( skinFile, attributes, decorationModel, project.getName(),
289                                                          locale );
290         }
291 
292         // Generate static site
293         if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
294         {
295             context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) );
296             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" );
297             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" );
298         }
299         else
300         {
301             context.addSiteDirectory( siteDirectory );
302             context.addModuleDirectory( xdocDirectory, "xdoc" );
303             context.addModuleDirectory( xdocDirectory, "fml" );
304         }
305 
306         if ( moduleExcludes != null )
307         {
308             context.setModuleExcludes( moduleExcludes );
309         }
310 
311         return context;
312     }
313 
314     /**
315      * Go through the list of reports and process each one like this:
316      * <ul>
317      * <li>Add the report to a map of reports keyed by filename having the report itself as value
318      * <li>If the report is not yet in the map of documents, add it together with a suitable renderer
319      * </ul>
320      *
321      * @param reports A List of MavenReports
322      * @param documents A Map of documents, keyed by filename
323      * @param locale the Locale the reports are processed for.
324      * @return A map with all reports keyed by filename having the report itself as value.
325      * The map will be used to populate a menu.
326      */
327     protected Map<String, MavenReport> locateReports( List<MavenReport> reports,
328                                                       Map<String, DocumentRenderer> documents, Locale locale )
329     {
330         Map<String, MavenReport> reportsByOutputName = new HashMap<String, MavenReport>();
331         for ( Iterator<MavenReport> i = reports.iterator(); i.hasNext(); )
332         {
333             MavenReport report = i.next();
334 
335             String outputName = report.getOutputName() + ".html";
336 
337             // Always add the report to the menu, see MSITE-150
338             reportsByOutputName.put( report.getOutputName(), report );
339 
340             if ( documents.containsKey( outputName ) )
341             {
342                 String displayLanguage = locale.getDisplayLanguage( Locale.ENGLISH );
343 
344                 getLog().info( "Skipped \"" + report.getName( locale ) + "\" report, file \"" + outputName
345                                    + "\" already exists for the " + displayLanguage + " version." );
346                 i.remove();
347             }
348             else
349             {
350                 RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName );
351                 ReportDocumentRenderer renderer = new ReportDocumentRenderer( report, renderingContext, getLog() );
352                 documents.put( outputName, renderer );
353             }
354         }
355         return reportsByOutputName;
356     }
357 
358     /**
359      * Go through the collection of reports and put each report into a list for the appropriate category. The list is
360      * put into a map keyed by the name of the category.
361      *
362      * @param reports A Collection of MavenReports
363      * @return A map keyed category having the report itself as value
364      */
365     protected Map<String, List<MavenReport>> categoriseReports( Collection<MavenReport> reports )
366     {
367         Map<String, List<MavenReport>> categories = new HashMap<String, List<MavenReport>>();
368         for ( MavenReport report : reports )
369         {
370             List<MavenReport> categoryReports = categories.get( report.getCategoryName() );
371             if ( categoryReports == null )
372             {
373                 categoryReports = new ArrayList<MavenReport>();
374                 categories.put( report.getCategoryName(), categoryReports );
375             }
376             categoryReports.add( report );
377         }
378         return categories;
379     }
380 
381     protected Map<String, DocumentRenderer> locateDocuments( SiteRenderingContext context, List<MavenReport> reports,
382                                                              Locale locale )
383         throws IOException, RendererException
384     {
385         Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles( context );
386 
387         Map<String, MavenReport> reportsByOutputName = locateReports( reports, documents, locale );
388 
389         // TODO: I want to get rid of categories eventually. There's no way to add your own in a fully i18n manner
390         Map<String, List<MavenReport>> categories = categoriseReports( reportsByOutputName.values() );
391 
392         siteTool.populateReportsMenu( context.getDecoration(), locale, categories );
393         populateReportItems( context.getDecoration(), locale, reportsByOutputName );
394 
395         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) && generateProjectInfo )
396         {
397             List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION );
398 
399             RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-info.html" );
400             String title = i18n.getString( "site-plugin", locale, "report.information.title" );
401             String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" );
402             String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" );
403             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
404                                                                              i18n, categoryReports, getLog() );
405 
406             if ( !documents.containsKey( renderer.getOutputName() ) )
407             {
408                 documents.put( renderer.getOutputName(), renderer );
409             }
410             else
411             {
412                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
413             }
414         }
415 
416         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) )
417         {
418             List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_REPORTS );
419             RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-reports.html" );
420             String title = i18n.getString( "site-plugin", locale, "report.project.title" );
421             String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" );
422             String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" );
423             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
424                                                                              i18n, categoryReports, getLog() );
425 
426             if ( !documents.containsKey( renderer.getOutputName() ) )
427             {
428                 documents.put( renderer.getOutputName(), renderer );
429             }
430             else
431             {
432                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
433             }
434         }
435         return documents;
436     }
437 
438     protected void populateReportItems( DecorationModel decorationModel, Locale locale,
439                                         Map<String, MavenReport> reportsByOutputName )
440     {
441         for ( Menu menu : decorationModel.getMenus() )
442         {
443             populateItemRefs( menu.getItems(), locale, reportsByOutputName );
444         }
445     }
446 
447     private void populateItemRefs( List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName )
448     {
449         for ( Iterator<MenuItem> i = items.iterator(); i.hasNext(); )
450         {
451             MenuItem item = i.next();
452 
453             if ( item.getRef() != null )
454             {
455                 MavenReport report = reportsByOutputName.get( item.getRef() );
456 
457                 if ( report != null )
458                 {
459 
460                     if ( item.getName() == null )
461                     {
462                         item.setName( report.getName( locale ) );
463                     }
464 
465                     if ( item.getHref() == null || item.getHref().length() == 0 )
466                     {
467                         item.setHref( report.getOutputName() + ".html" );
468                     }
469                 }
470                 else
471                 {
472                     getLog().warn( "Unrecognised reference: '" + item.getRef() + "'" );
473                     i.remove();
474                 }
475             }
476 
477             populateItemRefs( item.getItems(), locale, reportsByOutputName );
478         }
479     }
480 }