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