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.DocumentRenderingContext;
32 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
33 import org.apache.maven.doxia.tools.SiteTool;
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
44
45
46
47
48
49
50
51
52
53 @Mojo(name = "javadoc", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
54 @Execute(phase = LifecyclePhase.GENERATE_SOURCES)
55 public class JavadocReport extends AbstractJavadocMojo implements MavenMultiPageReport {
56
57
58
59
60
61 private File reportOutputDirectory;
62
63
64
65
66
67
68
69 @Parameter(property = "name")
70 private String name;
71
72
73
74
75
76
77
78 @Parameter(property = "description")
79 private String description;
80
81
82
83
84
85
86 @Override
87 public String getName(Locale locale) {
88 if (name == null || name.isEmpty()) {
89 return getBundle(locale).getString("report.javadoc.name");
90 }
91
92 return name;
93 }
94
95
96 @Override
97 public String getDescription(Locale locale) {
98 if (description == null || description.isEmpty()) {
99 return getBundle(locale).getString("report.javadoc.description");
100 }
101
102 return description;
103 }
104
105
106 public void generate(Sink sink, Locale locale) throws MavenReportException {
107 generate(sink, null, locale);
108 }
109
110
111 @Override
112 public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException {
113 try {
114 executeReport(locale);
115 } catch (MavenReportException | RuntimeException e) {
116 if (failOnError) {
117 throw e;
118 }
119 getLog().error("Error while creating javadoc report: " + e.getMessage(), e);
120 }
121 }
122
123
124 @Override
125 public String getOutputName() {
126 return (isTest() ? "test" : "") + "apidocs" + "/index";
127 }
128
129
130 @Override
131 public boolean isExternalReport() {
132 return true;
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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 @Override
200 public boolean canGenerateReport() throws MavenReportException {
201 if (skip) {
202 return false;
203 }
204
205 Collection<JavadocModule> sourcePaths = getSourcePaths();
206
207 Collection<Path> collectedSourcePaths =
208 sourcePaths.stream().flatMap(e -> e.getSourcePaths().stream()).collect(Collectors.toList());
209
210 Map<Path, Collection<String>> files = getFiles(collectedSourcePaths);
211
212 if (!canGenerateReport(files)) {
213 return false;
214 }
215
216 return true;
217 }
218
219
220 @Override
221 public String getCategoryName() {
222 return CATEGORY_PROJECT_REPORTS;
223 }
224
225
226 @Override
227 public File getReportOutputDirectory() {
228 if (reportOutputDirectory == null) {
229 reportOutputDirectory = new File(getOutputDirectory());
230 }
231
232 return reportOutputDirectory;
233 }
234
235
236 @Override
237 public void setReportOutputDirectory(File reportOutputDirectory) {
238 this.reportOutputDirectory = reportOutputDirectory;
239 this.outputDirectory = reportOutputDirectory;
240 }
241
242
243 @Override
244 protected String getPluginReportOutputDirectory() {
245 return getReportOutputDirectory().getAbsolutePath() + "/" + (isTest() ? "test" : "") + "apidocs";
246 }
247
248
249 @Override
250 protected void doExecute() throws MojoExecutionException, MojoFailureException {
251 try {
252 if (!canGenerateReport()) {
253 String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
254 getLog().info("Skipping " + reportMojoInfo + " report goal");
255 return;
256 }
257 } catch (MavenReportException e) {
258 throw new MojoExecutionException("Failed to determine whether report can be generated", e);
259 }
260
261 File outputDirectory = new File(getOutputDirectory());
262
263 String filename = getOutputName() + ".html";
264
265 Locale locale = SiteTool.DEFAULT_LOCALE;
266
267 try {
268 String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
269 DocumentRenderingContext docRenderingContext =
270 new DocumentRenderingContext(outputDirectory, filename, reportMojoInfo);
271
272 SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
273
274 generate(sink, null, locale);
275 } catch (MavenReportException | RuntimeException e) {
276 failOnError("An error has occurred in " + getName(Locale.ENGLISH) + " report generation", e);
277 }
278 }
279
280
281
282
283
284
285
286 private ResourceBundle getBundle(Locale locale) {
287 return ResourceBundle.getBundle("javadoc-report", locale, getClass().getClassLoader());
288 }
289 }