1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.reporting;
20
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Locale;
30 import java.util.Map;
31
32 import org.apache.maven.archiver.MavenArchiver;
33 import org.apache.maven.artifact.Artifact;
34 import org.apache.maven.doxia.sink.Sink;
35 import org.apache.maven.doxia.sink.SinkFactory;
36 import org.apache.maven.doxia.site.SiteModel;
37 import org.apache.maven.doxia.siterenderer.DocumentRenderingContext;
38 import org.apache.maven.doxia.siterenderer.Renderer;
39 import org.apache.maven.doxia.siterenderer.RendererException;
40 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
41 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
42 import org.apache.maven.doxia.tools.SiteTool;
43 import org.apache.maven.doxia.tools.SiteToolException;
44 import org.apache.maven.plugin.AbstractMojo;
45 import org.apache.maven.plugin.MojoExecution;
46 import org.apache.maven.plugin.MojoExecutionException;
47 import org.apache.maven.plugins.annotations.Component;
48 import org.apache.maven.plugins.annotations.Parameter;
49 import org.apache.maven.project.MavenProject;
50 import org.apache.maven.shared.utils.WriterFactory;
51 import org.codehaus.plexus.PlexusContainer;
52 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
53 import org.codehaus.plexus.util.ReaderFactory;
54 import org.eclipse.aether.RepositorySystemSession;
55 import org.eclipse.aether.repository.RemoteRepository;
56
57 import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public abstract class AbstractMavenReport extends AbstractMojo implements MavenMultiPageReport {
76
77
78
79
80
81
82
83
84
85
86
87 @Parameter(defaultValue = "${project.build.directory}/reports", required = true)
88 protected File outputDirectory;
89
90
91
92
93 @Parameter(defaultValue = "${project}", readonly = true, required = true)
94 protected MavenProject project;
95
96
97
98
99 @Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
100 protected MojoExecution mojoExecution;
101
102
103
104
105 @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
106 protected List<MavenProject> reactorProjects;
107
108
109
110
111 @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}", readonly = true)
112 private String inputEncoding;
113
114
115
116
117 @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true)
118 private String outputEncoding;
119
120
121
122
123 @Parameter(defaultValue = "${repositorySystemSession}", readonly = true, required = true)
124 protected RepositorySystemSession repoSession;
125
126
127
128
129 @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
130 protected List<RemoteRepository> remoteProjectRepositories;
131
132
133
134
135 @Parameter(defaultValue = "${basedir}/src/site")
136 protected File siteDirectory;
137
138
139
140
141
142
143
144 @Parameter(defaultValue = "default")
145 protected String locale;
146
147
148
149
150
151
152 @Parameter(defaultValue = "${project.build.outputTimestamp}")
153 protected String outputTimestamp;
154
155
156
157
158 @Component
159 protected SiteTool siteTool;
160
161
162
163
164 @Component
165 protected Renderer siteRenderer;
166
167
168 private Sink sink;
169
170
171 private SinkFactory sinkFactory;
172
173
174 private File reportOutputDirectory;
175
176
177
178
179 @Parameter(property = "output.format")
180 protected String outputFormat;
181
182 @Component
183 private PlexusContainer container;
184
185
186
187
188
189
190
191
192
193 @Override
194 public final void execute() throws MojoExecutionException {
195 try {
196 if (!canGenerateReport()) {
197 String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
198 getLog().info("Skipping " + reportMojoInfo + " report goal");
199 return;
200 }
201 } catch (MavenReportException e) {
202 throw new MojoExecutionException("Failed to determine whether report can be generated", e);
203 }
204
205 if (outputFormat != null) {
206 reportToMarkup();
207 } else {
208 reportToSite();
209 }
210 }
211
212 private void reportToMarkup() throws MojoExecutionException {
213 getLog().info("Rendering to " + outputFormat + " markup");
214
215 if (!isExternalReport()) {
216 File outputDirectory = new File(getOutputDirectory());
217 String filename = getOutputName() + '.' + outputFormat;
218 try {
219 sinkFactory = container.lookup(SinkFactory.class, outputFormat);
220 sink = sinkFactory.createSink(outputDirectory, filename);
221 } catch (ComponentLookupException cle) {
222 throw new MojoExecutionException(
223 "Cannot find SinkFactory for Doxia output format: " + outputFormat, cle);
224 } catch (IOException ioe) {
225 throw new MojoExecutionException("Cannot create sink to " + new File(outputDirectory, filename), ioe);
226 }
227 }
228
229 try {
230 Locale locale = getLocale();
231 generate(sink, sinkFactory, locale);
232 } catch (MavenReportException e) {
233 throw new MojoExecutionException(
234 "An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
235 } finally {
236 if (sink != null) {
237 sink.close();
238 }
239 }
240 }
241
242 private void reportToSite() throws MojoExecutionException {
243 File outputDirectory = new File(getOutputDirectory());
244
245 String filename = getOutputName() + ".html";
246
247 Locale locale = getLocale();
248
249 try {
250 SiteRenderingContext siteContext = createSiteRenderingContext(locale);
251
252
253 getSiteRenderer().copyResources(siteContext, outputDirectory);
254
255 String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
256 DocumentRenderingContext docRenderingContext =
257 new DocumentRenderingContext(outputDirectory, getOutputName(), reportMojoInfo);
258
259 SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
260
261
262 generate(sink, null, locale);
263
264 if (!isExternalReport())
265 {
266 outputDirectory.mkdirs();
267
268 try (Writer writer = new OutputStreamWriter(
269 new FileOutputStream(new File(outputDirectory, filename)), getOutputEncoding())) {
270
271 getSiteRenderer().mergeDocumentIntoSite(writer, sink, siteContext);
272 }
273 }
274
275
276 getSiteRenderer().copyResources(siteContext, outputDirectory);
277 } catch (RendererException | IOException | MavenReportException | SiteToolException e) {
278 throw new MojoExecutionException(
279 "An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
280 }
281 }
282
283 private SiteRenderingContext createSiteRenderingContext(Locale locale)
284 throws MavenReportException, IOException, SiteToolException {
285 SiteModel siteModel = siteTool.getSiteModel(
286 siteDirectory, locale, project, reactorProjects, repoSession, remoteProjectRepositories);
287
288 Map<String, Object> templateProperties = new HashMap<>();
289
290 templateProperties.put("standalone", Boolean.TRUE);
291 templateProperties.put("project", getProject());
292 templateProperties.put("inputEncoding", getInputEncoding());
293 templateProperties.put("outputEncoding", getOutputEncoding());
294
295 for (Map.Entry<Object, Object> entry : getProject().getProperties().entrySet()) {
296 templateProperties.put((String) entry.getKey(), entry.getValue());
297 }
298
299 SiteRenderingContext context;
300 try {
301 Artifact skinArtifact =
302 siteTool.getSkinArtifactFromRepository(repoSession, remoteProjectRepositories, siteModel.getSkin());
303
304 getLog().info(buffer().a("Rendering content with ")
305 .strong(skinArtifact.getId() + " skin")
306 .toString());
307
308 context = siteRenderer.createContextForSkin(
309 skinArtifact, templateProperties, siteModel, project.getName(), locale);
310 } catch (SiteToolException e) {
311 throw new MavenReportException("Failed to retrieve skin artifact", e);
312 } catch (RendererException e) {
313 throw new MavenReportException("Failed to create context for skin", e);
314 }
315
316
317 MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).ifPresent(v -> {
318 context.setPublishDate(Date.from(v));
319 });
320
321
322 context.setRootDirectory(project.getBasedir());
323
324 return context;
325 }
326
327
328
329
330
331
332
333
334
335 @Deprecated
336 @Override
337 public void generate(Sink sink, Locale locale) throws MavenReportException {
338 generate(sink, null, locale);
339 }
340
341
342
343
344
345
346
347
348
349 @Override
350 public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException {
351 this.sink = sink;
352 this.sinkFactory = sinkFactory;
353
354 executeReport(locale);
355 closeReport();
356 }
357
358
359
360
361 @Override
362 public String getCategoryName() {
363 return CATEGORY_PROJECT_REPORTS;
364 }
365
366 @Override
367 public File getReportOutputDirectory() {
368 if (reportOutputDirectory == null) {
369 reportOutputDirectory = new File(getOutputDirectory());
370 }
371
372 return reportOutputDirectory;
373 }
374
375 @Override
376 public void setReportOutputDirectory(File reportOutputDirectory) {
377 this.reportOutputDirectory = reportOutputDirectory;
378 this.outputDirectory = reportOutputDirectory;
379 }
380
381 protected String getOutputDirectory() {
382 return outputDirectory.getAbsolutePath();
383 }
384
385 protected MavenProject getProject() {
386 return project;
387 }
388
389 protected Renderer getSiteRenderer() {
390 return siteRenderer;
391 }
392
393
394
395
396
397
398 protected String getInputEncoding() {
399 return (inputEncoding == null) ? ReaderFactory.FILE_ENCODING : inputEncoding;
400 }
401
402
403
404
405
406
407 protected String getOutputEncoding() {
408 return (outputEncoding == null) ? WriterFactory.UTF_8 : outputEncoding;
409 }
410
411
412
413
414
415
416 protected Locale getLocale() {
417 return siteTool.getSiteLocales(locale).get(0);
418 }
419
420
421
422
423 protected void closeReport() {
424 if (getSink() != null) {
425 getSink().close();
426 }
427 }
428
429
430
431
432 public Sink getSink() {
433 return sink;
434 }
435
436
437
438
439 public SinkFactory getSinkFactory() {
440 return sinkFactory;
441 }
442
443
444
445
446
447 @Override
448 public boolean isExternalReport() {
449 return false;
450 }
451
452 @Override
453 public boolean canGenerateReport() throws MavenReportException {
454 return true;
455 }
456
457
458
459
460
461
462
463 protected abstract void executeReport(Locale locale) throws MavenReportException;
464 }