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.Menu;
36  import org.apache.maven.doxia.site.MenuItem;
37  import org.apache.maven.doxia.site.SiteModel;
38  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
39  import org.apache.maven.doxia.siterenderer.DocumentRenderingContext;
40  import org.apache.maven.doxia.siterenderer.RendererException;
41  import org.apache.maven.doxia.siterenderer.SiteRenderer;
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.MojoExecution;
49  import org.apache.maven.plugin.MojoExecutionException;
50  import org.apache.maven.plugin.MojoFailureException;
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.reporting.MavenReport;
55  import org.apache.maven.reporting.MavenReportException;
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.apache.maven.shared.utils.WriterFactory;
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      * The mojo execution
124      */
125     @Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
126     protected MojoExecution mojoExecution;
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      * Generate a sitemap. The result will be a "sitemap.html" file at the site root.
147      *
148      * @since 2.1
149      */
150     @Parameter(property = "generateSitemap", defaultValue = "false")
151     private boolean generateSitemap;
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     /**
170      * Timestamp for reproducible output archive entries, either formatted as ISO 8601
171      * <code>yyyy-MM-dd'T'HH:mm:ssXXX</code> or as an int representing seconds since the epoch (like
172      * <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
173      *
174      * @since 3.9.0
175      */
176     @Parameter(defaultValue = "${project.build.outputTimestamp}")
177     protected String outputTimestamp;
178 
179     @Component
180     protected MavenReportExecutor mavenReportExecutor;
181 
182     /**
183      * Gets the input files encoding.
184      *
185      * @return The input files encoding, never <code>null</code>.
186      */
187     protected String getInputEncoding() {
188         return (inputEncoding == null || inputEncoding.isEmpty()) ? ReaderFactory.FILE_ENCODING : inputEncoding;
189     }
190 
191     /**
192      * Gets the effective reporting output files encoding.
193      *
194      * @return The effective reporting output file encoding, never <code>null</code>.
195      */
196     protected String getOutputEncoding() {
197         return (outputEncoding == null) ? WriterFactory.UTF_8 : outputEncoding;
198     }
199 
200     /**
201      * Whether to save Velocity processed Doxia content (<code>*.&lt;ext&gt;.vm</code>)
202      * to <code>${generatedSiteDirectory}/processed</code>.
203      *
204      * @since 3.5
205      */
206     @Parameter
207     private boolean saveProcessedContent;
208 
209     protected void checkInputEncoding() {
210         if (inputEncoding == null || inputEncoding.isEmpty()) {
211             getLog().warn("Input file encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
212                     + ", i.e. build is platform dependent!");
213         }
214     }
215 
216     protected List<MavenReportExecution> getReports() throws MojoExecutionException {
217         MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest();
218         mavenReportExecutorRequest.setMavenSession(mavenSession);
219         mavenReportExecutorRequest.setExecutionId(mojoExecution.getExecutionId());
220         mavenReportExecutorRequest.setProject(project);
221         mavenReportExecutorRequest.setReportPlugins(getReportingPlugins());
222 
223         List<MavenReportExecution> allReports = mavenReportExecutor.buildMavenReports(mavenReportExecutorRequest);
224 
225         // filter out reports that can't be generated
226         List<MavenReportExecution> reportExecutions = new ArrayList<>(allReports.size());
227         for (MavenReportExecution exec : allReports) {
228             String reportMojoInfo = exec.getPlugin().getId() + ":" + exec.getGoal();
229             try {
230                 if (exec.canGenerateReport()) {
231                     reportExecutions.add(exec);
232                 } else if (exec.isUserDefined()) {
233                     getLog().info("Skipping " + reportMojoInfo + " report");
234                 }
235             } catch (MavenReportException e) {
236                 throw new MojoExecutionException(
237                         "Failed to determine whether report '" + reportMojoInfo + "' can be generated", e);
238             }
239         }
240         return reportExecutions;
241     }
242 
243     /**
244      * Get the report plugins from reporting section, adding if necessary (i.e. not excluded)
245      * default reports (i.e. maven-project-info-reports)
246      *
247      * @return the effective list of reports
248      * @since 3.7.1
249      */
250     private ReportPlugin[] getReportingPlugins() {
251         List<ReportPlugin> reportingPlugins = reporting.getPlugins();
252 
253         // MSITE-806: add default report plugin like done in maven-model-builder DefaultReportingConverter
254         boolean hasMavenProjectInfoReportsPlugin = false;
255         for (ReportPlugin plugin : reportingPlugins) {
256             if ("org.apache.maven.plugins".equals(plugin.getGroupId())
257                     && "maven-project-info-reports-plugin".equals(plugin.getArtifactId())) {
258                 hasMavenProjectInfoReportsPlugin = true;
259                 break;
260             }
261         }
262 
263         if (!reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin) {
264             ReportPlugin mpir = new ReportPlugin();
265             mpir.setArtifactId("maven-project-info-reports-plugin");
266             reportingPlugins.add(mpir);
267         }
268         return reportingPlugins.toArray(new ReportPlugin[0]);
269     }
270 
271     protected SiteRenderingContext createSiteRenderingContext(Locale locale)
272             throws MojoExecutionException, IOException, MojoFailureException {
273         SiteModel siteModel = prepareSiteModel(locale);
274         Map<String, Object> templateProperties = new HashMap<>();
275         templateProperties.put("project", project);
276         templateProperties.put("inputEncoding", getInputEncoding());
277         templateProperties.put("outputEncoding", getOutputEncoding());
278 
279         // Put any of the properties in directly into the Velocity context
280         for (Map.Entry<Object, Object> entry : project.getProperties().entrySet()) {
281             templateProperties.put((String) entry.getKey(), entry.getValue());
282         }
283 
284         // Comes last if someone wants to deliberately override any default or model properties
285         if (attributes != null) {
286             templateProperties.putAll(attributes);
287         }
288 
289         SiteRenderingContext context;
290         try {
291             Artifact skinArtifact =
292                     siteTool.getSkinArtifactFromRepository(repoSession, remoteProjectRepositories, siteModel.getSkin());
293 
294             getLog().info(buffer().a("Rendering content with ")
295                     .strong(skinArtifact.getId() + " skin")
296                     .toString());
297 
298             context = siteRenderer.createContextForSkin(
299                     skinArtifact, templateProperties, siteModel, project.getName(), locale);
300         } catch (SiteToolException e) {
301             throw new MojoExecutionException("Failed to retrieve skin artifact from repository", e);
302         } catch (RendererException e) {
303             throw new MojoExecutionException("Failed to create context for skin", e);
304         }
305 
306         // Add publish date
307         MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).ifPresent(v -> {
308             context.setPublishDate(Date.from(v));
309         });
310 
311         // Generate static site
312         context.setRootDirectory(project.getBasedir());
313         if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
314             context.addSiteDirectory(new File(siteDirectory, locale.toString()));
315         } else {
316             context.addSiteDirectory(siteDirectory);
317         }
318 
319         if (moduleExcludes != null) {
320             context.setModuleExcludes(moduleExcludes);
321         }
322 
323         if (saveProcessedContent) {
324             File processedDir = new File(generatedSiteDirectory, "processed");
325             if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
326                 context.setProcessedContentOutput(new File(processedDir, locale.toString()));
327             } else {
328                 context.setProcessedContentOutput(processedDir);
329             }
330         }
331 
332         return context;
333     }
334 
335     /**
336      * Go through the list of reports and process each one like this:
337      * <ul>
338      * <li>Add the report to a map of reports keyed by filename having the report itself as value
339      * <li>If the report is not yet in the map of documents, add it together with a suitable renderer
340      * </ul>
341      *
342      * @param reports A List of MavenReports
343      * @param documents A Map of documents, keyed by filename
344      * @param locale the Locale the reports are processed for.
345      * @return A map with all reports keyed by filename having the report itself as value.
346      * The map will be used to populate a menu.
347      */
348     protected Map<String, MavenReport> locateReports(
349             List<MavenReportExecution> reports, Map<String, DocumentRenderer> documents, Locale locale) {
350         Map<String, MavenReport> reportsByOutputName = new LinkedHashMap<>();
351         for (MavenReportExecution mavenReportExecution : reports) {
352             MavenReport report = mavenReportExecution.getMavenReport();
353 
354             String outputName = report.getOutputName();
355             String filename = outputName + ".html";
356 
357             // Always add the report to the menu, see MSITE-150
358             reportsByOutputName.put(outputName, report);
359 
360             if (documents.containsKey(filename)) {
361                 String reportMojoInfo = mavenReportExecution.getGoal() == null
362                         ? ""
363                         : (" (" + mavenReportExecution.getPlugin().getArtifactId() + ':'
364                                 + mavenReportExecution.getPlugin().getVersion() + ':' + mavenReportExecution.getGoal()
365                                 + ')');
366 
367                 getLog().info("Skipped \"" + report.getName(locale) + "\" report" + reportMojoInfo + ", file \""
368                         + filename + "\" already exists.");
369             } else {
370                 String generator = mavenReportExecution.getGoal() == null
371                         ? null
372                         : mavenReportExecution.getPlugin().getId() + ':' + mavenReportExecution.getGoal();
373                 DocumentRenderingContext docRenderingContext =
374                         new DocumentRenderingContext(siteDirectory, outputName, generator);
375                 DocumentRenderer docRenderer =
376                         new ReportDocumentRenderer(mavenReportExecution, docRenderingContext, getLog());
377                 documents.put(filename, docRenderer);
378             }
379         }
380         return reportsByOutputName;
381     }
382 
383     /**
384      * Go through the collection of reports and put each report into a list for the appropriate category. The list is
385      * put into a map keyed by the name of the category.
386      *
387      * @param reports A Collection of MavenReports
388      * @return A map keyed category having the report itself as value
389      */
390     protected Map<String, List<MavenReport>> categoriseReports(Collection<MavenReport> reports) {
391         Map<String, List<MavenReport>> categories = new LinkedHashMap<>();
392         for (MavenReport report : reports) {
393             List<MavenReport> categoryReports = categories.get(report.getCategoryName());
394             if (categoryReports == null) {
395                 categoryReports = new ArrayList<>();
396                 categories.put(report.getCategoryName(), categoryReports);
397             }
398             categoryReports.add(report);
399         }
400         return categories;
401     }
402 
403     /**
404      * Locate every document to be rendered for given locale:<ul>
405      * <li>handwritten content, ie Doxia files,</li>
406      * <li>reports,</li>
407      * <li>"Project Information" and "Project Reports" category summaries.</li>
408      * </ul>
409      *
410      * @param context the site context
411      * @param reports the documents
412      * @param locale the locale
413      * @return the documents and their renderers
414      * @throws IOException in case of file reading issue
415      * @throws RendererException in case of Doxia rendering issue
416      * @see CategorySummaryDocumentRenderer
417      */
418     protected Map<String, DocumentRenderer> locateDocuments(
419             SiteRenderingContext context, List<MavenReportExecution> reports, Locale locale)
420             throws IOException, RendererException {
421         Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles(context, true);
422 
423         Map<String, MavenReport> reportsByOutputName = locateReports(reports, documents, locale);
424 
425         // TODO: I want to get rid of categories eventually. There's no way to add your own in a fully i18n manner
426         Map<String, List<MavenReport>> categories = categoriseReports(reportsByOutputName.values());
427 
428         siteTool.populateReportsMenu(context.getSiteModel(), locale, categories);
429         populateReportItems(context.getSiteModel(), locale, reportsByOutputName);
430 
431         if (categories.containsKey(MavenReport.CATEGORY_PROJECT_INFORMATION) && generateProjectInfo) {
432             // add "Project Information" category summary document
433             List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_INFORMATION);
434             MojoExecution subMojoExecution =
435                     new MojoExecution(mojoExecution.getPlugin(), "project-info", mojoExecution.getExecutionId());
436             DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
437                     siteDirectory,
438                     subMojoExecution.getGoal(),
439                     subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
440             String title = i18n.getString("site-plugin", locale, "report.information.title");
441             String desc1 = i18n.getString("site-plugin", locale, "report.information.description1");
442             String desc2 = i18n.getString("site-plugin", locale, "report.information.description2");
443             DocumentRenderer docRenderer = new CategorySummaryDocumentRenderer(
444                     subMojoExecution, docRenderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
445 
446             String filename = docRenderer.getOutputName();
447             if (!documents.containsKey(filename)) {
448                 documents.put(filename, docRenderer);
449             } else {
450                 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
451             }
452         }
453 
454         if (categories.containsKey(MavenReport.CATEGORY_PROJECT_REPORTS)) {
455             // add "Project Reports" category summary document
456             List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_REPORTS);
457             MojoExecution subMojoExecution =
458                     new MojoExecution(mojoExecution.getPlugin(), "project-reports", mojoExecution.getExecutionId());
459             DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
460                     siteDirectory,
461                     subMojoExecution.getGoal(),
462                     subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
463             String title = i18n.getString("site-plugin", locale, "report.project.title");
464             String desc1 = i18n.getString("site-plugin", locale, "report.project.description1");
465             String desc2 = i18n.getString("site-plugin", locale, "report.project.description2");
466             DocumentRenderer docRenderer = new CategorySummaryDocumentRenderer(
467                     subMojoExecution, docRenderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
468 
469             String filename = docRenderer.getOutputName();
470             if (!documents.containsKey(filename)) {
471                 documents.put(filename, docRenderer);
472             } else {
473                 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
474             }
475         }
476 
477         if (generateSitemap) {
478             MojoExecution subMojoExecution =
479                     new MojoExecution(mojoExecution.getPlugin(), "sitemap", mojoExecution.getExecutionId());
480             DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
481                     siteDirectory,
482                     subMojoExecution.getGoal(),
483                     subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
484             String title = i18n.getString("site-plugin", locale, "site.sitemap.title");
485             DocumentRenderer docRenderer = new SitemapDocumentRenderer(
486                     subMojoExecution, docRenderingContext, title, context.getSiteModel(), i18n, getLog());
487 
488             String filename = docRenderer.getOutputName();
489             if (!documents.containsKey(filename)) {
490                 documents.put(filename, docRenderer);
491             } else {
492                 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
493             }
494         }
495 
496         return documents;
497     }
498 
499     protected void populateReportItems(
500             SiteModel siteModel, Locale locale, Map<String, MavenReport> reportsByOutputName) {
501         for (Menu menu : siteModel.getMenus()) {
502             populateItemRefs(menu.getItems(), locale, reportsByOutputName);
503         }
504     }
505 
506     private void populateItemRefs(List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName) {
507         for (Iterator<MenuItem> i = items.iterator(); i.hasNext(); ) {
508             MenuItem item = i.next();
509 
510             if (item.getRef() != null) {
511                 MavenReport report = reportsByOutputName.get(item.getRef());
512 
513                 if (report != null) {
514                     if (item.getName() == null) {
515                         item.setName(report.getName(locale));
516                     }
517 
518                     if (item.getHref() == null || item.getHref().length() == 0) {
519                         item.setHref(report.getOutputName() + ".html");
520                     }
521                 } else {
522                     getLog().warn("Unrecognised reference: '" + item.getRef() + "'");
523                     i.remove();
524                 }
525             }
526 
527             populateItemRefs(item.getItems(), locale, reportsByOutputName);
528         }
529     }
530 }