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