1 package org.apache.maven.plugin.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.artifact.resolver.ArtifactResolutionResult;
24 import org.apache.maven.plugin.javadoc.resolver.SourceResolverConfig;
25 import org.apache.maven.plugins.annotations.Execute;
26 import org.apache.maven.plugins.annotations.LifecyclePhase;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.apache.maven.plugins.annotations.ResolutionScope;
30 import org.apache.maven.project.MavenProject;
31 import org.apache.maven.reporting.MavenReportException;
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<String> getProjectBuildOutputDirs( MavenProject p )
243 {
244 List<String> dirs = new ArrayList<String>();
245 if ( StringUtils.isNotEmpty( p.getBuild().getOutputDirectory() ) )
246 {
247 dirs.add( p.getBuild().getOutputDirectory() );
248 }
249 if ( StringUtils.isNotEmpty( p.getBuild().getTestOutputDirectory() ) )
250 {
251 dirs.add( 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<String>( 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<String>( p.getExecutionProject().getTestCompileSourceRoots() ) );
279 }
280
281 @Override
282 protected List<Artifact> getProjectArtifacts( MavenProject p )
283 {
284 return ( p.getTestArtifacts() == null ? Collections.<Artifact>emptyList()
285 : new LinkedList<Artifact>( p.getTestArtifacts() ) );
286 }
287
288 @Override
289 protected File getJavadocDirectory()
290 {
291 return testJavadocDirectory;
292 }
293
294 @Override
295 protected String getDoctitle()
296 {
297 return testDoctitle;
298 }
299
300 @Override
301 protected File getOverview()
302 {
303 return testOverview;
304 }
305
306 @Override
307 protected String getWindowtitle()
308 {
309 return testWindowtitle;
310 }
311
312 @Override
313 protected List<Artifact> getCompileArtifacts( ArtifactResolutionResult result )
314 {
315 return JavadocUtil.getCompileArtifacts( result.getArtifacts(), true );
316 }
317
318
319
320
321
322
323
324 private ResourceBundle getBundle( Locale locale )
325 {
326 return ResourceBundle.getBundle( "test-javadoc-report", locale, getClass().getClassLoader() );
327 }
328
329
330
331
332 private void addMainJavadocLink()
333 {
334 if ( links == null )
335 {
336 links = new ArrayList<String>();
337 }
338
339
340 File apidocs = new File( getReportOutputDirectory().getParentFile(), "apidocs" );
341 if ( apidocs.isDirectory() && !links.contains( "../apidocs" ) )
342 {
343 links.add( "../apidocs" );
344 }
345 }
346
347
348
349
350
351
352 @Override
353 protected SourceResolverConfig configureDependencySourceResolution( final SourceResolverConfig config )
354 {
355 return super.configureDependencySourceResolution( config ).withoutCompileSources().withTestSources();
356 }
357
358 @Override
359 protected boolean isTest()
360 {
361 return true;
362 }
363 }