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.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.ResourceBundle;
29
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.plugins.annotations.Execute;
32 import org.apache.maven.plugins.annotations.LifecyclePhase;
33 import org.apache.maven.plugins.annotations.Mojo;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.plugins.annotations.ResolutionScope;
36 import org.apache.maven.plugins.javadoc.resolver.SourceResolverConfig;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.reporting.MavenReportException;
39 import org.codehaus.plexus.util.StringUtils;
40 import org.eclipse.aether.util.filter.ScopeDependencyFilter;
41
42
43
44
45
46
47
48
49
50 @Mojo(name = "test-javadoc", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
51 @Execute(phase = LifecyclePhase.GENERATE_TEST_SOURCES)
52 public class TestJavadocReport extends JavadocReport {
53
54
55
56
57
58
59
60
61
62 @Parameter(
63 property = "testDoctitle",
64 alias = "doctitle",
65 defaultValue = "${project.name} ${project.version} Test API")
66 private String testDoctitle;
67
68
69
70
71
72
73
74 @Parameter(
75 property = "testOverview",
76 alias = "overview",
77 defaultValue = "${basedir}/src/test/javadoc/overview.html")
78 private File testOverview;
79
80
81
82
83
84
85 @Parameter(
86 property = "testWindowtitle",
87 alias = "windowtitle",
88 defaultValue = "${project.name} ${project.version} Test API")
89 private String testWindowtitle;
90
91
92
93
94
95
96
97
98 @Parameter(
99 property = "reportTestOutputDirectory",
100 defaultValue = "${project.reporting.outputDirectory}/testapidocs",
101 required = true)
102 private File reportOutputDirectory;
103
104
105
106
107
108 @Parameter(property = "destDir", defaultValue = "testapidocs")
109 private String destDir;
110
111
112
113
114
115
116
117
118
119
120 @Parameter(alias = "javadocDirectory", defaultValue = "${basedir}/src/test/javadoc")
121 private File testJavadocDirectory;
122
123
124
125
126
127
128
129
130
131
132
133 @Parameter(property = "testName", alias = "name")
134 private String testName;
135
136
137
138
139
140
141
142 @Parameter(property = "testDescription", alias = "description")
143 private String testDescription;
144
145
146
147
148
149 @Override
150 protected void executeReport(Locale unusedLocale) throws MavenReportException {
151 addMainJavadocLink();
152
153 super.executeReport(unusedLocale);
154 }
155
156 @Override
157 public String getName(Locale locale) {
158 if (testName == null || testName.isEmpty()) {
159 return getBundle(locale).getString("report.test-javadoc.name");
160 }
161
162 return testName;
163 }
164
165 @Override
166 public String getDescription(Locale locale) {
167 if (testDescription == null || testDescription.isEmpty()) {
168 return getBundle(locale).getString("report.test-javadoc.description");
169 }
170
171 return testDescription;
172 }
173
174 @Override
175 public String getOutputName() {
176 return destDir + "/index";
177 }
178
179 @Override
180 public File getReportOutputDirectory() {
181 if (reportOutputDirectory == null) {
182 return outputDirectory;
183 }
184
185 return reportOutputDirectory;
186 }
187
188
189
190
191
192
193 @Override
194 public void setReportOutputDirectory(File reportOutputDirectory) {
195 updateReportOutputDirectory(reportOutputDirectory, destDir);
196 }
197
198 @Override
199 public void setDestDir(String destDir) {
200 this.destDir = destDir;
201 updateReportOutputDirectory(reportOutputDirectory, destDir);
202 }
203
204 private void updateReportOutputDirectory(File reportOutputDirectory, String destDir) {
205 if (reportOutputDirectory != null
206 && destDir != null
207 && !reportOutputDirectory.getAbsolutePath().endsWith(destDir)) {
208 this.reportOutputDirectory = new File(reportOutputDirectory, destDir);
209 } else {
210 this.reportOutputDirectory = reportOutputDirectory;
211 }
212 }
213
214
215
216
217
218
219 @Override
220 protected List<File> getProjectBuildOutputDirs(MavenProject p) {
221 List<File> dirs = new ArrayList<>();
222 if (StringUtils.isNotEmpty(p.getBuild().getOutputDirectory())) {
223 dirs.add(new File(p.getBuild().getOutputDirectory()));
224 }
225 if (StringUtils.isNotEmpty(p.getBuild().getTestOutputDirectory())) {
226 dirs.add(new File(p.getBuild().getTestOutputDirectory()));
227 }
228
229 return dirs;
230 }
231
232 @Override
233 protected List<String> getProjectSourceRoots(MavenProject p) {
234 if ("pom".equals(p.getPackaging().toLowerCase())) {
235 return Collections.emptyList();
236 }
237
238 return p.getTestCompileSourceRoots() == null
239 ? Collections.<String>emptyList()
240 : new LinkedList<>(p.getTestCompileSourceRoots());
241 }
242
243 @Override
244 protected List<String> getExecutionProjectSourceRoots(MavenProject p) {
245 if ("pom".equals(p.getExecutionProject().getPackaging().toLowerCase())) {
246 return Collections.emptyList();
247 }
248
249 return p.getExecutionProject().getTestCompileSourceRoots() == null
250 ? Collections.<String>emptyList()
251 : new LinkedList<>(p.getExecutionProject().getTestCompileSourceRoots());
252 }
253
254 @Override
255 protected File getJavadocDirectory() {
256 return testJavadocDirectory;
257 }
258
259 @Override
260 protected String getDoctitle() {
261 return testDoctitle;
262 }
263
264 @Override
265 protected File getOverview() {
266 return testOverview;
267 }
268
269 @Override
270 protected String getWindowtitle() {
271 return testWindowtitle;
272 }
273
274 @Override
275 protected ScopeDependencyFilter getDependencyScopeFilter() {
276 return new ScopeDependencyFilter(
277 Arrays.asList(
278 Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_TEST),
279 null);
280 }
281
282
283
284
285
286
287
288 private ResourceBundle getBundle(Locale locale) {
289 return ResourceBundle.getBundle(
290 "test-javadoc-report", locale, getClass().getClassLoader());
291 }
292
293
294
295
296 private void addMainJavadocLink() {
297 if (links == null) {
298 links = new ArrayList<>();
299 }
300
301
302 File apidocs = new File(getReportOutputDirectory().getParentFile(), "apidocs");
303 if (apidocs.isDirectory() && !links.contains("../apidocs")) {
304 links.add("../apidocs");
305 }
306 }
307
308
309
310
311
312
313 @Override
314 protected SourceResolverConfig configureDependencySourceResolution(final SourceResolverConfig config) {
315 return super.configureDependencySourceResolution(config)
316 .withoutCompileSources()
317 .withTestSources();
318 }
319
320 @Override
321 protected boolean isTest() {
322 return true;
323 }
324 }