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.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
67
68
69
70
71 public abstract class AbstractSiteRenderingMojo extends AbstractSiteDescriptorMojo {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @Parameter
89 private Map<String, String> moduleExcludes;
90
91
92
93
94
95 @Parameter
96 private Map<String, Object> attributes;
97
98
99
100
101 @Component
102 protected SiteRenderer siteRenderer;
103
104
105
106
107
108
109
110
111
112
113
114 @Parameter(alias = "workingDirectory", defaultValue = "${project.build.directory}/generated-site")
115 protected File generatedSiteDirectory;
116
117
118
119
120 @Parameter(defaultValue = "${session}", readonly = true, required = true)
121 protected MavenSession mavenSession;
122
123
124
125
126 @Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
127 protected MojoExecution mojoExecution;
128
129
130
131
132
133
134
135 @Parameter(defaultValue = "${project.reporting}", readonly = true)
136 private Reporting reporting;
137
138
139
140
141
142
143 @Parameter(property = "generateProjectInfo", defaultValue = "true")
144 private boolean generateProjectInfo;
145
146
147
148
149
150
151 @Parameter(property = "generateSitemap", defaultValue = "false")
152 private boolean generateSitemap;
153
154
155
156
157
158
159 @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
160 private String inputEncoding;
161
162
163
164
165
166
167 @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}")
168 private String outputEncoding;
169
170
171
172
173
174
175
176
177 @Parameter(defaultValue = "${project.build.outputTimestamp}")
178 protected String outputTimestamp;
179
180 @Component
181 protected MavenReportExecutor mavenReportExecutor;
182
183
184
185
186
187
188 protected String getInputEncoding() {
189 return (inputEncoding == null || inputEncoding.isEmpty()) ? ReaderFactory.FILE_ENCODING : inputEncoding;
190 }
191
192
193
194
195
196
197 protected String getOutputEncoding() {
198 return (outputEncoding == null) ? WriterFactory.UTF_8 : outputEncoding;
199 }
200
201
202
203
204
205
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
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
247
248
249
250
251
252 private ReportPlugin[] getReportingPlugins() {
253 List<ReportPlugin> reportingPlugins = reporting.getPlugins();
254
255
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
282 for (Map.Entry<Object, Object> entry : project.getProperties().entrySet()) {
283 templateProperties.put((String) entry.getKey(), entry.getValue());
284 }
285
286
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
309 MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).ifPresent(v -> {
310 context.setPublishDate(Date.from(v));
311 });
312
313
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
341
342
343
344
345
346
347
348
349
350
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
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
395
396
397
398
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
415
416
417
418
419
420
421
422
423
424
425
426
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
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
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
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 }