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 java.io.File; |
23 | |
import java.io.IOException; |
24 | |
|
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Collection; |
27 | |
import java.util.HashMap; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.LinkedHashMap; |
30 | |
import java.util.List; |
31 | |
import java.util.Locale; |
32 | |
import java.util.Map; |
33 | |
|
34 | |
import org.apache.maven.artifact.Artifact; |
35 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
36 | |
import org.apache.maven.doxia.sink.render.RenderingContext; |
37 | |
import org.apache.maven.doxia.site.decoration.DecorationModel; |
38 | |
import org.apache.maven.doxia.site.decoration.Menu; |
39 | |
import org.apache.maven.doxia.site.decoration.MenuItem; |
40 | |
import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler; |
41 | |
import org.apache.maven.doxia.siterenderer.DocumentRenderer; |
42 | |
import org.apache.maven.doxia.siterenderer.Renderer; |
43 | |
import org.apache.maven.doxia.siterenderer.RendererException; |
44 | |
import org.apache.maven.doxia.siterenderer.SiteRenderingContext; |
45 | |
import org.apache.maven.doxia.tools.SiteToolException; |
46 | |
import org.apache.maven.execution.MavenSession; |
47 | |
import org.apache.maven.plugin.MojoExecutionException; |
48 | |
import org.apache.maven.plugin.MojoFailureException; |
49 | |
import org.apache.maven.plugins.annotations.Component; |
50 | |
import org.apache.maven.plugins.annotations.Parameter; |
51 | |
import org.apache.maven.reporting.MavenReport; |
52 | |
import org.apache.maven.reporting.exec.MavenReportExecution; |
53 | |
import org.apache.maven.reporting.exec.MavenReportExecutor; |
54 | |
import org.apache.maven.reporting.exec.MavenReportExecutorRequest; |
55 | |
import org.apache.maven.reporting.exec.ReportPlugin; |
56 | |
import org.codehaus.plexus.PlexusConstants; |
57 | |
import org.codehaus.plexus.PlexusContainer; |
58 | |
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
59 | |
import org.codehaus.plexus.context.Context; |
60 | |
import org.codehaus.plexus.context.ContextException; |
61 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | 3 | public abstract class AbstractSiteRenderingMojo |
70 | |
extends AbstractSiteMojo implements Contextualizable |
71 | |
{ |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
@Parameter |
89 | |
private Map<String, String> moduleExcludes; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
@Component |
95 | |
private DecorationModelInheritanceAssembler assembler; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
@Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true ) |
103 | |
private List<ArtifactRepository> repositories; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
@Parameter( property = "templateDirectory", defaultValue = "src/site" ) |
111 | |
private File templateDirectory; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
@Parameter( property = "template" ) |
119 | |
private String template; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
@Parameter( property = "templateFile" ) |
128 | |
private File templateFile; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
@Parameter |
135 | |
private Map<String, Object> attributes; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
@Component |
141 | |
protected Renderer siteRenderer; |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
@Parameter( property = "reports", required = true, readonly = true ) |
147 | |
protected List<MavenReport> reports; |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
@Parameter( defaultValue = "${basedir}/xdocs" ) |
155 | |
private File xdocDirectory; |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
@Parameter( alias = "workingDirectory", defaultValue = "${project.build.directory}/generated-site" ) |
164 | |
protected File generatedSiteDirectory; |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
@Component |
170 | |
protected MavenSession mavenSession; |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
@Parameter |
184 | |
private ReportPlugin[] reportPlugins; |
185 | |
|
186 | |
private PlexusContainer container; |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
@Parameter( property = "relativizeDecorationLinks", defaultValue = "true" ) |
198 | |
private boolean relativizeDecorationLinks; |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
@Parameter( property = "generateProjectInfo", defaultValue = "true" ) |
206 | |
private boolean generateProjectInfo; |
207 | |
|
208 | |
|
209 | |
public void contextualize( Context context ) |
210 | |
throws ContextException |
211 | |
{ |
212 | 3 | container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
213 | 3 | } |
214 | |
|
215 | |
protected List<MavenReportExecution> getReports() |
216 | |
throws MojoExecutionException |
217 | |
{ |
218 | 0 | if ( isMaven3OrMore() ) |
219 | |
{ |
220 | 0 | MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest(); |
221 | 0 | mavenReportExecutorRequest.setLocalRepository( localRepository ); |
222 | 0 | mavenReportExecutorRequest.setMavenSession( mavenSession ); |
223 | 0 | mavenReportExecutorRequest.setProject( project ); |
224 | 0 | mavenReportExecutorRequest.setReportPlugins( reportPlugins ); |
225 | |
|
226 | |
MavenReportExecutor mavenReportExecutor; |
227 | |
try |
228 | |
{ |
229 | 0 | mavenReportExecutor = (MavenReportExecutor) container.lookup( MavenReportExecutor.class.getName() ); |
230 | |
} |
231 | 0 | catch ( ComponentLookupException e ) |
232 | |
{ |
233 | 0 | throw new MojoExecutionException( "could not get MavenReportExecutor component", e ); |
234 | 0 | } |
235 | 0 | return mavenReportExecutor.buildMavenReports( mavenReportExecutorRequest ); |
236 | |
} |
237 | |
|
238 | 0 | List<MavenReportExecution> reportExecutions = new ArrayList<MavenReportExecution>( reports.size() ); |
239 | 0 | for ( MavenReport report : reports ) |
240 | |
{ |
241 | 0 | if ( report.canGenerateReport() ) |
242 | |
{ |
243 | 0 | reportExecutions.add( new MavenReportExecution( report ) ); |
244 | |
} |
245 | |
} |
246 | 0 | return reportExecutions; |
247 | |
} |
248 | |
|
249 | |
protected SiteRenderingContext createSiteRenderingContext( Locale locale ) |
250 | |
throws MojoExecutionException, IOException, MojoFailureException |
251 | |
{ |
252 | 0 | if ( attributes == null ) |
253 | |
{ |
254 | 0 | attributes = new HashMap<String, Object>(); |
255 | |
} |
256 | |
|
257 | 0 | if ( attributes.get( "project" ) == null ) |
258 | |
{ |
259 | 0 | attributes.put( "project", project ); |
260 | |
} |
261 | |
|
262 | 0 | if ( attributes.get( "inputEncoding" ) == null ) |
263 | |
{ |
264 | 0 | attributes.put( "inputEncoding", getInputEncoding() ); |
265 | |
} |
266 | |
|
267 | 0 | if ( attributes.get( "outputEncoding" ) == null ) |
268 | |
{ |
269 | 0 | attributes.put( "outputEncoding", getOutputEncoding() ); |
270 | |
} |
271 | |
|
272 | |
|
273 | 0 | for ( Map.Entry<Object, Object> entry : project.getProperties().entrySet() ) |
274 | |
{ |
275 | 0 | attributes.put( (String) entry.getKey(), entry.getValue() ); |
276 | |
} |
277 | |
|
278 | |
DecorationModel decorationModel; |
279 | |
try |
280 | |
{ |
281 | 0 | decorationModel = siteTool.getDecorationModel( project, reactorProjects, localRepository, repositories, |
282 | |
siteTool.getRelativePath( siteDirectory.getAbsolutePath(), |
283 | |
project.getBasedir().getAbsolutePath() ), |
284 | |
locale, getInputEncoding(), getOutputEncoding() ); |
285 | |
} |
286 | 0 | catch ( SiteToolException e ) |
287 | |
{ |
288 | 0 | throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e ); |
289 | 0 | } |
290 | |
|
291 | 0 | if ( relativizeDecorationLinks ) |
292 | |
{ |
293 | 0 | final String url = project.getUrl(); |
294 | |
|
295 | 0 | if ( url == null ) |
296 | |
{ |
297 | 0 | getLog().warn( "No project URL defined - decoration links will not be relativized!" ); |
298 | |
} |
299 | |
else |
300 | |
{ |
301 | 0 | getLog().info( "Relativizing decoration links with respect to project URL: " + url ); |
302 | 0 | assembler.resolvePaths( decorationModel, url ); |
303 | |
} |
304 | |
} |
305 | |
|
306 | 0 | if ( template != null ) |
307 | |
{ |
308 | 0 | if ( templateFile != null ) |
309 | |
{ |
310 | 0 | getLog().warn( "'template' configuration is ignored when 'templateFile' is set" ); |
311 | |
} |
312 | |
else |
313 | |
{ |
314 | 0 | templateFile = new File( templateDirectory, template ); |
315 | |
} |
316 | |
} |
317 | |
|
318 | |
File skinFile; |
319 | |
try |
320 | |
{ |
321 | 0 | Artifact skinArtifact = |
322 | |
siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel ); |
323 | 0 | getLog().info( "Rendering site with " + skinArtifact.getId() + " skin." ); |
324 | |
|
325 | 0 | skinFile = skinArtifact.getFile(); |
326 | |
} |
327 | 0 | catch ( SiteToolException e ) |
328 | |
{ |
329 | 0 | throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e ); |
330 | 0 | } |
331 | |
SiteRenderingContext context; |
332 | 0 | if ( templateFile != null ) |
333 | |
{ |
334 | 0 | if ( !templateFile.exists() ) |
335 | |
{ |
336 | 0 | throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" ); |
337 | |
} |
338 | 0 | context = siteRenderer.createContextForTemplate( templateFile, skinFile, attributes, decorationModel, |
339 | |
project.getName(), locale ); |
340 | |
} |
341 | |
else |
342 | |
{ |
343 | 0 | context = siteRenderer.createContextForSkin( skinFile, attributes, decorationModel, project.getName(), |
344 | |
locale ); |
345 | |
} |
346 | |
|
347 | |
|
348 | 0 | if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) ) |
349 | |
{ |
350 | 0 | context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) ); |
351 | 0 | context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" ); |
352 | 0 | context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" ); |
353 | |
} |
354 | |
else |
355 | |
{ |
356 | 0 | context.addSiteDirectory( siteDirectory ); |
357 | 0 | context.addModuleDirectory( xdocDirectory, "xdoc" ); |
358 | 0 | context.addModuleDirectory( xdocDirectory, "fml" ); |
359 | |
} |
360 | |
|
361 | 0 | if ( moduleExcludes != null ) |
362 | |
{ |
363 | 0 | context.setModuleExcludes( moduleExcludes ); |
364 | |
} |
365 | |
|
366 | 0 | return context; |
367 | |
} |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
protected Map<String, MavenReport> locateReports( List<MavenReportExecution> reports, |
383 | |
Map<String, DocumentRenderer> documents, Locale locale ) |
384 | |
{ |
385 | |
|
386 | 0 | List<MavenReportExecution> filtered = new ArrayList<MavenReportExecution>( reports ); |
387 | |
|
388 | 0 | Map<String, MavenReport> reportsByOutputName = new LinkedHashMap<String, MavenReport>(); |
389 | 0 | for ( MavenReportExecution mavenReportExecution : filtered ) |
390 | |
{ |
391 | 0 | MavenReport report = mavenReportExecution.getMavenReport(); |
392 | |
|
393 | 0 | String outputName = report.getOutputName() + ".html"; |
394 | |
|
395 | |
|
396 | 0 | reportsByOutputName.put( report.getOutputName(), report ); |
397 | |
|
398 | 0 | if ( documents.containsKey( outputName ) ) |
399 | |
{ |
400 | 0 | String displayLanguage = locale.getDisplayLanguage( Locale.ENGLISH ); |
401 | |
|
402 | 0 | getLog().info( "Skipped \"" + report.getName( locale ) + "\" report, file \"" + outputName |
403 | |
+ "\" already exists for the " + displayLanguage + " version." ); |
404 | 0 | reports.remove( mavenReportExecution ); |
405 | 0 | } |
406 | |
else |
407 | |
{ |
408 | 0 | RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName ); |
409 | 0 | DocumentRenderer renderer = new ReportDocumentRenderer( mavenReportExecution, renderingContext, getLog() ); |
410 | 0 | documents.put( outputName, renderer ); |
411 | |
} |
412 | 0 | } |
413 | 0 | return reportsByOutputName; |
414 | |
} |
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
protected Map<String, List<MavenReport>> categoriseReports( Collection<MavenReport> reports ) |
424 | |
{ |
425 | 0 | Map<String, List<MavenReport>> categories = new LinkedHashMap<String, List<MavenReport>>(); |
426 | 0 | for ( MavenReport report : reports ) |
427 | |
{ |
428 | 0 | List<MavenReport> categoryReports = categories.get( report.getCategoryName() ); |
429 | 0 | if ( categoryReports == null ) |
430 | |
{ |
431 | 0 | categoryReports = new ArrayList<MavenReport>(); |
432 | 0 | categories.put( report.getCategoryName(), categoryReports ); |
433 | |
} |
434 | 0 | categoryReports.add( report ); |
435 | 0 | } |
436 | 0 | return categories; |
437 | |
} |
438 | |
|
439 | |
protected Map<String, DocumentRenderer> locateDocuments( SiteRenderingContext context, List<MavenReportExecution> reports, |
440 | |
Locale locale ) |
441 | |
throws IOException, RendererException |
442 | |
{ |
443 | 0 | Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles( context ); |
444 | |
|
445 | 0 | Map<String, MavenReport> reportsByOutputName = locateReports( reports, documents, locale ); |
446 | |
|
447 | |
|
448 | 0 | Map<String, List<MavenReport>> categories = categoriseReports( reportsByOutputName.values() ); |
449 | |
|
450 | 0 | siteTool.populateReportsMenu( context.getDecoration(), locale, categories ); |
451 | 0 | populateReportItems( context.getDecoration(), locale, reportsByOutputName ); |
452 | |
|
453 | 0 | if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) && generateProjectInfo ) |
454 | |
{ |
455 | 0 | List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION ); |
456 | |
|
457 | 0 | RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-info.html" ); |
458 | 0 | String title = i18n.getString( "site-plugin", locale, "report.information.title" ); |
459 | 0 | String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" ); |
460 | 0 | String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" ); |
461 | 0 | DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2, |
462 | |
i18n, categoryReports, getLog() ); |
463 | |
|
464 | 0 | if ( !documents.containsKey( renderer.getOutputName() ) ) |
465 | |
{ |
466 | 0 | documents.put( renderer.getOutputName(), renderer ); |
467 | |
} |
468 | |
else |
469 | |
{ |
470 | 0 | getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" ); |
471 | |
} |
472 | |
} |
473 | |
|
474 | 0 | if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) ) |
475 | |
{ |
476 | 0 | List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_REPORTS ); |
477 | 0 | RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-reports.html" ); |
478 | 0 | String title = i18n.getString( "site-plugin", locale, "report.project.title" ); |
479 | 0 | String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" ); |
480 | 0 | String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" ); |
481 | 0 | DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2, |
482 | |
i18n, categoryReports, getLog() ); |
483 | |
|
484 | 0 | if ( !documents.containsKey( renderer.getOutputName() ) ) |
485 | |
{ |
486 | 0 | documents.put( renderer.getOutputName(), renderer ); |
487 | |
} |
488 | |
else |
489 | |
{ |
490 | 0 | getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" ); |
491 | |
} |
492 | |
} |
493 | 0 | return documents; |
494 | |
} |
495 | |
|
496 | |
protected void populateReportItems( DecorationModel decorationModel, Locale locale, |
497 | |
Map<String, MavenReport> reportsByOutputName ) |
498 | |
{ |
499 | 0 | for ( Menu menu : decorationModel.getMenus() ) |
500 | |
{ |
501 | 0 | populateItemRefs( menu.getItems(), locale, reportsByOutputName ); |
502 | |
} |
503 | 0 | } |
504 | |
|
505 | |
private void populateItemRefs( List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName ) |
506 | |
{ |
507 | 0 | for ( Iterator<MenuItem> i = items.iterator(); i.hasNext(); ) |
508 | |
{ |
509 | 0 | MenuItem item = i.next(); |
510 | |
|
511 | 0 | if ( item.getRef() != null ) |
512 | |
{ |
513 | 0 | MavenReport report = reportsByOutputName.get( item.getRef() ); |
514 | |
|
515 | 0 | if ( report != null ) |
516 | |
{ |
517 | 0 | if ( item.getName() == null ) |
518 | |
{ |
519 | 0 | item.setName( report.getName( locale ) ); |
520 | |
} |
521 | |
|
522 | 0 | if ( item.getHref() == null || item.getHref().length() == 0 ) |
523 | |
{ |
524 | 0 | item.setHref( report.getOutputName() + ".html" ); |
525 | |
} |
526 | |
} |
527 | |
else |
528 | |
{ |
529 | 0 | getLog().warn( "Unrecognised reference: '" + item.getRef() + "'" ); |
530 | 0 | i.remove(); |
531 | |
} |
532 | |
} |
533 | |
|
534 | 0 | populateItemRefs( item.getItems(), locale, reportsByOutputName ); |
535 | 0 | } |
536 | 0 | } |
537 | |
} |