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 javax.inject.Inject;
22
23 import java.io.File;
24 import java.nio.file.Path;
25 import java.util.Collection;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.ResourceBundle;
29 import java.util.stream.Collectors;
30
31 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
32 import org.apache.maven.doxia.sink.Sink;
33 import org.apache.maven.doxia.sink.SinkFactory;
34 import org.apache.maven.doxia.siterenderer.DocumentRenderingContext;
35 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
36 import org.apache.maven.doxia.tools.SiteTool;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.plugin.MojoFailureException;
39 import org.apache.maven.plugins.annotations.Execute;
40 import org.apache.maven.plugins.annotations.LifecyclePhase;
41 import org.apache.maven.plugins.annotations.Mojo;
42 import org.apache.maven.plugins.annotations.Parameter;
43 import org.apache.maven.plugins.annotations.ResolutionScope;
44 import org.apache.maven.plugins.javadoc.resolver.ResourceResolver;
45 import org.apache.maven.project.ProjectBuilder;
46 import org.apache.maven.reporting.MavenMultiPageReport;
47 import org.apache.maven.reporting.MavenReportException;
48 import org.apache.maven.toolchain.ToolchainManager;
49 import org.codehaus.plexus.archiver.manager.ArchiverManager;
50 import org.eclipse.aether.RepositorySystem;
51
52
53
54
55
56
57
58
59
60
61 @Mojo(name = "javadoc", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
62 @Execute(phase = LifecyclePhase.GENERATE_SOURCES)
63 public class JavadocReport extends AbstractJavadocMojo implements MavenMultiPageReport {
64
65
66
67
68
69 private File reportOutputDirectory;
70
71
72
73
74
75
76
77 @Parameter(property = "name")
78 private String name;
79
80
81
82
83
84
85
86 @Parameter(property = "description")
87 private String description;
88
89 @Inject
90 public JavadocReport(
91 SiteTool siteTool,
92 ArchiverManager archiverManager,
93 ResourceResolver resourceResolver,
94 RepositorySystem repoSystem,
95 ArtifactHandlerManager artifactHandlerManager,
96 ProjectBuilder mavenProjectBuilder,
97 ToolchainManager toolchainManager) {
98 super(
99 siteTool,
100 archiverManager,
101 resourceResolver,
102 repoSystem,
103 artifactHandlerManager,
104 mavenProjectBuilder,
105 toolchainManager);
106 }
107
108
109
110
111
112
113 @Override
114 public String getName(Locale locale) {
115 if (name == null || name.isEmpty()) {
116 return getBundle(locale).getString("report.javadoc.name");
117 }
118
119 return name;
120 }
121
122
123 @Override
124 public String getDescription(Locale locale) {
125 if (description == null || description.isEmpty()) {
126 return getBundle(locale).getString("report.javadoc.description");
127 }
128
129 return description;
130 }
131
132
133 @Override
134 public void generate(Sink sink, Locale locale) throws MavenReportException {
135 generate(sink, null, locale);
136 }
137
138
139 @Override
140 public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException {
141 try {
142 executeReport(locale);
143 } catch (MavenReportException | RuntimeException e) {
144 if (failOnError) {
145 throw e;
146 }
147 getLog().error("Error while creating javadoc report: " + e.getMessage(), e);
148 }
149 }
150
151
152
153
154 @Override
155 @Deprecated
156 public String getOutputName() {
157 return getOutputPath();
158 }
159
160
161 @Override
162 public String getOutputPath() {
163 return (isTest() ? "test" : "") + "apidocs" + "/index";
164 }
165
166
167 @Override
168 public boolean isExternalReport() {
169 return true;
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
232
233
234
235
236 @Override
237 public boolean canGenerateReport() throws MavenReportException {
238 if (skip) {
239 return false;
240 }
241
242 Collection<JavadocModule> sourcePaths = getSourcePaths();
243
244 Collection<Path> collectedSourcePaths =
245 sourcePaths.stream().flatMap(e -> e.getSourcePaths().stream()).collect(Collectors.toList());
246
247 Map<Path, Collection<String>> files = getFiles(collectedSourcePaths);
248
249 return canGenerateReport(files);
250 }
251
252
253 @Override
254 public String getCategoryName() {
255 return CATEGORY_PROJECT_REPORTS;
256 }
257
258
259 @Override
260 public File getReportOutputDirectory() {
261 if (reportOutputDirectory == null) {
262 reportOutputDirectory = new File(getOutputDirectory());
263 }
264
265 return reportOutputDirectory;
266 }
267
268
269 @Override
270 public void setReportOutputDirectory(File reportOutputDirectory) {
271 this.reportOutputDirectory = reportOutputDirectory;
272 this.outputDirectory = reportOutputDirectory;
273 }
274
275
276 @Override
277 protected String getPluginReportOutputDirectory() {
278 return getReportOutputDirectory().getAbsolutePath() + "/" + (isTest() ? "test" : "") + "apidocs";
279 }
280
281
282 @Override
283 protected void doExecute() throws MojoExecutionException, MojoFailureException {
284 try {
285 if (!canGenerateReport()) {
286 String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
287 getLog().info("Skipping " + reportMojoInfo + " report goal");
288 return;
289 }
290 } catch (MavenReportException e) {
291 throw new MojoExecutionException("Failed to determine whether report can be generated", e);
292 }
293
294 File outputDirectory = new File(getOutputDirectory());
295
296 String filename = getOutputName() + ".html";
297
298 Locale locale = SiteTool.DEFAULT_LOCALE;
299
300 try {
301 String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
302 DocumentRenderingContext docRenderingContext =
303 new DocumentRenderingContext(outputDirectory, filename, reportMojoInfo);
304
305 SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
306
307 generate(sink, null, locale);
308 } catch (MavenReportException | RuntimeException e) {
309 failOnError("An error has occurred in " + getName(Locale.ENGLISH) + " report generation", e);
310 }
311 }
312
313
314
315
316
317
318
319 private ResourceBundle getBundle(Locale locale) {
320 return ResourceBundle.getBundle("javadoc-report", locale, getClass().getClassLoader());
321 }
322 }