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.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
170
171
172
173
174
175
176 @Parameter(defaultValue = "${project.build.outputTimestamp}")
177 protected String outputTimestamp;
178
179 @Component
180 protected MavenReportExecutor mavenReportExecutor;
181
182
183
184
185
186
187 protected String getInputEncoding() {
188 return (inputEncoding == null || inputEncoding.isEmpty()) ? ReaderFactory.FILE_ENCODING : inputEncoding;
189 }
190
191
192
193
194
195
196 protected String getOutputEncoding() {
197 return outputEncoding == null ? "UTF-8" : outputEncoding;
198 }
199
200
201
202
203
204
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(File outputDirectory) 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
226 List<MavenReportExecution> reportExecutions = new ArrayList<>(allReports.size());
227 for (MavenReportExecution exec : allReports) {
228 String reportMojoInfo = exec.getPlugin().getId() + ":" + exec.getGoal();
229 exec.getMavenReport().setReportOutputDirectory(outputDirectory);
230 try {
231 if (exec.canGenerateReport()) {
232 reportExecutions.add(exec);
233 } else if (exec.isUserDefined()) {
234 getLog().info("Skipping " + reportMojoInfo + " report");
235 }
236 } catch (MavenReportException e) {
237 throw new MojoExecutionException(
238 "Failed to determine whether report '" + reportMojoInfo + "' can be generated", e);
239 }
240 }
241 return reportExecutions;
242 }
243
244
245
246
247
248
249
250
251 private ReportPlugin[] getReportingPlugins() {
252 List<ReportPlugin> reportingPlugins = reporting.getPlugins();
253
254
255 boolean hasMavenProjectInfoReportsPlugin = false;
256 for (ReportPlugin plugin : reportingPlugins) {
257 if ("org.apache.maven.plugins".equals(plugin.getGroupId())
258 && "maven-project-info-reports-plugin".equals(plugin.getArtifactId())) {
259 hasMavenProjectInfoReportsPlugin = true;
260 break;
261 }
262 }
263
264 if (!reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin) {
265 ReportPlugin mpir = new ReportPlugin();
266 mpir.setArtifactId("maven-project-info-reports-plugin");
267 reportingPlugins.add(mpir);
268 }
269 return reportingPlugins.toArray(new ReportPlugin[0]);
270 }
271
272 protected SiteRenderingContext createSiteRenderingContext(Locale locale)
273 throws MojoExecutionException, IOException, MojoFailureException {
274 SiteModel siteModel = prepareSiteModel(locale);
275 Map<String, Object> templateProperties = new HashMap<>();
276 templateProperties.put("project", project);
277 templateProperties.put("inputEncoding", getInputEncoding());
278 templateProperties.put("outputEncoding", getOutputEncoding());
279
280
281 for (Map.Entry<Object, Object> entry : project.getProperties().entrySet()) {
282 templateProperties.put((String) entry.getKey(), entry.getValue());
283 }
284
285
286 if (attributes != null) {
287 templateProperties.putAll(attributes);
288 }
289
290 SiteRenderingContext context;
291 try {
292 Artifact skinArtifact =
293 siteTool.getSkinArtifactFromRepository(repoSession, remoteProjectRepositories, siteModel.getSkin());
294
295 getLog().info(buffer().a("Rendering content with ")
296 .strong(skinArtifact.getId() + " skin")
297 .build());
298
299 context = siteRenderer.createContextForSkin(
300 skinArtifact, templateProperties, siteModel, project.getName(), locale);
301 } catch (SiteToolException e) {
302 throw new MojoExecutionException("Failed to retrieve skin artifact from repository", e);
303 } catch (RendererException e) {
304 throw new MojoExecutionException("Failed to create context for skin", e);
305 }
306
307
308 MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).ifPresent(v -> {
309 context.setPublishDate(Date.from(v));
310 });
311
312
313 context.setRootDirectory(project.getBasedir());
314 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
315 context.addSiteDirectory(new SiteDirectory(new File(siteDirectory, locale.toString()), true));
316 context.addSiteDirectory(new SiteDirectory(new File(generatedSiteDirectory, locale.toString()), false));
317 } else {
318 context.addSiteDirectory(new SiteDirectory(siteDirectory, true));
319 context.addSiteDirectory(new SiteDirectory(generatedSiteDirectory, false));
320 }
321
322 if (moduleExcludes != null) {
323 context.setModuleExcludes(moduleExcludes);
324 }
325
326 if (saveProcessedContent) {
327 File processedDir = new File(generatedSiteDirectory, "processed");
328 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
329 context.setProcessedContentOutput(new File(processedDir, locale.toString()));
330 } else {
331 context.setProcessedContentOutput(processedDir);
332 }
333 }
334
335 return context;
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351 protected Map<String, MavenReport> locateReports(
352 List<MavenReportExecution> reports, Map<String, DocumentRenderer> documents, Locale locale) {
353 Map<String, MavenReport> reportsByOutputName = new LinkedHashMap<>();
354 for (MavenReportExecution mavenReportExecution : reports) {
355 MavenReport report = mavenReportExecution.getMavenReport();
356
357 String outputName = report.getOutputName();
358 String filename = outputName + ".html";
359
360
361 reportsByOutputName.put(outputName, report);
362
363 if (documents.containsKey(filename)) {
364 String reportMojoInfo = mavenReportExecution.getGoal() == null
365 ? ""
366 : (" (" + mavenReportExecution.getPlugin().getArtifactId() + ':'
367 + mavenReportExecution.getPlugin().getVersion() + ':' + mavenReportExecution.getGoal()
368 + ')');
369
370 getLog().info("Skipped \"" + report.getName(locale) + "\" report" + reportMojoInfo + ", file \""
371 + filename + "\" already exists.");
372 } else {
373 File localizedSiteDirectory;
374 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
375 localizedSiteDirectory = new File(siteDirectory, locale.toString());
376 } else {
377 localizedSiteDirectory = siteDirectory;
378 }
379 String generator = mavenReportExecution.getGoal() == null
380 ? null
381 : mavenReportExecution.getPlugin().getId() + ':' + mavenReportExecution.getGoal();
382 DocumentRenderingContext docRenderingContext =
383 new DocumentRenderingContext(localizedSiteDirectory, outputName, generator);
384 DocumentRenderer docRenderer =
385 new ReportDocumentRenderer(mavenReportExecution, docRenderingContext, getLog());
386 documents.put(filename, docRenderer);
387 }
388 }
389 return reportsByOutputName;
390 }
391
392
393
394
395
396
397
398
399 protected Map<String, List<MavenReport>> categoriseReports(Collection<MavenReport> reports) {
400 Map<String, List<MavenReport>> categories = new LinkedHashMap<>();
401 for (MavenReport report : reports) {
402 List<MavenReport> categoryReports = categories.get(report.getCategoryName());
403 if (categoryReports == null) {
404 categoryReports = new ArrayList<>();
405 categories.put(report.getCategoryName(), categoryReports);
406 }
407 categoryReports.add(report);
408 }
409 return categories;
410 }
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 protected Map<String, DocumentRenderer> locateDocuments(
428 SiteRenderingContext context, List<MavenReportExecution> reports, Locale locale)
429 throws IOException, RendererException {
430 Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles(context);
431
432 Map<String, MavenReport> reportsByOutputName = locateReports(reports, documents, locale);
433
434
435 Map<String, List<MavenReport>> categories = categoriseReports(reportsByOutputName.values());
436
437 siteTool.populateReportsMenu(context.getSiteModel(), locale, categories);
438 populateReportItems(context.getSiteModel(), locale, reportsByOutputName);
439
440 File localizedSiteDirectory;
441 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
442 localizedSiteDirectory = new File(siteDirectory, locale.toString());
443 } else {
444 localizedSiteDirectory = siteDirectory;
445 }
446
447 if (categories.containsKey(MavenReport.CATEGORY_PROJECT_INFORMATION) && generateProjectInfo) {
448
449 List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_INFORMATION);
450 MojoExecution subMojoExecution =
451 new MojoExecution(mojoExecution.getPlugin(), "project-info", mojoExecution.getExecutionId());
452 DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
453 localizedSiteDirectory,
454 subMojoExecution.getGoal(),
455 subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
456 String title = i18n.getString("site-plugin", locale, "report.information.title");
457 String desc1 = i18n.getString("site-plugin", locale, "report.information.description1");
458 String desc2 = i18n.getString("site-plugin", locale, "report.information.description2");
459 DocumentRenderer docRenderer = new CategorySummaryDocumentRenderer(
460 subMojoExecution, docRenderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
461
462 String filename = docRenderer.getOutputName();
463 if (!documents.containsKey(filename)) {
464 documents.put(filename, docRenderer);
465 } else {
466 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
467 }
468 }
469
470 if (categories.containsKey(MavenReport.CATEGORY_PROJECT_REPORTS)) {
471
472 List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_REPORTS);
473 MojoExecution subMojoExecution =
474 new MojoExecution(mojoExecution.getPlugin(), "project-reports", mojoExecution.getExecutionId());
475 DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
476 localizedSiteDirectory,
477 subMojoExecution.getGoal(),
478 subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
479 String title = i18n.getString("site-plugin", locale, "report.project.title");
480 String desc1 = i18n.getString("site-plugin", locale, "report.project.description1");
481 String desc2 = i18n.getString("site-plugin", locale, "report.project.description2");
482 DocumentRenderer docRenderer = new CategorySummaryDocumentRenderer(
483 subMojoExecution, docRenderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
484
485 String filename = docRenderer.getOutputName();
486 if (!documents.containsKey(filename)) {
487 documents.put(filename, docRenderer);
488 } else {
489 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
490 }
491 }
492
493 if (generateSitemap) {
494 MojoExecution subMojoExecution =
495 new MojoExecution(mojoExecution.getPlugin(), "sitemap", mojoExecution.getExecutionId());
496 DocumentRenderingContext docRenderingContext = new DocumentRenderingContext(
497 localizedSiteDirectory,
498 subMojoExecution.getGoal(),
499 subMojoExecution.getPlugin().getId() + ':' + subMojoExecution.getGoal());
500 String title = i18n.getString("site-plugin", locale, "site.sitemap.title");
501 DocumentRenderer docRenderer = new SitemapDocumentRenderer(
502 subMojoExecution, docRenderingContext, title, context.getSiteModel(), i18n, getLog());
503
504 String filename = docRenderer.getOutputName();
505 if (!documents.containsKey(filename)) {
506 documents.put(filename, docRenderer);
507 } else {
508 getLog().info("Skipped \"" + title + "\" report; file \"" + filename + "\" already exists.");
509 }
510 }
511
512 return documents;
513 }
514
515 protected void populateReportItems(
516 SiteModel siteModel, Locale locale, Map<String, MavenReport> reportsByOutputName) {
517 for (Menu menu : siteModel.getMenus()) {
518 populateItemRefs(menu.getItems(), locale, reportsByOutputName);
519 }
520 }
521
522 private void populateItemRefs(List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName) {
523 for (Iterator<MenuItem> i = items.iterator(); i.hasNext(); ) {
524 MenuItem item = i.next();
525
526 if (item.getRef() != null) {
527 MavenReport report = reportsByOutputName.get(item.getRef());
528
529 if (report != null) {
530 if (item.getName() == null) {
531 item.setName(report.getName(locale));
532 }
533
534 if (item.getHref() == null || item.getHref().length() == 0) {
535 item.setHref(report.getOutputName() + ".html");
536 }
537 } else {
538 getLog().warn("Unrecognised reference: '" + item.getRef() + "'");
539 i.remove();
540 }
541 }
542
543 populateItemRefs(item.getItems(), locale, reportsByOutputName);
544 }
545 }
546 }