1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.plugin.buildmetadata;
17
18 import java.io.File;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.Writer;
22 import java.util.HashMap;
23 import java.util.Locale;
24 import java.util.Map;
25 import java.util.ResourceBundle;
26
27 import org.apache.commons.lang.LocaleUtils;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.resolver.ArtifactResolver;
31 import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext;
32 import org.apache.maven.doxia.site.decoration.Body;
33 import org.apache.maven.doxia.site.decoration.DecorationModel;
34 import org.apache.maven.doxia.siterenderer.Renderer;
35 import org.apache.maven.doxia.siterenderer.RendererException;
36 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
37 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
38 import org.apache.maven.plugin.MojoExecutionException;
39 import org.apache.maven.plugin.logging.Log;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.reporting.AbstractMavenReport;
42 import org.apache.maven.reporting.MavenReportException;
43 import org.codehaus.plexus.util.StringUtils;
44
45 import de.smartics.maven.util.LoggingUtils;
46 import de.smartics.maven.util.report.ReportUtils;
47
48
49
50
51
52
53
54 public abstract class AbstractReportMojo extends AbstractMavenReport
55 {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 protected MavenProject project;
73
74
75
76
77
78
79
80
81
82 protected Renderer siteRenderer;
83
84
85
86
87
88
89
90
91
92 protected ArtifactRepository localRepository;
93
94
95
96
97
98
99
100
101
102 protected ArtifactResolver resolver;
103
104
105
106
107
108
109
110
111
112 protected ArtifactFactory factory;
113
114
115
116
117
118
119
120
121
122
123
124 protected File outputDirectory;
125
126
127
128
129
130
131
132
133
134
135
136
137 protected String logLevel;
138
139
140
141
142
143
144
145
146
147 protected String locale;
148
149
150
151
152
153
154
155
156 protected boolean skip;
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 @Override
178 protected final MavenProject getProject()
179 {
180 return project;
181 }
182
183
184
185
186
187
188 @Override
189 protected final Renderer getSiteRenderer()
190 {
191 return siteRenderer;
192 }
193
194
195
196
197
198
199
200
201
202
203 @Override
204 protected final String getOutputDirectory()
205 {
206 return outputDirectory.getAbsolutePath();
207 }
208
209
210
211
212
213
214
215
216
217 public void execute() throws MojoExecutionException
218 {
219 final Log log = getLog();
220 if (!canGenerateReport())
221 {
222 if (log.isInfoEnabled())
223 {
224 log.info("Report '" + getName(Locale.getDefault())
225 + "' skipped due to offline mode.");
226 }
227 return;
228 }
229
230 LoggingUtils.configureLogger(log, logLevel);
231
232 provideSink();
233 }
234
235
236
237
238
239
240
241 @Override
242 public boolean canGenerateReport()
243 {
244 return super.canGenerateReport() && !skip;
245 }
246
247
248
249
250
251
252
253
254
255
256 @Override
257 protected void executeReport(final Locale locale) throws MavenReportException
258 {
259 final Log log = getLog();
260 LoggingUtils.configureLogger(log, logLevel);
261 }
262
263
264
265
266
267
268
269
270
271 protected final void provideSink() throws MojoExecutionException
272 {
273 final Locale reportLocale = determineLocale();
274
275 try
276 {
277 final DecorationModel model = new DecorationModel();
278 model.setBody(new Body());
279 final Map<String, String> attributes = new HashMap<String, String>();
280 attributes.put("outputEncoding", "UTF-8");
281 final SiteRenderingContext siteContext =
282 siteRenderer.createContextForSkin(ReportUtils.getSkinArtifactFile(
283 project, localRepository, resolver, factory), attributes, model,
284 getName(reportLocale), reportLocale);
285
286 final RenderingContext context =
287 new RenderingContext(outputDirectory, getOutputName() + ".html");
288
289 final SiteRendererSink sink = new SiteRendererSink(context);
290 generate(sink, reportLocale);
291
292 provideDir();
293
294
295
296 final Writer writer =
297 new FileWriter(new File(outputDirectory, getOutputName() + ".html"));
298 siteRenderer.generateDocument(writer, sink, siteContext);
299
300 siteRenderer.copyResources(siteContext, new File(project.getBasedir(),
301 "src/site/resources"), outputDirectory);
302 }
303 catch (final RendererException e)
304 {
305 throw new MojoExecutionException(createErrorMessage(reportLocale), e);
306 }
307 catch (final IOException e)
308 {
309 throw new MojoExecutionException(createErrorMessage(reportLocale), e);
310 }
311 catch (final MavenReportException e)
312 {
313 throw new MojoExecutionException(createErrorMessage(reportLocale), e);
314 }
315 }
316
317 private void provideDir() throws IOException
318 {
319 if (!outputDirectory.exists())
320 {
321 if (!outputDirectory.mkdirs())
322 {
323 throw new IOException("Cannot generate directories '"
324 + outputDirectory.getPath() + "'");
325 }
326 }
327 }
328
329
330
331
332
333
334
335 private String createErrorMessage(final Locale reportLocale)
336 {
337 return "An error has occurred in " + getName(reportLocale)
338 + " report generation.";
339 }
340
341
342
343
344
345
346
347 private Locale determineLocale()
348 {
349 return StringUtils.isNotBlank(this.locale) ? LocaleUtils
350 .toLocale(this.locale) : Locale.getDefault();
351 }
352
353
354
355
356
357
358
359 protected final ResourceBundle getBundle(final Locale locale)
360 {
361 return ResourceBundle.getBundle(
362 "de.smartics.maven.buildmetadata.BuildReport", locale);
363 }
364
365
366
367 }