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.siterenderer.SiteRenderingContext.SiteDirectory;
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.MojoExecution;
50  import org.apache.maven.plugin.MojoExecutionException;
51  import org.apache.maven.plugin.MojoFailureException;
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.reporting.MavenReport;
56  import org.apache.maven.reporting.MavenReportException;
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.apache.maven.shared.utils.WriterFactory;
61  import org.codehaus.plexus.util.ReaderFactory;
62  
63  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
64  
65  /**
66   * Base class for site rendering mojos.
67   *
68   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
69   *
70   */
71  public abstract class AbstractSiteRenderingMojo extends AbstractSiteDescriptorMojo {
72      /**
73       * Module type exclusion mappings
74       * ex: <code>fml  -> **&#47;*-m1.fml</code>  (excludes fml files ending in '-m1.fml' recursively)
75       * <p/>
76       * The configuration looks like this:
77       * <pre>
78       *   &lt;moduleExcludes&gt;
79       *     &lt;moduleType&gt;filename1.ext,**&#47;*sample.ext&lt;/moduleType&gt;
80       *     &lt;!-- moduleType can be one of 'apt', 'fml' or 'xdoc'. --&gt;
81       *     &lt;!-- The value is a comma separated list of           --&gt;
82       *     &lt;!-- filenames or fileset patterns.                   --&gt;
83       *     &lt;!-- Here's an example:                               --&gt;
84       *     &lt;xdoc&gt;changes.xml,navigation.xml&lt;/xdoc&gt;
85       *   &lt;/moduleExcludes&gt;
86       * </pre>
87       */
88      @Parameter
89      private Map<String, String> moduleExcludes;
90  
91      /**
92       * Additional template properties for rendering the site. See
93       * <a href="/doxia/doxia-sitetools/doxia-site-renderer/">Doxia Site Renderer</a>.
94       */
95      @Parameter
96      private Map<String, Object> attributes;
97  
98      /**
99       * Site renderer.
100      */
101     @Component
102     protected SiteRenderer siteRenderer;
103 
104     /**
105      * Directory containing generated documentation in source format (Doxia supported markup).
106      * This is used to pick up other source docs that might have been generated at build time (by reports or any other
107      * build time mean).
108      * This directory is expected to have the same structure as <code>siteDirectory</code>
109      * (ie. one directory per Doxia-source-supported markup types).
110      *
111      * todo should we deprecate in favour of reports directly using Doxia Sink API, without this Doxia source
112      * intermediate step?
113      */
114     @Parameter(alias = "workingDirectory", defaultValue = "${project.build.directory}/generated-site")
115     protected File generatedSiteDirectory;
116 
117     /**
118      * The current Maven session.
119      */
120     @Parameter(defaultValue = "${session}", readonly = true, required = true)
121     protected MavenSession mavenSession;
122 
123     /**
124      * The mojo execution
125      */
126     @Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
127     protected MojoExecution mojoExecution;
128 
129     /**
130      * replaces previous reportPlugins parameter, that was injected by Maven core from
131      * reporting section: but this new configuration format has been abandoned.
132      *
133      * @since 3.7.1
134      */
135     @Parameter(defaultValue = "${project.reporting}", readonly = true)
136     private Reporting reporting;
137 
138     /**
139      * Whether to generate the summary page for project reports: project-info.html.
140      *
141      * @since 2.3
142      */
143     @Parameter(property = "generateProjectInfo", defaultValue = "true")
144     private boolean generateProjectInfo;
145 
146     /**
147      * Generate a sitemap. The result will be a "sitemap.html" file at the site root.
148      *
149      * @since 2.1
150      */
151     @Parameter(property = "generateSitemap", defaultValue = "false")
152     private boolean generateSitemap;
153 
154     /**
155      * Specifies the input encoding.
156      *
157      * @since 2.3
158      */
159     @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
160     private String inputEncoding;
161 
162     /**
163      * Specifies the output encoding.
164      *
165      * @since 2.3
166      */
167     @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}")
168     private String outputEncoding;
169 
170     /**
171      * Timestamp for reproducible output archive entries, either formatted as ISO 8601
172      * <code>yyyy-MM-dd'T'HH:mm:ssXXX</code> or as an int representing seconds since the epoch (like
173      * <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
174      *
175      * @since 3.9.0
176      */
177     @Parameter(defaultValue = "${project.build.outputTimestamp}")
178     protected String outputTimestamp;
179 
180     @Component
181     protected MavenReportExecutor mavenReportExecutor;
182 
183     /**
184      * Gets the input files encoding.
185      *
186      * @return The input files encoding, never <code>null</code>.
187      */
188     protected String getInputEncoding() {
189         return (inputEncoding == null || inputEncoding.isEmpty()) ? ReaderFactory.FILE_ENCODING : inputEncoding;
190     }
191 
192     /**
193      * Gets the effective reporting output files encoding.
194      *
195      * @return The effective reporting output file encoding, never <code>null</code>.
196      */
197     protected String getOutputEncoding() {
198         return (outputEncoding == null) ? WriterFactory.UTF_8 : outputEncoding;
199     }
200 
201     /**
202      * Whether to save Velocity processed Doxia content (<code>*.&lt;ext&gt;.vm</code>)
203      * to <code>${generatedSiteDirectory}/processed</code>.
204      *
205      * @since 3.5
206      */
207     @Parameter
208     private boolean saveProcessedContent;
209 
210     protected void checkInputEncoding() {
211         if (inputEncoding == null || inputEncoding.isEmpty()) {
212             getLog().warn("Input file encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
213                     + ", i.e. build is platform dependent!");
214         }
215     }
216 
217     protected List<MavenReportExecution> getReports(File outputDirectory) throws MojoExecutionException {
218         MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest();
219         mavenReportExecutorRequest.setMavenSession(mavenSession);
220         mavenReportExecutorRequest.setExecutionId(mojoExecution.getExecutionId());
221         mavenReportExecutorRequest.setProject(project);
222         mavenReportExecutorRequest.setReportPlugins(getReportingPlugins());
223 
224         List<MavenReportExecution> allReports = mavenReportExecutor.buildMavenReports(mavenReportExecutorRequest);
225 
226         // filter out reports that can't be generated
227         List<MavenReportExecution> reportExecutions = new ArrayList<>(allReports.size());
228         for (MavenReportExecution exec : allReports) {
229             String reportMojoInfo = exec.getPlugin().getId() + ":" + exec.getGoal();
230             exec.getMavenReport().setReportOutputDirectory(outputDirectory);
231             try {
232                 if (exec.canGenerateReport()) {
233                     reportExecutions.add(exec);
234                 } else if (exec.isUserDefined()) {
235                     getLog().info("Skipping " + reportMojoInfo + " report");
236                 }
237             } catch (MavenReportException e) {
238                 throw new MojoExecutionException(
239                         "Failed to determine whether report '" + reportMojoInfo + "' can be generated", e);
240             }
241         }
242         return reportExecutions;
243     }
244 
245     /**
246      * Get the report plugins from reporting section, adding if necessary (i.e. not excluded)
247      * default reports (i.e. maven-project-info-reports)
248      *
249      * @return the effective list of reports
250      * @since 3.7.1
251      */
252     private ReportPlugin[] getReportingPlugins() {
253         List<ReportPlugin> reportingPlugins = reporting.getPlugins();
254 
255         // MSITE-806: add default report plugin like done in maven-model-builder DefaultReportingConverter
256         boolean hasMavenProjectInfoReportsPlugin = false;
257         for (ReportPlugin plugin : reportingPlugins) {
258             if ("org.apache.maven.plugins".equals(plugin.getGroupId())
259                     && "maven-project-info-reports-plugin".equals(plugin.getArtifactId())) {
260                 hasMavenProjectInfoReportsPlugin = true;
261                 break;
262             }
263         }
264 
265         if (!reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin) {
266             ReportPlugin mpir = new ReportPlugin();
267             mpir.setArtifactId("maven-project-info-reports-plugin");
268             reportingPlugins.add(mpir);
269         }
270         return reportingPlugins.toArray(new ReportPlugin[0]);
271     }
272 
273     protected SiteRenderingContext createSiteRenderingContext(Locale locale)
274             throws MojoExecutionException, IOException, MojoFailureException {
275         SiteModel siteModel = prepareSiteModel(locale);
276         Map<String, Object> templateProperties = new HashMap<>();
277         templateProperties.put("project", project);
278         templateProperties.put("inputEncoding", getInputEncoding());
279         templateProperties.put("outputEncoding", getOutputEncoding());
280 
281         // Put any of the properties in directly into the Velocity context
282         for (Map.Entry<Object, Object> entry : project.getProperties().entrySet()) {
283             templateProperties.put((String) entry.getKey(), entry.getValue());
284         }
285 
286         // Comes last if someone wants to deliberately override any default or model properties
287         if (attributes != null) {
288             templateProperties.putAll(attributes);
289         }
290 
291         SiteRenderingContext context;
292         try {
293             Artifact skinArtifact =
294                     siteTool.getSkinArtifactFromRepository(repoSession, remoteProjectRepositories, siteModel.getSkin());
295 
296             getLog().info(buffer().a("Rendering content with ")
297                     .strong(skinArtifact.getId() + " skin")
298                     .toString());
299 
300             context = siteRenderer.createContextForSkin(
301                     skinArtifact, templateProperties, siteModel, project.getName(), locale);
302         } catch (SiteToolException e) {
303             throw new MojoExecutionException("Failed to retrieve skin artifact from repository", e);
304         } catch (RendererException e) {
305             throw new MojoExecutionException("Failed to create context for skin", e);
306         }
307 
308         // Add publish date
309         MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).ifPresent(v -> {
310             context.setPublishDate(Date.from(v));
311         });
312 
313         // Generate static site
314         context.setRootDirectory(project.getBasedir());
315         if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
316             context.addSiteDirectory(new SiteDirectory(new File(siteDirectory, locale.toString()), true));
317             context.addSiteDirectory(new SiteDirectory(new File(generatedSiteDirectory, locale.toString()), false));
318         } else {
319             context.addSiteDirectory(new SiteDirectory(siteDirectory, true));
320             context.addSiteDirectory(new SiteDirectory(generatedSiteDirectory, false));
321         }
322 
323         if (moduleExcludes != null) {
324             context.setModuleExcludes(moduleExcludes);
325         }
326 
327         if (saveProcessedContent) {
328             File processedDir = new File(generatedSiteDirectory, "processed");
329             if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
330                 context.setProcessedContentOutput(new File(processedDir, locale.toString()));
331             } else {
332                 context.setProcessedContentOutput(processedDir);
333             }
334         }
335 
336         return context;
337     }
338 
339     /**
340      * Go through the list of reports and process each one like this:
341      * <ul>
342      * <li>Add the report to a map of reports keyed by filename having the report itself as value
343      * <li>If the report is not yet in the map of documents, add it together with a suitable renderer
344      * </ul>
345      *
346      * @param reports A List of MavenReports
347      * @param documents A Map of documents, keyed by filename
348      * @param locale the Locale the reports are processed for.
349      * @return A map with all reports keyed by filename having the report itself as value.
350      * The map will be used to populate a menu.
351      */
352     protected Map<String, MavenReport> locateReports(
353             List<MavenReportExecution> reports, Map<String, DocumentRenderer> documents, Locale locale) {
354         Map<String, MavenReport> reportsByOutputName = new LinkedHashMap<>();
355         for (MavenReportExecution mavenReportExecution : reports) {
356             MavenReport report = mavenReportExecution.getMavenReport();
357 
358             String outputName = report.getOutputName();
359             String filename = outputName + ".html";
360 
361             // Always add the report to the menu, see MSITE-150
362             reportsByOutputName.put(outputName, report);
363 
364             if (documents.containsKey(filename)) {
365                 String reportMojoInfo = mavenReportExecution.getGoal() == null
366                         ? ""
367                         : (" (" + mavenReportExecution.getPlugin().getArtifactId() + ':'
368                                 + mavenReportExecution.getPlugin().getVersion() + ':' + mavenReportExecution.getGoal()
369                                 + ')');
370 
371                 getLog().info("Skipped \"" + report.getName(locale) + "\" report" + reportMojoInfo + ", file \""
372                         + filename + "\" already exists.");
373             } else {
374                 File localizedSiteDirectory;
375                 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
376                     localizedSiteDirectory = new File(siteDirectory, locale.toString());
377                 } else {
378                     localizedSiteDirectory = siteDirectory;
379                 }
380                 String generator = mavenReportExecution.getGoal() == null
381                         ? null
382                         : mavenReportExecution.getPlugin().getId() + ':' + mavenReportExecution.getGoal();
383                 DocumentRenderingContext docRenderingContext =
384                         new DocumentRenderingContext(localizedSiteDirectory, outputName, generator);
385                 DocumentRenderer docRenderer =
386                         new ReportDocumentRenderer(mavenReportExecution, docRenderingContext, getLog());
387                 documents.put(filename, docRenderer);
388             }
389         }
390         return reportsByOutputName;
391     }
392 
393     /**
394      * Go through the collection of reports and put each report into a list for the appropriate category. The list is
395      * put into a map keyed by the name of the category.
396      *
397      * @param reports A Collection of MavenReports
398      * @return A map keyed category having the report itself as value
399      */
400     protected Map<String, List<MavenReport>> categoriseReports(Collection<MavenReport> reports) {
401         Map<String, List<MavenReport>> categories = new LinkedHashMap<>();
402         for (MavenReport report : reports) {
403             List<MavenReport> categoryReports = categories.get(report.getCategoryName());
404             if (categoryReports == null) {
405                 categoryReports = new ArrayList<>();
406                 categories.put(report.getCategoryName(), categoryReports);
407             }
408             categoryReports.add(report);
409         }
410         return categories;
411     }
412 
413     /**
414      * Locate every document to be rendered for given locale:<ul>
415      * <li>handwritten content, ie Doxia files,</li>
416      * <li>reports,</li>
417      * <li>"Project Information" and "Project Reports" category summaries.</li>
418      * </ul>
419      *
420      * @param context the site context
421      * @param reports the documents
422      * @param locale the locale
423      * @return the documents and their renderers
424      * @throws IOException in case of file reading issue
425      * @throws RendererException in case of Doxia rendering issue
426      * @see CategorySummaryDocumentRenderer
427      */
428     protected Map<String, DocumentRenderer> locateDocuments(
429             SiteRenderingContext context, List<MavenReportExecution> reports, Locale locale)
430             throws IOException, RendererException {
431         Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles(context);
432 
433         Map<String, MavenReport> reportsByOutputName = locateReports(reports, documents, locale);
434 
435         // TODO: I want to get rid of categories eventually. There's no way to add your own in a fully i18n manner
436         Map<String, List<MavenReport>> categories = categoriseReports(reportsByOutputName.values());
437 
438         siteTool.populateReportsMenu(context.getSiteModel(), locale, categories);
439         populateReportItems(context.getSiteModel(), locale, reportsByOutputName);
440 
441         File localizedSiteDirectory;
442         if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
443             localizedSiteDirectory = new File(siteDirectory, locale.toString());
444         } else {
445             localizedSiteDirectory = siteDirectory;
446         }
447 
448         if (categories.containsKey(MavenReport.CATEGORY_PROJECT_INFORMATION) && generateProjectInfo) {
449             // add "Project Information" category summary document
450             List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_INFORMATION);
451             MojoExecution subMojoExecution =
452                     new MojoExecution(mojoExecution.getPlugin(), "project-info", mojoExecution.getExecutionId());
453             DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
454                     localizedSiteDirectory,
455                     subMojoExecution.getGoal(),
456                     subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
457             String title = i18n.getString("site-plugin", locale, "report.information.title");
458             String desc1 = i18n.getString("site-plugin", locale, "report.information.description1");
459             String desc2 = i18n.getString("site-plugin", locale, "report.information.description2");
460             DocumentRenderer docRenderer = new CategorySummaryDocumentRenderer(
461                     subMojoExecution, docRenderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
462 
463             String filename = docRenderer.getOutputName();
464             if (!documents.containsKey(filename)) {
465                 documents.put(filename, docRenderer);
466             } else {
467                 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
468             }
469         }
470 
471         if (categories.containsKey(MavenReport.CATEGORY_PROJECT_REPORTS)) {
472             // add "Project Reports" category summary document
473             List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_REPORTS);
474             MojoExecution subMojoExecution =
475                     new MojoExecution(mojoExecution.getPlugin(), "project-reports", mojoExecution.getExecutionId());
476             DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
477                     localizedSiteDirectory,
478                     subMojoExecution.getGoal(),
479                     subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
480             String title = i18n.getString("site-plugin", locale, "report.project.title");
481             String desc1 = i18n.getString("site-plugin", locale, "report.project.description1");
482             String desc2 = i18n.getString("site-plugin", locale, "report.project.description2");
483             DocumentRenderer docRenderer = new CategorySummaryDocumentRenderer(
484                     subMojoExecution, docRenderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
485 
486             String filename = docRenderer.getOutputName();
487             if (!documents.containsKey(filename)) {
488                 documents.put(filename, docRenderer);
489             } else {
490                 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
491             }
492         }
493 
494         if (generateSitemap) {
495             MojoExecution subMojoExecution =
496                     new MojoExecution(mojoExecution.getPlugin(), "sitemap", mojoExecution.getExecutionId());
497             DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
498                     localizedSiteDirectory,
499                     subMojoExecution.getGoal(),
500                     subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
501             String title = i18n.getString("site-plugin", locale, "site.sitemap.title");
502             DocumentRenderer docRenderer = new SitemapDocumentRenderer(
503                     subMojoExecution, docRenderingContext, title, context.getSiteModel(), i18n, getLog());
504 
505             String filename = docRenderer.getOutputName();
506             if (!documents.containsKey(filename)) {
507                 documents.put(filename, docRenderer);
508             } else {
509                 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
510             }
511         }
512 
513         return documents;
514     }
515 
516     protected void populateReportItems(
517             SiteModel siteModel, Locale locale, Map<String, MavenReport> reportsByOutputName) {
518         for (Menu menu : siteModel.getMenus()) {
519             populateItemRefs(menu.getItems(), locale, reportsByOutputName);
520         }
521     }
522 
523     private void populateItemRefs(List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName) {
524         for (Iterator<MenuItem> i = items.iterator(); i.hasNext(); ) {
525             MenuItem item = i.next();
526 
527             if (item.getRef() != null) {
528                 MavenReport report = reportsByOutputName.get(item.getRef());
529 
530                 if (report != null) {
531                     if (item.getName() == null) {
532                         item.setName(report.getName(locale));
533                     }
534 
535                     if (item.getHref() == null || item.getHref().length() == 0) {
536                         item.setHref(report.getOutputName() + ".html");
537                     }
538                 } else {
539                     getLog().warn("Unrecognised reference: '" + item.getRef() + "'");
540                     i.remove();
541                 }
542             }
543 
544             populateItemRefs(item.getItems(), locale, reportsByOutputName);
545         }
546     }
547 }