1 | |
package org.apache.maven.plugins.site; |
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.artifact.factory.ArtifactFactory; |
23 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
24 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
25 | |
import org.apache.maven.doxia.sink.render.RenderingContext; |
26 | |
import org.apache.maven.doxia.site.decoration.DecorationModel; |
27 | |
import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler; |
28 | |
import org.apache.maven.doxia.siterenderer.DocumentRenderer; |
29 | |
import org.apache.maven.doxia.siterenderer.Renderer; |
30 | |
import org.apache.maven.doxia.siterenderer.RendererException; |
31 | |
import org.apache.maven.doxia.siterenderer.SiteRenderingContext; |
32 | |
import org.apache.maven.doxia.tools.SiteToolException; |
33 | |
import org.apache.maven.plugin.MojoExecutionException; |
34 | |
import org.apache.maven.plugin.MojoFailureException; |
35 | |
import org.apache.maven.reporting.MavenReport; |
36 | |
|
37 | |
import java.io.File; |
38 | |
import java.io.IOException; |
39 | |
import java.util.ArrayList; |
40 | |
import java.util.Collection; |
41 | |
import java.util.HashMap; |
42 | |
import java.util.Iterator; |
43 | |
import java.util.List; |
44 | |
import java.util.Locale; |
45 | |
import java.util.Map; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 1 | public abstract class AbstractSiteRenderingMojo |
54 | |
extends AbstractSiteMojo |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
protected Map<String, String> moduleExcludes; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
protected DecorationModelInheritanceAssembler assembler; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
protected ArtifactResolver artifactResolver; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
protected List<ArtifactRepository> repositories; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
protected ArtifactFactory artifactFactory; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
private File templateDirectory; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
private String template; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private File templateFile; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
protected Map<Object, Object> attributes; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
protected Renderer siteRenderer; |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
protected List<MavenReport> reports; |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private File xdocDirectory; |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
protected File generatedSiteDirectory; |
167 | |
|
168 | |
protected List<MavenReport> filterReports( List<MavenReport> reports ) |
169 | |
{ |
170 | 0 | List<MavenReport> filteredReports = new ArrayList<MavenReport>( reports.size() ); |
171 | 0 | for ( MavenReport report : reports ) |
172 | |
{ |
173 | |
|
174 | |
try |
175 | |
{ |
176 | 0 | if ( report.canGenerateReport() ) |
177 | |
{ |
178 | 0 | filteredReports.add( report ); |
179 | |
} |
180 | |
} |
181 | 0 | catch ( AbstractMethodError e ) |
182 | |
{ |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | 0 | getLog().warn( |
188 | |
"Error loading report " + report.getClass().getName() |
189 | |
+ " - AbstractMethodError: canGenerateReport()" ); |
190 | 0 | filteredReports.add( report ); |
191 | 0 | } |
192 | |
} |
193 | 0 | return filteredReports; |
194 | |
} |
195 | |
|
196 | |
protected SiteRenderingContext createSiteRenderingContext( Locale locale ) |
197 | |
throws MojoExecutionException, IOException, MojoFailureException |
198 | |
{ |
199 | 0 | if ( attributes == null ) |
200 | |
{ |
201 | 0 | attributes = new HashMap<Object, Object>(); |
202 | |
} |
203 | |
|
204 | 0 | if ( attributes.get( "project" ) == null ) |
205 | |
{ |
206 | 0 | attributes.put( "project", project ); |
207 | |
} |
208 | |
|
209 | 0 | if ( attributes.get( "inputEncoding" ) == null ) |
210 | |
{ |
211 | 0 | attributes.put( "inputEncoding", getInputEncoding() ); |
212 | |
} |
213 | |
|
214 | 0 | if ( attributes.get( "outputEncoding" ) == null ) |
215 | |
{ |
216 | 0 | attributes.put( "outputEncoding", getOutputEncoding() ); |
217 | |
} |
218 | |
|
219 | |
|
220 | 0 | attributes.putAll( project.getProperties() ); |
221 | |
|
222 | |
DecorationModel decorationModel; |
223 | |
try |
224 | |
{ |
225 | 0 | decorationModel = siteTool.getDecorationModel( project, reactorProjects, localRepository, repositories, |
226 | |
toRelative( project.getBasedir(), |
227 | |
siteDirectory.getAbsolutePath() ), |
228 | |
locale, getInputEncoding(), getOutputEncoding() ); |
229 | |
} |
230 | 0 | catch ( SiteToolException e ) |
231 | |
{ |
232 | 0 | throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e ); |
233 | 0 | } |
234 | 0 | if ( template != null ) |
235 | |
{ |
236 | 0 | if ( templateFile != null ) |
237 | |
{ |
238 | 0 | getLog().warn( "'template' configuration is ignored when 'templateFile' is set" ); |
239 | |
} |
240 | |
else |
241 | |
{ |
242 | 0 | templateFile = new File( templateDirectory, template ); |
243 | |
} |
244 | |
} |
245 | |
|
246 | |
File skinFile; |
247 | |
try |
248 | |
{ |
249 | 0 | skinFile = siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel ) |
250 | |
.getFile(); |
251 | |
} |
252 | 0 | catch ( SiteToolException e ) |
253 | |
{ |
254 | 0 | throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e ); |
255 | 0 | } |
256 | |
SiteRenderingContext context; |
257 | 0 | if ( templateFile != null ) |
258 | |
{ |
259 | 0 | if ( !templateFile.exists() ) |
260 | |
{ |
261 | 0 | throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" ); |
262 | |
} |
263 | 0 | context = siteRenderer.createContextForTemplate( templateFile, skinFile, attributes, decorationModel, |
264 | |
project.getName(), locale ); |
265 | |
} |
266 | |
else |
267 | |
{ |
268 | 0 | context = siteRenderer.createContextForSkin( skinFile, attributes, decorationModel, project.getName(), |
269 | |
locale ); |
270 | |
} |
271 | |
|
272 | |
|
273 | 0 | if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) ) |
274 | |
{ |
275 | 0 | context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) ); |
276 | 0 | context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" ); |
277 | 0 | context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" ); |
278 | |
} |
279 | |
else |
280 | |
{ |
281 | 0 | context.addSiteDirectory( siteDirectory ); |
282 | 0 | context.addModuleDirectory( xdocDirectory, "xdoc" ); |
283 | 0 | context.addModuleDirectory( xdocDirectory, "fml" ); |
284 | |
} |
285 | |
|
286 | 0 | if ( moduleExcludes != null ) |
287 | |
{ |
288 | 0 | context.setModuleExcludes( moduleExcludes ); |
289 | |
} |
290 | |
|
291 | 0 | return context; |
292 | |
} |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
protected Map<String, MavenReport> locateReports( List<MavenReport> reports, |
308 | |
Map<String, DocumentRenderer> documents, Locale locale ) |
309 | |
{ |
310 | 0 | Map<String, MavenReport> reportsByOutputName = new HashMap<String, MavenReport>(); |
311 | 0 | for ( Iterator<MavenReport> i = reports.iterator(); i.hasNext(); ) |
312 | |
{ |
313 | 0 | MavenReport report = i.next(); |
314 | |
|
315 | 0 | String outputName = report.getOutputName() + ".html"; |
316 | |
|
317 | |
|
318 | 0 | reportsByOutputName.put( report.getOutputName(), report ); |
319 | |
|
320 | 0 | if ( documents.containsKey( outputName ) ) |
321 | |
{ |
322 | 0 | String displayLanguage = locale.getDisplayLanguage( Locale.ENGLISH ); |
323 | |
|
324 | 0 | getLog().info( "Skipped \"" + report.getName( locale ) + "\" report, file \"" + outputName |
325 | |
+ "\" already exists for the " + displayLanguage + " version." ); |
326 | 0 | i.remove(); |
327 | 0 | } |
328 | |
else |
329 | |
{ |
330 | 0 | RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName ); |
331 | 0 | ReportDocumentRenderer renderer = new ReportDocumentRenderer( report, renderingContext, getLog() ); |
332 | 0 | documents.put( outputName, renderer ); |
333 | |
} |
334 | 0 | } |
335 | 0 | return reportsByOutputName; |
336 | |
} |
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
protected Map<String, List<MavenReport>> categoriseReports( Collection<MavenReport> reports ) |
346 | |
{ |
347 | 0 | Map<String, List<MavenReport>> categories = new HashMap<String, List<MavenReport>>(); |
348 | 0 | for ( MavenReport report : reports ) |
349 | |
{ |
350 | 0 | List<MavenReport> categoryReports = categories.get( report.getCategoryName() ); |
351 | 0 | if ( categoryReports == null ) |
352 | |
{ |
353 | 0 | categoryReports = new ArrayList<MavenReport>(); |
354 | 0 | categories.put( report.getCategoryName(), categoryReports ); |
355 | |
} |
356 | 0 | categoryReports.add( report ); |
357 | 0 | } |
358 | 0 | return categories; |
359 | |
} |
360 | |
|
361 | |
protected Map<String, DocumentRenderer> locateDocuments( SiteRenderingContext context, List<MavenReport> reports, |
362 | |
Locale locale ) |
363 | |
throws IOException, RendererException |
364 | |
{ |
365 | 0 | Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles( context ); |
366 | |
|
367 | 0 | Map<String, MavenReport> reportsByOutputName = locateReports( reports, documents, locale ); |
368 | |
|
369 | |
|
370 | 0 | Map<String, List<MavenReport>> categories = categoriseReports( reportsByOutputName.values() ); |
371 | |
|
372 | 0 | siteTool.populateReportsMenu( context.getDecoration(), locale, categories ); |
373 | 0 | populateReportItems( context.getDecoration(), locale, reportsByOutputName ); |
374 | |
|
375 | 0 | if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) ) |
376 | |
{ |
377 | 0 | List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION ); |
378 | |
|
379 | 0 | RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-info.html" ); |
380 | 0 | String title = i18n.getString( "site-plugin", locale, "report.information.title" ); |
381 | 0 | String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" ); |
382 | 0 | String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" ); |
383 | 0 | DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2, |
384 | |
i18n, categoryReports, getLog() ); |
385 | |
|
386 | 0 | if ( !documents.containsKey( renderer.getOutputName() ) ) |
387 | |
{ |
388 | 0 | documents.put( renderer.getOutputName(), renderer ); |
389 | |
} |
390 | |
else |
391 | |
{ |
392 | 0 | getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" ); |
393 | |
} |
394 | |
} |
395 | |
|
396 | 0 | if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) ) |
397 | |
{ |
398 | 0 | List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_REPORTS ); |
399 | 0 | RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-reports.html" ); |
400 | 0 | String title = i18n.getString( "site-plugin", locale, "report.project.title" ); |
401 | 0 | String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" ); |
402 | 0 | String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" ); |
403 | 0 | DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2, |
404 | |
i18n, categoryReports, getLog() ); |
405 | |
|
406 | 0 | if ( !documents.containsKey( renderer.getOutputName() ) ) |
407 | |
{ |
408 | 0 | documents.put( renderer.getOutputName(), renderer ); |
409 | |
} |
410 | |
else |
411 | |
{ |
412 | 0 | getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" ); |
413 | |
} |
414 | |
} |
415 | 0 | return documents; |
416 | |
} |
417 | |
} |