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