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 @Parameter(defaultValue = "${project.reporting.outputDirectory}", readonly = true, required = true)
82 protected File outputDirectory;
83
84
85
86
87 @Parameter(defaultValue = "${project}", readonly = true, required = true)
88 protected MavenProject project;
89
90
91
92
93 @Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
94 protected MojoExecution mojoExecution;
95
96
97
98
99 @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
100 protected List<MavenProject> reactorProjects;
101
102
103
104
105 @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}", readonly = true)
106 private String inputEncoding;
107
108
109
110
111 @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true)
112 private String outputEncoding;
113
114
115
116
117 @Parameter(defaultValue = "${repositorySystemSession}", readonly = true, required = true)
118 protected RepositorySystemSession repoSession;
119
120
121
122
123 @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
124 protected List<RemoteRepository> remoteProjectRepositories;
125
126
127
128
129 @Parameter(defaultValue = "${basedir}/src/site")
130 protected File siteDirectory;
131
132
133
134
135
136
137
138
139 @Parameter(defaultValue = "default")
140 protected String locale;
141
142
143
144
145 @Component
146 protected SiteTool siteTool;
147
148
149
150
151 @Component
152 protected Renderer siteRenderer;
153
154
155 private Sink sink;
156
157
158 private SinkFactory sinkFactory;
159
160
161 private File reportOutputDirectory;
162
163
164
165
166 @Parameter(property = "output.format")
167 protected String outputFormat;
168
169 @Component
170 private PlexusContainer container;
171
172
173
174
175
176
177
178 @Override
179 public void execute() throws MojoExecutionException {
180 if (!canGenerateReport()) {
181 return;
182 }
183
184 if (outputFormat != null) {
185 reportToMarkup();
186 } else {
187 reportToSite();
188 }
189 }
190
191 private void reportToMarkup() throws MojoExecutionException {
192 getLog().info("Rendering to " + outputFormat + " markup");
193
194 if (!isExternalReport()) {
195 String filename = getOutputName() + '.' + outputFormat;
196 try {
197 sinkFactory = container.lookup(SinkFactory.class, outputFormat);
198 sink = sinkFactory.createSink(outputDirectory, filename);
199 } catch (ComponentLookupException cle) {
200 throw new MojoExecutionException(
201 "Cannot find SinkFactory for Doxia output format: " + outputFormat, cle);
202 } catch (IOException ioe) {
203 throw new MojoExecutionException("Cannot create sink to " + new File(outputDirectory, filename), ioe);
204 }
205 }
206
207 try {
208 Locale locale = getLocale();
209 generate(sink, sinkFactory, locale);
210 } catch (MavenReportException e) {
211 throw new MojoExecutionException(
212 "An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
213 } finally {
214 if (sink != null) {
215 sink.close();
216 }
217 }
218 }
219
220 private void reportToSite() throws MojoExecutionException {
221 File outputDirectory = new File(getOutputDirectory());
222
223 String filename = getOutputName() + ".html";
224
225 Locale locale = getLocale();
226
227 try {
228 SiteRenderingContext siteContext = createSiteRenderingContext(locale);
229
230
231 getSiteRenderer().copyResources(siteContext, outputDirectory);
232
233 String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
234 DocumentRenderingContext docRenderingContext =
235 new DocumentRenderingContext(outputDirectory, getOutputName(), reportMojoInfo);
236
237 SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
238
239
240 generate(sink, null, locale);
241
242 if (!isExternalReport())
243 {
244 outputDirectory.mkdirs();
245
246 try (Writer writer = new OutputStreamWriter(
247 new FileOutputStream(new File(outputDirectory, filename)), getOutputEncoding())) {
248
249 getSiteRenderer().mergeDocumentIntoSite(writer, sink, siteContext);
250 }
251 }
252
253
254 getSiteRenderer().copyResources(siteContext, outputDirectory);
255 } catch (RendererException | IOException | MavenReportException | SiteToolException e) {
256 throw new MojoExecutionException(
257 "An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
258 }
259 }
260
261 private SiteRenderingContext createSiteRenderingContext(Locale locale)
262 throws MavenReportException, IOException, SiteToolException {
263 SiteModel siteModel = siteTool.getSiteModel(
264 siteDirectory, locale, project, reactorProjects, repoSession, remoteProjectRepositories);
265
266 Map<String, Object> templateProperties = new HashMap<>();
267
268 templateProperties.put("standalone", Boolean.TRUE);
269 templateProperties.put("project", getProject());
270 templateProperties.put("inputEncoding", getInputEncoding());
271 templateProperties.put("outputEncoding", getOutputEncoding());
272
273 for (Map.Entry<Object, Object> entry : getProject().getProperties().entrySet()) {
274 templateProperties.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(
287 skinArtifact, templateProperties, siteModel, project.getName(), locale);
288 } catch (SiteToolException e) {
289 throw new MavenReportException("Failed to retrieve skin artifact", e);
290 } catch (RendererException e) {
291 throw new MavenReportException("Failed to create context for skin", e);
292 }
293
294
295 String outputTimestamp = getProject().getProperties().getProperty("project.build.outputTimestamp");
296 MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).ifPresent(v -> {
297 context.setPublishDate(Date.from(v));
298 });
299
300
301 context.setRootDirectory(project.getBasedir());
302
303 return context;
304 }
305
306
307
308
309
310
311
312
313
314 @Deprecated
315 @Override
316 public void generate(Sink sink, Locale locale) throws MavenReportException {
317 generate(sink, null, locale);
318 }
319
320
321
322
323
324
325
326
327
328 @Override
329 public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException {
330 if (!canGenerateReport()) {
331 getLog().info("This report cannot be generated as part of the current build. "
332 + "The report name should be referenced in this line of output.");
333 return;
334 }
335
336 this.sink = sink;
337
338 this.sinkFactory = sinkFactory;
339
340 executeReport(locale);
341
342 closeReport();
343 }
344
345
346
347
348 @Override
349 public String getCategoryName() {
350 return CATEGORY_PROJECT_REPORTS;
351 }
352
353 @Override
354 public File getReportOutputDirectory() {
355 if (reportOutputDirectory == null) {
356 reportOutputDirectory = new File(getOutputDirectory());
357 }
358
359 return reportOutputDirectory;
360 }
361
362 @Override
363 public void setReportOutputDirectory(File reportOutputDirectory) {
364 this.reportOutputDirectory = reportOutputDirectory;
365 this.outputDirectory = reportOutputDirectory;
366 }
367
368 protected String getOutputDirectory() {
369 return outputDirectory.getAbsolutePath();
370 }
371
372 protected MavenProject getProject() {
373 return project;
374 }
375
376 protected Renderer getSiteRenderer() {
377 return siteRenderer;
378 }
379
380
381
382
383
384
385 protected String getInputEncoding() {
386 return (inputEncoding == null) ? ReaderFactory.FILE_ENCODING : inputEncoding;
387 }
388
389
390
391
392
393
394 protected String getOutputEncoding() {
395 return (outputEncoding == null) ? WriterFactory.UTF_8 : outputEncoding;
396 }
397
398
399
400
401
402
403 protected Locale getLocale() {
404 return siteTool.getSiteLocales(locale).get(0);
405 }
406
407
408
409
410 protected void closeReport() {
411 if (getSink() != null) {
412 getSink().close();
413 }
414 }
415
416
417
418
419 public Sink getSink() {
420 return sink;
421 }
422
423
424
425
426 public SinkFactory getSinkFactory() {
427 return sinkFactory;
428 }
429
430
431
432
433
434 @Override
435 public boolean isExternalReport() {
436 return false;
437 }
438
439 @Override
440 public boolean canGenerateReport() {
441 return true;
442 }
443
444
445
446
447
448
449
450 protected abstract void executeReport(Locale locale) throws MavenReportException;
451 }