1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.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.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
66
67
68
69
70 public abstract class AbstractSiteRenderingMojo extends AbstractSiteDescriptorMojo {
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 @Parameter
88 private Map<String, String> moduleExcludes;
89
90
91
92
93
94 @Parameter
95 private Map<String, Object> attributes;
96
97
98
99
100 @Component
101 protected SiteRenderer siteRenderer;
102
103
104
105
106
107
108
109
110
111
112
113 @Parameter(alias = "workingDirectory", defaultValue = "${project.build.directory}/generated-site")
114 protected File generatedSiteDirectory;
115
116
117
118
119 @Parameter(defaultValue = "${session}", readonly = true, required = true)
120 protected MavenSession mavenSession;
121
122
123
124
125 @Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
126 protected MojoExecution mojoExecution;
127
128
129
130
131
132
133
134 @Parameter(defaultValue = "${project.reporting}", readonly = true)
135 private Reporting reporting;
136
137
138
139
140
141
142 @Parameter(property = "generateProjectInfo", defaultValue = "true")
143 private boolean generateProjectInfo;
144
145
146
147
148
149
150 @Parameter(property = "generateSitemap", defaultValue = "false")
151 private boolean generateSitemap;
152
153
154
155
156
157
158 @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
159 private String inputEncoding;
160
161
162
163
164
165
166 @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}")
167 private String outputEncoding;
168
169 @Component
170 protected MavenReportExecutor mavenReportExecutor;
171
172
173
174
175
176
177 protected String getInputEncoding() {
178 return (StringUtils.isEmpty(inputEncoding)) ? ReaderFactory.FILE_ENCODING : inputEncoding;
179 }
180
181
182
183
184
185
186 protected String getOutputEncoding() {
187 return (outputEncoding == null) ? ReaderFactory.UTF_8 : outputEncoding;
188 }
189
190
191
192
193
194
195
196 @Parameter
197 private boolean saveProcessedContent;
198
199 protected void checkInputEncoding() {
200 if (StringUtils.isEmpty(inputEncoding)) {
201 getLog().warn("Input file encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
202 + ", i.e. build is platform dependent!");
203 }
204 }
205
206 protected List<MavenReportExecution> getReports() throws MojoExecutionException {
207 MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest();
208 mavenReportExecutorRequest.setMavenSession(mavenSession);
209 mavenReportExecutorRequest.setExecutionId(mojoExecution.getExecutionId());
210 mavenReportExecutorRequest.setProject(project);
211 mavenReportExecutorRequest.setReportPlugins(getReportingPlugins());
212
213 List<MavenReportExecution> allReports = mavenReportExecutor.buildMavenReports(mavenReportExecutorRequest);
214
215
216 List<MavenReportExecution> reportExecutions = new ArrayList<>(allReports.size());
217 for (MavenReportExecution exec : allReports) {
218 if (exec.canGenerateReport()) {
219 reportExecutions.add(exec);
220 }
221 }
222 return reportExecutions;
223 }
224
225
226
227
228
229
230
231
232 private ReportPlugin[] getReportingPlugins() {
233 List<ReportPlugin> reportingPlugins = reporting.getPlugins();
234
235
236 boolean hasMavenProjectInfoReportsPlugin = false;
237 for (ReportPlugin plugin : reportingPlugins) {
238 if ("org.apache.maven.plugins".equals(plugin.getGroupId())
239 && "maven-project-info-reports-plugin".equals(plugin.getArtifactId())) {
240 hasMavenProjectInfoReportsPlugin = true;
241 break;
242 }
243 }
244
245 if (!reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin) {
246 ReportPlugin mpir = new ReportPlugin();
247 mpir.setArtifactId("maven-project-info-reports-plugin");
248 reportingPlugins.add(mpir);
249 }
250 return reportingPlugins.toArray(new ReportPlugin[0]);
251 }
252
253 protected SiteRenderingContext createSiteRenderingContext(Locale locale)
254 throws MojoExecutionException, IOException, MojoFailureException {
255 SiteModel siteModel = prepareSiteModel(locale);
256 if (attributes == null) {
257 attributes = new HashMap<>();
258 }
259
260 if (attributes.get("project") == null) {
261 attributes.put("project", project);
262 }
263
264 if (attributes.get("inputEncoding") == null) {
265 attributes.put("inputEncoding", getInputEncoding());
266 }
267
268 if (attributes.get("outputEncoding") == null) {
269 attributes.put("outputEncoding", getOutputEncoding());
270 }
271
272
273 for (Map.Entry<Object, Object> entry : project.getProperties().entrySet()) {
274 attributes.put((String) entry.getKey(), entry.getValue());
275 }
276
277 SiteRenderingContext context;
278 try {
279 Artifact skinArtifact =
280 siteTool.getSkinArtifactFromRepository(repoSession, remoteProjectRepositories, siteModel.getSkin());
281
282 getLog().info(buffer().a("Rendering content with ")
283 .strong(skinArtifact.getId() + " skin")
284 .toString());
285
286 context = siteRenderer.createContextForSkin(skinArtifact, attributes, siteModel, project.getName(), locale);
287 } catch (SiteToolException e) {
288 throw new MojoExecutionException("SiteToolException while preparing skin: " + e.getMessage(), e);
289 } catch (RendererException e) {
290 throw new MojoExecutionException(
291 "RendererException while preparing context for skin: " + e.getMessage(), e);
292 }
293
294
295 MavenProject p = attributes.get("project") != null ? (MavenProject) attributes.get("project") : project;
296 String outputTimestamp = p.getProperties().getProperty("project.build.outputTimestamp");
297 MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).ifPresent(v -> {
298 context.setPublishDate(Date.from(v));
299 });
300
301
302 context.setRootDirectory(project.getBasedir());
303 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
304 context.addSiteDirectory(new File(siteDirectory, locale.toString()));
305 } else {
306 context.addSiteDirectory(siteDirectory);
307 }
308
309 if (moduleExcludes != null) {
310 context.setModuleExcludes(moduleExcludes);
311 }
312
313 if (saveProcessedContent) {
314 File processedDir = new File(generatedSiteDirectory, "processed");
315 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
316 context.setProcessedContentOutput(new File(processedDir, locale.toString()));
317 } else {
318 context.setProcessedContentOutput(processedDir);
319 }
320 }
321
322 return context;
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 protected Map<String, MavenReport> locateReports(
339 List<MavenReportExecution> reports, Map<String, DocumentRenderer> documents, Locale locale) {
340 Map<String, MavenReport> reportsByOutputName = new LinkedHashMap<>();
341 for (MavenReportExecution mavenReportExecution : reports) {
342 MavenReport report = mavenReportExecution.getMavenReport();
343
344 String outputName = report.getOutputName();
345 String filename = outputName + ".html";
346
347
348 reportsByOutputName.put(outputName, report);
349
350 if (documents.containsKey(filename)) {
351 String reportMojoInfo = mavenReportExecution.getGoal() == null
352 ? ""
353 : (" (" + mavenReportExecution.getPlugin().getArtifactId() + ':'
354 + mavenReportExecution.getPlugin().getVersion() + ':' + mavenReportExecution.getGoal()
355 + ')');
356
357 getLog().info("Skipped \"" + report.getName(locale) + "\" report" + reportMojoInfo + ", file \""
358 + filename + "\" already exists.");
359 } else {
360 String generator = mavenReportExecution.getGoal() == null
361 ? null
362 : mavenReportExecution.getPlugin().getId() + ':' + mavenReportExecution.getGoal();
363 DocumentRenderingContext docRenderingContext =
364 new DocumentRenderingContext(siteDirectory, outputName, generator);
365 DocumentRenderer docRenderer =
366 new ReportDocumentRenderer(mavenReportExecution, docRenderingContext, getLog());
367 documents.put(filename, docRenderer);
368 }
369 }
370 return reportsByOutputName;
371 }
372
373
374
375
376
377
378
379
380 protected Map<String, List<MavenReport>> categoriseReports(Collection<MavenReport> reports) {
381 Map<String, List<MavenReport>> categories = new LinkedHashMap<>();
382 for (MavenReport report : reports) {
383 List<MavenReport> categoryReports = categories.get(report.getCategoryName());
384 if (categoryReports == null) {
385 categoryReports = new ArrayList<>();
386 categories.put(report.getCategoryName(), categoryReports);
387 }
388 categoryReports.add(report);
389 }
390 return categories;
391 }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408 protected Map<String, DocumentRenderer> locateDocuments(
409 SiteRenderingContext context, List<MavenReportExecution> reports, Locale locale)
410 throws IOException, RendererException {
411 Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles(context, true);
412
413 Map<String, MavenReport> reportsByOutputName = locateReports(reports, documents, locale);
414
415
416 Map<String, List<MavenReport>> categories = categoriseReports(reportsByOutputName.values());
417
418 siteTool.populateReportsMenu(context.getSiteModel(), locale, categories);
419 populateReportItems(context.getSiteModel(), locale, reportsByOutputName);
420
421 if (categories.containsKey(MavenReport.CATEGORY_PROJECT_INFORMATION) && generateProjectInfo) {
422
423 List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_INFORMATION);
424 MojoExecution subMojoExecution =
425 new MojoExecution(mojoExecution.getPlugin(), "project-info", mojoExecution.getExecutionId());
426 DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
427 siteDirectory,
428 subMojoExecution.getGoal(),
429 subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
430 String title = i18n.getString("site-plugin", locale, "report.information.title");
431 String desc1 = i18n.getString("site-plugin", locale, "report.information.description1");
432 String desc2 = i18n.getString("site-plugin", locale, "report.information.description2");
433 DocumentRenderer docRenderer = new CategorySummaryDocumentRenderer(
434 subMojoExecution, docRenderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
435
436 String filename = docRenderer.getOutputName();
437 if (!documents.containsKey(filename)) {
438 documents.put(filename, docRenderer);
439 } else {
440 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
441 }
442 }
443
444 if (categories.containsKey(MavenReport.CATEGORY_PROJECT_REPORTS)) {
445
446 List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_REPORTS);
447 MojoExecution subMojoExecution =
448 new MojoExecution(mojoExecution.getPlugin(), "project-reports", mojoExecution.getExecutionId());
449 DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
450 siteDirectory,
451 subMojoExecution.getGoal(),
452 subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
453 String title = i18n.getString("site-plugin", locale, "report.project.title");
454 String desc1 = i18n.getString("site-plugin", locale, "report.project.description1");
455 String desc2 = i18n.getString("site-plugin", locale, "report.project.description2");
456 DocumentRenderer docRenderer = new CategorySummaryDocumentRenderer(
457 subMojoExecution, docRenderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
458
459 String filename = docRenderer.getOutputName();
460 if (!documents.containsKey(filename)) {
461 documents.put(filename, docRenderer);
462 } else {
463 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
464 }
465 }
466
467 if (generateSitemap) {
468 MojoExecution subMojoExecution =
469 new MojoExecution(mojoExecution.getPlugin(), "sitemap", mojoExecution.getExecutionId());
470 DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
471 siteDirectory,
472 subMojoExecution.getGoal(),
473 subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
474 String title = i18n.getString("site-plugin", locale, "site.sitemap.title");
475 DocumentRenderer docRenderer = new SitemapDocumentRenderer(
476 subMojoExecution, docRenderingContext, title, context.getSiteModel(), i18n, getLog());
477
478 String filename = docRenderer.getOutputName();
479 if (!documents.containsKey(filename)) {
480 documents.put(filename, docRenderer);
481 } else {
482 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
483 }
484 }
485
486 return documents;
487 }
488
489 protected void populateReportItems(
490 SiteModel siteModel, Locale locale, Map<String, MavenReport> reportsByOutputName) {
491 for (Menu menu : siteModel.getMenus()) {
492 populateItemRefs(menu.getItems(), locale, reportsByOutputName);
493 }
494 }
495
496 private void populateItemRefs(List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName) {
497 for (Iterator<MenuItem> i = items.iterator(); i.hasNext(); ) {
498 MenuItem item = i.next();
499
500 if (item.getRef() != null) {
501 MavenReport report = reportsByOutputName.get(item.getRef());
502
503 if (report != null) {
504 if (item.getName() == null) {
505 item.setName(report.getName(locale));
506 }
507
508 if (item.getHref() == null || item.getHref().length() == 0) {
509 item.setHref(report.getOutputName() + ".html");
510 }
511 } else {
512 getLog().warn("Unrecognised reference: '" + item.getRef() + "'");
513 i.remove();
514 }
515 }
516
517 populateItemRefs(item.getItems(), locale, reportsByOutputName);
518 }
519 }
520 }