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