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
127 @Override
128 public void generate( Sink sink, Locale locale )
129 throws MavenReportException
130 {
131 outputDirectory = getReportOutputDirectory();
132
133 try
134 {
135 executeReport( locale );
136 }
137 catch ( MavenReportException | RuntimeException e )
138 {
139 if ( failOnError )
140 {
141 throw e;
142 }
143 getLog().error( "Error while creating javadoc report: " + e.getMessage(), e );
144 }
145 }
146
147
148 @Override
149 public String getOutputName()
150 {
151 return destDir + "/index";
152 }
153
154
155 @Override
156 public boolean isExternalReport()
157 {
158 return true;
159 }
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 @Override
226 public boolean canGenerateReport()
227 {
228 boolean canGenerate = false;
229
230 if ( this.isAggregator() || !"pom".equals( this.project.getPackaging() ) )
231 {
232 Collection<Path> sourcePaths;
233 Map<Path, Collection<String>> files;
234 try
235 {
236 sourcePaths = getSourcePaths().stream()
237 .flatMap( e -> e.getSourcePaths().stream() )
238 .collect( Collectors.toList() );
239 files = getFiles( sourcePaths );
240 }
241 catch ( MavenReportException e )
242 {
243 getLog().error( e.getMessage(), e );
244 return false;
245 }
246
247 canGenerate = canGenerateReport( files );
248 }
249 if ( getLog().isDebugEnabled() )
250 {
251 getLog().debug( " canGenerateReport = " + canGenerate + " for project " + this.project );
252 }
253 return canGenerate;
254 }
255
256
257 @Override
258 public String getCategoryName()
259 {
260 return CATEGORY_PROJECT_REPORTS;
261 }
262
263
264 @Override
265 public File getReportOutputDirectory()
266 {
267 if ( reportOutputDirectory == null )
268 {
269 return outputDirectory;
270 }
271
272 return reportOutputDirectory;
273 }
274
275
276
277
278
279
280 @Override
281 public void setReportOutputDirectory( File reportOutputDirectory )
282 {
283 updateReportOutputDirectory( reportOutputDirectory, destDir );
284 }
285
286
287
288
289 public void setDestDir( String theDestDir )
290 {
291 this.destDir = theDestDir;
292 updateReportOutputDirectory( reportOutputDirectory, theDestDir );
293 }
294
295 private void updateReportOutputDirectory( File reportOutputDirectory, String destDir )
296 {
297 if ( reportOutputDirectory != null && destDir != null
298 && !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) )
299 {
300 this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
301 }
302 else
303 {
304 this.reportOutputDirectory = reportOutputDirectory;
305 }
306 }
307
308
309 @Override
310 public void doExecute()
311 throws MojoExecutionException, MojoFailureException
312 {
313 if ( skip )
314 {
315 getLog().info( "Skipping javadoc generation" );
316 return;
317 }
318
319 try
320 {
321 RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
322 SiteRendererSink sink = new SiteRendererSink( context );
323 Locale locale = Locale.getDefault();
324 generate( sink, locale );
325 }
326 catch ( MavenReportException | RuntimeException e )
327 {
328 failOnError( "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation", e );
329 }
330 }
331
332
333
334
335
336
337
338 private ResourceBundle getBundle( Locale locale )
339 {
340 return ResourceBundle.getBundle( "javadoc-report", locale, getClass().getClassLoader() );
341 }
342 }