1 package org.apache.maven.plugins.javadoc;
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.util.Collection;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.ResourceBundle;
27
28 import org.apache.maven.doxia.siterenderer.RenderingContext;
29 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.MojoFailureException;
32 import org.apache.maven.plugins.annotations.Execute;
33 import org.apache.maven.plugins.annotations.LifecyclePhase;
34 import org.apache.maven.plugins.annotations.Mojo;
35 import org.apache.maven.plugins.annotations.Parameter;
36 import org.apache.maven.plugins.annotations.ResolutionScope;
37 import org.apache.maven.reporting.MavenReport;
38 import org.apache.maven.reporting.MavenReportException;
39 import org.codehaus.doxia.sink.Sink;
40 import org.codehaus.plexus.util.StringUtils;
41
42
43
44
45
46
47
48
49
50
51
52
53 @Mojo( name = "javadoc", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
54 @Execute( phase = LifecyclePhase.GENERATE_SOURCES )
55 public class JavadocReport
56 extends AbstractJavadocMojo
57 implements MavenReport
58 {
59
60
61
62
63
64
65
66 @Parameter( property = "reportOutputDirectory", defaultValue = "${project.reporting.outputDirectory}/apidocs",
67 required = true )
68 private File reportOutputDirectory;
69
70
71
72
73
74
75
76 @Parameter( property = "destDir", defaultValue = "apidocs" )
77 private String destDir;
78
79
80
81
82
83
84
85 @Parameter( property = "name" )
86 private String name;
87
88
89
90
91
92
93
94 @Parameter( property = "description" )
95 private String description;
96
97
98
99
100
101
102 @Override
103 public String getName( Locale locale )
104 {
105 if ( StringUtils.isEmpty( name ) )
106 {
107 return getBundle( locale ).getString( "report.javadoc.name" );
108 }
109
110 return name;
111 }
112
113
114 @Override
115 public String getDescription( Locale locale )
116 {
117 if ( StringUtils.isEmpty( description ) )
118 {
119 return getBundle( locale ).getString( "report.javadoc.description" );
120 }
121
122 return description;
123 }
124
125
126 @Override
127 public void generate( Sink sink, Locale locale )
128 throws MavenReportException
129 {
130 outputDirectory = getReportOutputDirectory();
131
132 try
133 {
134 executeReport( locale );
135 }
136 catch ( MavenReportException e )
137 {
138 if ( failOnError )
139 {
140 throw e;
141 }
142 getLog().error( "Error while creating javadoc report: " + e.getMessage(), e );
143 }
144 catch ( RuntimeException e )
145 {
146 if ( failOnError )
147 {
148 throw e;
149 }
150 getLog().error( "Error while creating javadoc report: " + e.getMessage(), e );
151 }
152 }
153
154
155 @Override
156 public String getOutputName()
157 {
158 return destDir + "/index";
159 }
160
161
162 @Override
163 public boolean isExternalReport()
164 {
165 return true;
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 @Override
233 public boolean canGenerateReport()
234 {
235 boolean canGenerate = false;
236
237 if ( !this.isAggregator() || ( this.isAggregator() && this.project.isExecutionRoot() ) )
238 {
239 Collection<String> sourcePaths;
240 List<String> files;
241 try
242 {
243 sourcePaths = collect( getSourcePaths().values() );
244 files = getFiles( sourcePaths );
245 }
246 catch ( MavenReportException e )
247 {
248 getLog().error( e.getMessage(), e );
249 return false;
250 }
251
252 canGenerate = canGenerateReport( files );
253 }
254 if ( getLog().isDebugEnabled() )
255 {
256 getLog().debug( " canGenerateReport = " + canGenerate + " for project " + this.project );
257 }
258 return canGenerate;
259 }
260
261
262 @Override
263 public String getCategoryName()
264 {
265 return CATEGORY_PROJECT_REPORTS;
266 }
267
268
269 @Override
270 public File getReportOutputDirectory()
271 {
272 if ( reportOutputDirectory == null )
273 {
274 return outputDirectory;
275 }
276
277 return reportOutputDirectory;
278 }
279
280
281
282
283
284
285 @Override
286 public void setReportOutputDirectory( File reportOutputDirectory )
287 {
288 updateReportOutputDirectory( reportOutputDirectory, destDir );
289 }
290
291
292
293
294 public void setDestDir( String theDestDir )
295 {
296 this.destDir = theDestDir;
297 updateReportOutputDirectory( reportOutputDirectory, theDestDir );
298 }
299
300 private void updateReportOutputDirectory( File reportOutputDirectory, String destDir )
301 {
302 if ( reportOutputDirectory != null && destDir != null
303 && !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) )
304 {
305 this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
306 }
307 else
308 {
309 this.reportOutputDirectory = reportOutputDirectory;
310 }
311 }
312
313
314 @Override
315 public void doExecute()
316 throws MojoExecutionException, MojoFailureException
317 {
318 if ( skip )
319 {
320 getLog().info( "Skipping javadoc generation" );
321 return;
322 }
323
324 try
325 {
326 RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
327 SiteRendererSink sink = new SiteRendererSink( context );
328 Locale locale = Locale.getDefault();
329 generate( sink, locale );
330 }
331 catch ( MavenReportException e )
332 {
333 failOnError( "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation", e );
334 }
335 catch ( RuntimeException e )
336 {
337 failOnError( "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation", e );
338 }
339 }
340
341
342
343
344
345
346
347 private ResourceBundle getBundle( Locale locale )
348 {
349 return ResourceBundle.getBundle( "javadoc-report", locale, getClass().getClassLoader() );
350 }
351 }