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