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 @Override
153 public String getOutputName() {
154 return (isTest() ? "test" : "") + "apidocs" + "/index";
155 }
156
157
158 @Override
159 public boolean isExternalReport() {
160 return true;
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
226
227 @Override
228 public boolean canGenerateReport() throws MavenReportException {
229 if (skip) {
230 return false;
231 }
232
233 Collection<JavadocModule> sourcePaths = getSourcePaths();
234
235 Collection<Path> collectedSourcePaths =
236 sourcePaths.stream().flatMap(e -> e.getSourcePaths().stream()).collect(Collectors.toList());
237
238 Map<Path, Collection<String>> files = getFiles(collectedSourcePaths);
239
240 return canGenerateReport(files);
241 }
242
243
244 @Override
245 public String getCategoryName() {
246 return CATEGORY_PROJECT_REPORTS;
247 }
248
249
250 @Override
251 public File getReportOutputDirectory() {
252 if (reportOutputDirectory == null) {
253 reportOutputDirectory = new File(getOutputDirectory());
254 }
255
256 return reportOutputDirectory;
257 }
258
259
260 @Override
261 public void setReportOutputDirectory(File reportOutputDirectory) {
262 this.reportOutputDirectory = reportOutputDirectory;
263 this.outputDirectory = reportOutputDirectory;
264 }
265
266
267 @Override
268 protected String getPluginReportOutputDirectory() {
269 return getReportOutputDirectory().getAbsolutePath() + "/" + (isTest() ? "test" : "") + "apidocs";
270 }
271
272
273 @Override
274 protected void doExecute() throws MojoExecutionException, MojoFailureException {
275 try {
276 if (!canGenerateReport()) {
277 String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
278 getLog().info("Skipping " + reportMojoInfo + " report goal");
279 return;
280 }
281 } catch (MavenReportException e) {
282 throw new MojoExecutionException("Failed to determine whether report can be generated", e);
283 }
284
285 File outputDirectory = new File(getOutputDirectory());
286
287 String filename = getOutputName() + ".html";
288
289 Locale locale = SiteTool.DEFAULT_LOCALE;
290
291 try {
292 String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
293 DocumentRenderingContext docRenderingContext =
294 new DocumentRenderingContext(outputDirectory, filename, reportMojoInfo);
295
296 SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
297
298 generate(sink, null, locale);
299 } catch (MavenReportException | RuntimeException e) {
300 failOnError("An error has occurred in " + getName(Locale.ENGLISH) + " report generation", e);
301 }
302 }
303
304
305
306
307
308
309
310 private ResourceBundle getBundle(Locale locale) {
311 return ResourceBundle.getBundle("javadoc-report", locale, getClass().getClassLoader());
312 }
313 }