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  
99  
100 
101 
102 
103 
104     @Parameter(alias = "javadocDirectory", defaultValue = "${basedir}/src/test/javadoc")
105     private File testJavadocDirectory;
106 
107     
108     
109     
110 
111     
112 
113 
114 
115 
116 
117     @Parameter(property = "testName", alias = "name")
118     private String testName;
119 
120     
121 
122 
123 
124 
125 
126     @Parameter(property = "testDescription", alias = "description")
127     private String testDescription;
128 
129     
130     
131     
132 
133     @Override
134     protected void executeReport(Locale unusedLocale) throws MavenReportException {
135         addMainJavadocLink();
136 
137         super.executeReport(unusedLocale);
138     }
139 
140     @Override
141     public String getName(Locale locale) {
142         if (testName == null || testName.isEmpty()) {
143             return getBundle(locale).getString("report.test-javadoc.name");
144         }
145 
146         return testName;
147     }
148 
149     @Override
150     public String getDescription(Locale locale) {
151         if (testDescription == null || testDescription.isEmpty()) {
152             return getBundle(locale).getString("report.test-javadoc.description");
153         }
154 
155         return testDescription;
156     }
157 
158     
159     
160     
161     
162 
163     @Override
164     protected List<File> getProjectBuildOutputDirs(MavenProject p) {
165         List<File> dirs = new ArrayList<>();
166         if (StringUtils.isNotEmpty(p.getBuild().getOutputDirectory())) {
167             dirs.add(new File(p.getBuild().getOutputDirectory()));
168         }
169         if (StringUtils.isNotEmpty(p.getBuild().getTestOutputDirectory())) {
170             dirs.add(new File(p.getBuild().getTestOutputDirectory()));
171         }
172 
173         return dirs;
174     }
175 
176     @Override
177     protected List<String> getProjectSourceRoots(MavenProject p) {
178         if ("pom".equals(p.getPackaging().toLowerCase())) {
179             return Collections.emptyList();
180         }
181 
182         return p.getTestCompileSourceRoots() == null
183                 ? Collections.<String>emptyList()
184                 : new LinkedList<>(p.getTestCompileSourceRoots());
185     }
186 
187     @Override
188     protected List<String> getExecutionProjectSourceRoots(MavenProject p) {
189         if ("pom".equals(p.getExecutionProject().getPackaging().toLowerCase())) {
190             return Collections.emptyList();
191         }
192 
193         return p.getExecutionProject().getTestCompileSourceRoots() == null
194                 ? Collections.<String>emptyList()
195                 : new LinkedList<>(p.getExecutionProject().getTestCompileSourceRoots());
196     }
197 
198     @Override
199     protected File getJavadocDirectory() {
200         return testJavadocDirectory;
201     }
202 
203     @Override
204     protected String getDoctitle() {
205         return testDoctitle;
206     }
207 
208     @Override
209     protected File getOverview() {
210         return testOverview;
211     }
212 
213     @Override
214     protected String getWindowtitle() {
215         return testWindowtitle;
216     }
217 
218     @Override
219     protected ScopeDependencyFilter getDependencyScopeFilter() {
220         return new ScopeDependencyFilter(
221                 Arrays.asList(
222                         Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_TEST),
223                 null);
224     }
225 
226     
227 
228 
229 
230 
231 
232     private ResourceBundle getBundle(Locale locale) {
233         return ResourceBundle.getBundle(
234                 "test-javadoc-report", locale, getClass().getClassLoader());
235     }
236 
237     
238 
239 
240     private void addMainJavadocLink() {
241         if (links == null) {
242             links = new ArrayList<>();
243         }
244 
245         File apidocs = new File(getReportOutputDirectory(), "apidocs");
246         if (apidocs.isDirectory() && !links.contains("../apidocs")) {
247             links.add("../apidocs");
248         }
249     }
250 
251     
252 
253 
254 
255 
256     @Override
257     protected SourceResolverConfig configureDependencySourceResolution(final SourceResolverConfig config) {
258         return super.configureDependencySourceResolution(config)
259                 .withoutCompileSources()
260                 .withTestSources();
261     }
262 
263     @Override
264     protected boolean isTest() {
265         return true;
266     }
267 }