1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.apidoc;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.Map;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.DependencyResolutionRequiredException;
28 import org.apache.maven.plugin.logging.Log;
29 import org.apache.maven.reporting.MavenReportException;
30 import org.codehaus.plexus.util.StringUtils;
31
32 import de.smartics.analysis.javadoc.JavadocArgumentsUtils;
33 import de.smartics.analysis.javadoc.JavadocUtils;
34 import de.smartics.analysis.javadoc.conf.DefaultJavadocProjectConfiguration;
35 import de.smartics.analysis.javadoc.conf.JavadocProjectConfiguration;
36 import de.smartics.analysis.javadoc.log.JavadocMessageLogger;
37 import de.smartics.analysis.javadoc.util.StringFunction;
38 import de.smartics.maven.util.PathUtils;
39
40
41
42
43
44
45
46
47
48
49
50
51 public class ApiDocReportMojo extends AbstractReportMojo
52 {
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 protected List<Artifact> pluginArtifacts;
68
69
70
71
72
73
74
75
76
77
78
79 private String additionalSources;
80
81
82
83
84
85
86
87
88 private String sourceEncoding;
89
90
91
92
93
94
95
96 private String sourceVersion;
97
98
99
100
101
102
103
104
105 private String includes;
106
107
108
109
110
111
112
113
114 private String excludes;
115
116
117
118
119
120
121
122
123 private String xrefLocation;
124
125
126
127
128
129
130
131
132
133 private boolean noticeMessagesRendered;
134
135
136
137
138
139
140
141 private String doclet;
142
143
144
145
146
147
148
149
150
151
152
153
154 private String reportSetId;
155
156
157
158
159
160
161
162
163 private String additionalparam;
164
165
166
167
168
169
170
171
172 private List<String> messageFilter;
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 public String getName(final Locale locale)
192 {
193 return getBundle(locale).getString("report.name");
194 }
195
196
197
198
199
200
201 public String getDescription(final Locale locale)
202 {
203 return getBundle(locale).getString("report.description");
204 }
205
206
207
208
209
210
211 public String getOutputName()
212 {
213 return "apidoc-report";
214 }
215
216
217
218
219
220
221
222
223
224
225 @SuppressWarnings("unchecked")
226 private JavadocProjectConfiguration createProjectConfiguration()
227 throws DependencyResolutionRequiredException
228 {
229 final List<String> classRootDirectoryNames =
230 project.getCompileClasspathElements();
231
232 final List<String> sourceRootDirectoryNames = new ArrayList<String>();
233 sourceRootDirectoryNames.addAll(StringFunction.split(
234 this.additionalSources, ","));
235 sourceRootDirectoryNames.addAll(project.getCompileSourceRoots());
236 final List<String> toolClassPath = PathUtils.toClassPath(pluginArtifacts);
237
238 final Log log = getLog();
239 if (log.isDebugEnabled())
240 {
241 log.debug("Tool class path: " + toolClassPath);
242 log.debug("Class roots : " + classRootDirectoryNames);
243 log.debug("Source roots : " + sourceRootDirectoryNames);
244 }
245
246 final List<String> includesList = StringFunction.split(this.includes, ",");
247 final List<String> excludesList = StringFunction.split(this.excludes, ",");
248
249 final DefaultJavadocProjectConfiguration.Builder builder =
250 new DefaultJavadocProjectConfiguration.Builder(project.getName());
251
252 builder.setIncludes(includesList);
253 builder.setExcludes(excludesList);
254 builder.setSourceEncoding(this.sourceEncoding);
255 builder.setSourceVersion(this.sourceVersion);
256 builder.setToolClassPath(toolClassPath);
257 builder.setClassRootDirectoryNames(classRootDirectoryNames);
258 builder.setSourceRootDirectoryNames(sourceRootDirectoryNames);
259
260 final JavadocProjectConfiguration config = builder.build();
261 return config;
262 }
263
264
265
266
267
268
269 @Override
270 protected void executeReport(final Locale locale) throws MavenReportException
271 {
272 super.executeReport(locale);
273
274 try
275 {
276 final String toolId = "apidoc";
277 final JavadocMessageLogger logger = new JavadocMessageLogger();
278
279 final JavadocProjectConfiguration config = createProjectConfiguration();
280 final Map<String, String> arguments = configureArguments(config);
281 final Log log = getLog();
282 if (!checkConfiguration(arguments))
283 {
284 if (log.isInfoEnabled())
285 {
286 log.info("Report generation skipped. No packages to report.");
287 }
288 return;
289 }
290
291 if (log.isDebugEnabled())
292 {
293 log.debug("Arguments passed to Javadoc tool: " + arguments);
294 }
295
296 final int returnCode =
297 JavadocUtils.executeJavadocParser(toolId, doclet, arguments, logger);
298 if (returnCode != 0)
299 {
300 handleJavadocToolErrorCode(logger);
301 }
302
303 renderReport(locale, config, logger);
304 }
305 catch (final Exception e)
306 {
307 final String message = "Cannot generate " + getName(locale) + ".";
308 if (getLog().isWarnEnabled())
309 {
310 getLog().warn(message, e);
311 }
312 throw new MavenReportException(message, e);
313 }
314 }
315
316 private void handleJavadocToolErrorCode(final JavadocMessageLogger logger)
317 throws IllegalStateException
318 {
319 final String loggerMessage = logger.toString();
320 if (loggerMessage
321 .contains("No public or protected classes found to document"))
322 {
323 return;
324 }
325 else
326 {
327 throw new IllegalStateException("Cannot parse Java files: "
328 + loggerMessage);
329 }
330 }
331
332
333
334
335
336
337
338
339
340 private Map<String, String> configureArguments(
341 final JavadocProjectConfiguration config) throws IOException
342 {
343 final Map<String, String> arguments = new LinkedHashMap<String, String>();
344 if (StringUtils.isNotBlank(additionalparam))
345 {
346 arguments.put(Constants.ADDITIONAL_PARAMS, additionalparam);
347 }
348 final JavadocPluginConfigurationExtractor extractor =
349 new JavadocPluginConfigurationExtractor();
350 extractor.addJavadocPluginArguments(project, reportSetId, arguments);
351 JavadocArgumentsUtils.addJavadocArguments(config, arguments);
352
353 setTargetDirAsArgument(arguments);
354 return arguments;
355 }
356
357
358
359
360
361
362
363
364
365
366
367 private boolean checkConfiguration(final Map<String, String> arguments)
368 {
369 final String packages = arguments.get(JavadocUtils.PARAM_NAME_PACKAGENAMES);
370 return StringUtils.isNotBlank(packages);
371 }
372
373
374
375
376
377
378
379
380
381 private void setTargetDirAsArgument(final Map<String, String> arguments)
382 throws IOException
383 {
384 final File targetDir =
385 new File(project.getBuild().getDirectory(), "apidoc-report");
386 if (!targetDir.exists() && !targetDir.mkdirs())
387 {
388 throw new IOException("Cannot create directory " + targetDir.getPath());
389 }
390 arguments.put("-d", targetDir.getPath());
391 }
392
393
394
395
396
397
398
399
400
401
402 private void renderReport(
403 final Locale locale,
404 final JavadocProjectConfiguration projectConfig,
405 final JavadocMessageLogger logger)
406 {
407 getLog().debug(logger.toString());
408
409 final JavadocMessageConfig.Builder builder =
410 new JavadocMessageConfig.Builder();
411 builder.setBundle(getBundle(locale));
412
413 builder.setSourceRoots(projectConfig.getSourceRootDirectoryNames());
414 builder.setXrefLocation(xrefLocation);
415 builder.setNoticeMessagesRendered(noticeMessagesRendered);
416 builder.setMessageFilter(messageFilter);
417 final JavadocMessageConfig messageConfig = builder.build();
418
419 final JavadocMessageReportRenderer renderer =
420 new JavadocMessageReportRenderer(getSink(), messageConfig, logger);
421 renderer.render();
422 }
423
424
425
426 }