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 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( defaultValue = "${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( readonly = true )
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 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
213 }
214
215 protected List<MavenReportExecution> getReports()
216 throws MojoExecutionException
217 {
218 if ( isMaven3OrMore() )
219 {
220 MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest();
221 mavenReportExecutorRequest.setLocalRepository( localRepository );
222 mavenReportExecutorRequest.setMavenSession( mavenSession );
223 mavenReportExecutorRequest.setProject( project );
224 mavenReportExecutorRequest.setReportPlugins( reportPlugins );
225
226 MavenReportExecutor mavenReportExecutor;
227 try
228 {
229 mavenReportExecutor = (MavenReportExecutor) container.lookup( MavenReportExecutor.class.getName() );
230 }
231 catch ( ComponentLookupException e )
232 {
233 throw new MojoExecutionException( "could not get MavenReportExecutor component", e );
234 }
235 return mavenReportExecutor.buildMavenReports( mavenReportExecutorRequest );
236 }
237
238 List<MavenReportExecution> reportExecutions = new ArrayList<MavenReportExecution>( reports.size() );
239 for ( MavenReport report : reports )
240 {
241 if ( report.canGenerateReport() )
242 {
243 reportExecutions.add( new MavenReportExecution( report ) );
244 }
245 }
246 return reportExecutions;
247 }
248
249 protected SiteRenderingContext createSiteRenderingContext( Locale locale )
250 throws MojoExecutionException, IOException, MojoFailureException
251 {
252 if ( attributes == null )
253 {
254 attributes = new HashMap<String, Object>();
255 }
256
257 if ( attributes.get( "project" ) == null )
258 {
259 attributes.put( "project", project );
260 }
261
262 if ( attributes.get( "inputEncoding" ) == null )
263 {
264 attributes.put( "inputEncoding", getInputEncoding() );
265 }
266
267 if ( attributes.get( "outputEncoding" ) == null )
268 {
269 attributes.put( "outputEncoding", getOutputEncoding() );
270 }
271
272
273 for ( Map.Entry<Object, Object> entry : project.getProperties().entrySet() )
274 {
275 attributes.put( (String) entry.getKey(), entry.getValue() );
276 }
277
278 DecorationModel decorationModel;
279 try
280 {
281 decorationModel = siteTool.getDecorationModel( project, reactorProjects, localRepository, repositories,
282 siteTool.getRelativePath( siteDirectory.getAbsolutePath(),
283 project.getBasedir().getAbsolutePath() ),
284 locale, getInputEncoding(), getOutputEncoding() );
285 }
286 catch ( SiteToolException e )
287 {
288 throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
289 }
290
291 if ( relativizeDecorationLinks )
292 {
293 final String url = project.getUrl();
294
295 if ( url == null )
296 {
297 getLog().warn( "No project URL defined - decoration links will not be relativized!" );
298 }
299 else
300 {
301
302 final String localeUrl =
303 locale.equals( Locale.getDefault() ) ? url : url + "/" + locale.getLanguage();
304 getLog().info( "Relativizing decoration links with respect to project URL: " + localeUrl );
305 assembler.resolvePaths( decorationModel, localeUrl );
306 }
307 }
308
309 if ( template != null )
310 {
311 if ( templateFile != null )
312 {
313 getLog().warn( "'template' configuration is ignored when 'templateFile' is set" );
314 }
315 else
316 {
317 templateFile = new File( templateDirectory, template );
318 }
319 }
320
321 File skinFile;
322 try
323 {
324 Artifact skinArtifact =
325 siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel );
326 getLog().info( "Rendering site with " + skinArtifact.getId() + " skin." );
327
328 skinFile = skinArtifact.getFile();
329 }
330 catch ( SiteToolException e )
331 {
332 throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
333 }
334 SiteRenderingContext context;
335 if ( templateFile != null )
336 {
337 if ( !templateFile.exists() )
338 {
339 throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" );
340 }
341 context = siteRenderer.createContextForTemplate( templateFile, skinFile, attributes, decorationModel,
342 project.getName(), locale );
343 }
344 else
345 {
346 context = siteRenderer.createContextForSkin( skinFile, attributes, decorationModel, project.getName(),
347 locale );
348 }
349
350
351 if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
352 {
353 context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) );
354 context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" );
355 context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" );
356 }
357 else
358 {
359 context.addSiteDirectory( siteDirectory );
360 context.addModuleDirectory( xdocDirectory, "xdoc" );
361 context.addModuleDirectory( xdocDirectory, "fml" );
362 }
363
364 if ( moduleExcludes != null )
365 {
366 context.setModuleExcludes( moduleExcludes );
367 }
368
369 return context;
370 }
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385 protected Map<String, MavenReport> locateReports( List<MavenReportExecution> reports,
386 Map<String, DocumentRenderer> documents, Locale locale )
387 {
388
389 List<MavenReportExecution> filtered = new ArrayList<MavenReportExecution>( reports );
390
391 Map<String, MavenReport> reportsByOutputName = new LinkedHashMap<String, MavenReport>();
392 for ( MavenReportExecution mavenReportExecution : filtered )
393 {
394 MavenReport report = mavenReportExecution.getMavenReport();
395
396 String outputName = report.getOutputName() + ".html";
397
398
399 reportsByOutputName.put( report.getOutputName(), report );
400
401 if ( documents.containsKey( outputName ) )
402 {
403 String displayLanguage = locale.getDisplayLanguage( Locale.ENGLISH );
404
405 getLog().info( "Skipped \"" + report.getName( locale ) + "\" report, file \"" + outputName
406 + "\" already exists for the " + displayLanguage + " version." );
407 reports.remove( mavenReportExecution );
408 }
409 else
410 {
411 RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName );
412 DocumentRenderer renderer = new ReportDocumentRenderer( mavenReportExecution, renderingContext, getLog() );
413 documents.put( outputName, renderer );
414 }
415 }
416 return reportsByOutputName;
417 }
418
419
420
421
422
423
424
425
426 protected Map<String, List<MavenReport>> categoriseReports( Collection<MavenReport> reports )
427 {
428 Map<String, List<MavenReport>> categories = new LinkedHashMap<String, List<MavenReport>>();
429 for ( MavenReport report : reports )
430 {
431 List<MavenReport> categoryReports = categories.get( report.getCategoryName() );
432 if ( categoryReports == null )
433 {
434 categoryReports = new ArrayList<MavenReport>();
435 categories.put( report.getCategoryName(), categoryReports );
436 }
437 categoryReports.add( report );
438 }
439 return categories;
440 }
441
442 protected Map<String, DocumentRenderer> locateDocuments( SiteRenderingContext context, List<MavenReportExecution> reports,
443 Locale locale )
444 throws IOException, RendererException
445 {
446 Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles( context );
447
448 Map<String, MavenReport> reportsByOutputName = locateReports( reports, documents, locale );
449
450
451 Map<String, List<MavenReport>> categories = categoriseReports( reportsByOutputName.values() );
452
453 siteTool.populateReportsMenu( context.getDecoration(), locale, categories );
454 populateReportItems( context.getDecoration(), locale, reportsByOutputName );
455
456 if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) && generateProjectInfo )
457 {
458 List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION );
459
460 RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-info.html" );
461 String title = i18n.getString( "site-plugin", locale, "report.information.title" );
462 String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" );
463 String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" );
464 DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
465 i18n, categoryReports, getLog() );
466
467 if ( !documents.containsKey( renderer.getOutputName() ) )
468 {
469 documents.put( renderer.getOutputName(), renderer );
470 }
471 else
472 {
473 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
474 }
475 }
476
477 if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) )
478 {
479 List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_REPORTS );
480 RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-reports.html" );
481 String title = i18n.getString( "site-plugin", locale, "report.project.title" );
482 String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" );
483 String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" );
484 DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
485 i18n, categoryReports, getLog() );
486
487 if ( !documents.containsKey( renderer.getOutputName() ) )
488 {
489 documents.put( renderer.getOutputName(), renderer );
490 }
491 else
492 {
493 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
494 }
495 }
496 return documents;
497 }
498
499 protected void populateReportItems( DecorationModel decorationModel, Locale locale,
500 Map<String, MavenReport> reportsByOutputName )
501 {
502 for ( Menu menu : decorationModel.getMenus() )
503 {
504 populateItemRefs( menu.getItems(), locale, reportsByOutputName );
505 }
506 }
507
508 private void populateItemRefs( List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName )
509 {
510 for ( Iterator<MenuItem> i = items.iterator(); i.hasNext(); )
511 {
512 MenuItem item = i.next();
513
514 if ( item.getRef() != null )
515 {
516 MavenReport report = reportsByOutputName.get( item.getRef() );
517
518 if ( report != null )
519 {
520 if ( item.getName() == null )
521 {
522 item.setName( report.getName( locale ) );
523 }
524
525 if ( item.getHref() == null || item.getHref().length() == 0 )
526 {
527 item.setHref( report.getOutputName() + ".html" );
528 }
529 }
530 else
531 {
532 getLog().warn( "Unrecognised reference: '" + item.getRef() + "'" );
533 i.remove();
534 }
535 }
536
537 populateItemRefs( item.getItems(), locale, reportsByOutputName );
538 }
539 }
540 }