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