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