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