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