1 package org.apache.maven.reporting;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.doxia.sink.Sink;
23 import org.apache.maven.doxia.sink.SinkFactory;
24 import org.apache.maven.doxia.site.decoration.DecorationModel;
25 import org.apache.maven.doxia.siterenderer.Renderer;
26 import org.apache.maven.doxia.siterenderer.RendererException;
27 import org.apache.maven.doxia.siterenderer.RenderingContext;
28 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
29 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.shared.utils.WriterFactory;
36 import org.codehaus.plexus.util.ReaderFactory;
37
38 import java.io.File;
39 import java.io.FileOutputStream;
40 import java.io.IOException;
41 import java.io.OutputStreamWriter;
42 import java.io.Writer;
43 import java.util.HashMap;
44 import java.util.Locale;
45 import java.util.Map;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public abstract class AbstractMavenReport
64 extends AbstractMojo
65 implements MavenMultiPageReport
66 {
67
68
69
70
71
72 @Parameter( defaultValue = "${project.reporting.outputDirectory}", readonly = true, required = true )
73 protected File outputDirectory;
74
75
76
77
78 @Parameter( defaultValue = "${project}", readonly = true, required = true )
79 protected MavenProject project;
80
81
82
83
84 @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}", readonly = true )
85 private String inputEncoding;
86
87
88
89
90 @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true )
91 private String outputEncoding;
92
93
94
95
96 @Component
97 protected Renderer siteRenderer;
98
99
100 private Sink sink;
101
102
103 private SinkFactory sinkFactory;
104
105
106 private File reportOutputDirectory;
107
108
109
110
111
112
113
114 @Override
115 public void execute()
116 throws MojoExecutionException
117 {
118 if ( !canGenerateReport() )
119 {
120 return;
121 }
122
123 File outputDirectory = new File( getOutputDirectory() );
124
125 String filename = getOutputName() + ".html";
126
127 Locale locale = Locale.getDefault();
128
129 SiteRenderingContext siteContext = new SiteRenderingContext();
130 siteContext.setDecoration( new DecorationModel() );
131 siteContext.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
132 siteContext.setLocale( locale );
133 siteContext.setTemplateProperties( getTemplateProperties() );
134
135
136 RenderingContext context = new RenderingContext( outputDirectory, filename, null );
137
138 SiteRendererSink sink = new SiteRendererSink( context );
139
140 try
141 {
142
143 generate( sink, null, locale );
144
145 if ( !isExternalReport() )
146 {
147 outputDirectory.mkdirs();
148
149 try ( Writer writer =
150 new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, filename ) ),
151 getOutputEncoding() ) )
152 {
153 getSiteRenderer().mergeDocumentIntoSite( writer, sink, siteContext );
154 }
155 }
156 }
157 catch ( RendererException | IOException | MavenReportException e )
158 {
159 throw new MojoExecutionException(
160 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
161 }
162 }
163
164
165
166
167
168
169 private Map<String, Object> getTemplateProperties()
170 {
171 Map<String, Object> templateProperties = new HashMap<>();
172 templateProperties.put( "project", getProject() );
173 templateProperties.put( "inputEncoding", getInputEncoding() );
174 templateProperties.put( "outputEncoding", getOutputEncoding() );
175
176 for ( Map.Entry<Object, Object> entry : getProject().getProperties().entrySet() )
177 {
178 templateProperties.put( (String) entry.getKey(), entry.getValue() );
179 }
180 return templateProperties;
181 }
182
183
184
185
186
187
188
189
190
191 @Deprecated
192 public void generate( org.codehaus.doxia.sink.Sink sink, Locale locale )
193 throws MavenReportException
194 {
195 generate( sink, null, locale );
196 }
197
198
199
200
201
202
203
204
205
206 @Deprecated
207 @Override
208 public void generate( Sink sink, Locale locale )
209 throws MavenReportException
210 {
211 generate( sink, null, locale );
212 }
213
214
215
216
217
218
219
220
221
222 @Override
223 public void generate( Sink sink, SinkFactory sinkFactory, Locale locale )
224 throws MavenReportException
225 {
226 if ( !canGenerateReport() )
227 {
228 getLog().info( "This report cannot be generated as part of the current build. "
229 + "The report name should be referenced in this line of output." );
230 return;
231 }
232
233 this.sink = sink;
234
235 this.sinkFactory = sinkFactory;
236
237 executeReport( locale );
238
239 closeReport();
240 }
241
242
243
244
245 @Override
246 public String getCategoryName()
247 {
248 return CATEGORY_PROJECT_REPORTS;
249 }
250
251 @Override
252 public File getReportOutputDirectory()
253 {
254 if ( reportOutputDirectory == null )
255 {
256 reportOutputDirectory = new File( getOutputDirectory() );
257 }
258
259 return reportOutputDirectory;
260 }
261
262 @Override
263 public void setReportOutputDirectory( File reportOutputDirectory )
264 {
265 this.reportOutputDirectory = reportOutputDirectory;
266 this.outputDirectory = reportOutputDirectory;
267 }
268
269 protected String getOutputDirectory()
270 {
271 return outputDirectory.getAbsolutePath();
272 }
273
274 protected MavenProject getProject()
275 {
276 return project;
277 }
278
279 protected Renderer getSiteRenderer()
280 {
281 return siteRenderer;
282 }
283
284
285
286
287
288
289 protected String getInputEncoding()
290 {
291 return ( inputEncoding == null ) ? ReaderFactory.FILE_ENCODING : inputEncoding;
292 }
293
294
295
296
297
298
299 protected String getOutputEncoding()
300 {
301 return ( outputEncoding == null ) ? WriterFactory.UTF_8 : outputEncoding;
302 }
303
304
305
306
307 protected void closeReport()
308 {
309 getSink().close();
310 }
311
312
313
314
315 public Sink getSink()
316 {
317 return sink;
318 }
319
320
321
322
323 public SinkFactory getSinkFactory()
324 {
325 return sinkFactory;
326 }
327
328
329
330
331
332 @Override
333 public boolean isExternalReport()
334 {
335 return false;
336 }
337
338 @Override
339 public boolean canGenerateReport()
340 {
341 return true;
342 }
343
344
345
346
347
348
349
350 protected abstract void executeReport( Locale locale )
351 throws MavenReportException;
352 }