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