1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.javadoc;
20
21 import java.io.File;
22 import java.nio.file.Path;
23 import java.util.Collection;
24 import java.util.Locale;
25 import java.util.Map;
26 import java.util.ResourceBundle;
27 import java.util.stream.Collectors;
28
29 import org.apache.maven.doxia.sink.Sink;
30 import org.apache.maven.doxia.sink.SinkFactory;
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.MavenMultiPageReport;
41 import org.apache.maven.reporting.MavenReportException;
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 extends AbstractJavadocMojo implements MavenMultiPageReport {
55
56
57
58
59
60
61
62 @Parameter(
63 property = "reportOutputDirectory",
64 defaultValue = "${project.reporting.outputDirectory}/apidocs",
65 required = true)
66 private File reportOutputDirectory;
67
68
69
70
71
72
73
74 @Parameter(property = "destDir", defaultValue = "apidocs")
75 private String destDir;
76
77
78
79
80
81
82
83 @Parameter(property = "name")
84 private String name;
85
86
87
88
89
90
91
92 @Parameter(property = "description")
93 private String description;
94
95
96
97
98
99
100 @Override
101 public String getName(Locale locale) {
102 if (name == null || name.isEmpty()) {
103 return getBundle(locale).getString("report.javadoc.name");
104 }
105
106 return name;
107 }
108
109
110 @Override
111 public String getDescription(Locale locale) {
112 if (description == null || description.isEmpty()) {
113 return getBundle(locale).getString("report.javadoc.description");
114 }
115
116 return description;
117 }
118
119
120 @Override
121 public void generate(org.codehaus.doxia.sink.Sink sink, Locale locale) throws MavenReportException {
122 generate(sink, null, locale);
123 }
124
125 public void generate(Sink sink, Locale locale) throws MavenReportException {
126 generate(sink, null, locale);
127 }
128
129
130 @Override
131 public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException {
132 outputDirectory = getReportOutputDirectory();
133
134 try {
135 executeReport(locale);
136 } catch (MavenReportException | RuntimeException e) {
137 if (failOnError) {
138 throw e;
139 }
140 getLog().error("Error while creating javadoc report: " + e.getMessage(), e);
141 }
142 }
143
144
145 @Override
146 public String getOutputName() {
147 return destDir + "/index";
148 }
149
150
151 @Override
152 public boolean isExternalReport() {
153 return true;
154 }
155
156
157
158
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 @Override
221 public boolean canGenerateReport() {
222 boolean canGenerate = false;
223
224 if (!skip && (this.isAggregator() || !"pom".equals(this.project.getPackaging()))) {
225 Collection<Path> sourcePaths;
226 Map<Path, Collection<String>> files;
227 try {
228 sourcePaths = getSourcePaths().stream()
229 .flatMap(e -> e.getSourcePaths().stream())
230 .collect(Collectors.toList());
231 files = getFiles(sourcePaths);
232 } catch (MavenReportException e) {
233 getLog().error(e.getMessage(), e);
234 return false;
235 }
236
237 canGenerate = canGenerateReport(files);
238 }
239 if (getLog().isDebugEnabled()) {
240 getLog().debug(" canGenerateReport = " + canGenerate + " for project " + this.project);
241 }
242 return canGenerate;
243 }
244
245
246 @Override
247 public String getCategoryName() {
248 return CATEGORY_PROJECT_REPORTS;
249 }
250
251
252 @Override
253 public File getReportOutputDirectory() {
254 if (reportOutputDirectory == null) {
255 return outputDirectory;
256 }
257
258 return reportOutputDirectory;
259 }
260
261
262
263
264
265
266 @Override
267 public void setReportOutputDirectory(File reportOutputDirectory) {
268 updateReportOutputDirectory(reportOutputDirectory, destDir);
269 }
270
271
272
273
274 public void setDestDir(String theDestDir) {
275 this.destDir = theDestDir;
276 updateReportOutputDirectory(reportOutputDirectory, theDestDir);
277 }
278
279 private void updateReportOutputDirectory(File reportOutputDirectory, String destDir) {
280 if (reportOutputDirectory != null
281 && destDir != null
282 && !reportOutputDirectory.getAbsolutePath().endsWith(destDir)) {
283 this.reportOutputDirectory = new File(reportOutputDirectory, destDir);
284 } else {
285 this.reportOutputDirectory = reportOutputDirectory;
286 }
287 }
288
289
290 @Override
291 public void doExecute() throws MojoExecutionException, MojoFailureException {
292 File outputDirectory = new File(getOutputDirectory());
293
294 String filename = getOutputName() + ".html";
295
296 Locale locale = Locale.getDefault();
297
298 try {
299
300 RenderingContext docRenderingContext = new RenderingContext(outputDirectory, filename, null);
301
302 SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
303
304 generate(sink, null, locale);
305
306 } catch (MavenReportException | RuntimeException e) {
307 failOnError("An error has occurred in " + getName(Locale.ENGLISH) + " report generation", e);
308 }
309 }
310
311
312
313
314
315
316
317 private ResourceBundle getBundle(Locale locale) {
318 return ResourceBundle.getBundle("javadoc-report", locale, getClass().getClassLoader());
319 }
320 }