View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Generates documentation for the <code>Java Test code</code> in an <b>NON aggregator</b> project using the standard
44   * <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html">Javadoc Tool</a>.
45   *
46   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
47   * @since 2.3
48   * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html">Javadoc Tool</a>
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      // Javadoc Options (should be inline with options defined in TestJavadocJar)
55      // ----------------------------------------------------------------------
56  
57      /**
58       * Specifies the Test title to be placed near the top of the overview summary file.
59       * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html#standard-doclet-options">Doclet option doctitle</a>.
60       * @since 2.5
61       */
62      @Parameter(
63              property = "testDoctitle",
64              alias = "doctitle",
65              defaultValue = "${project.name} ${project.version} Test API")
66      private String testDoctitle;
67  
68      /**
69       * Specifies that Javadoc should retrieve the text for the Test overview documentation from the "source" file
70       * specified by path/filename and place it on the Overview page (overview-summary.html).
71       * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html#standard-doclet-options">Doclet option overview</a>.
72       * @since 2.5
73       */
74      @Parameter(
75              property = "testOverview",
76              alias = "overview",
77              defaultValue = "${basedir}/src/test/javadoc/overview.html")
78      private File testOverview;
79  
80      /**
81       * Specifies the Test title to be placed in the HTML title tag.
82       * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html#standard-doclet-options">Doclet option windowtitle</a>.
83       * @since 2.5
84       */
85      @Parameter(
86              property = "testWindowtitle",
87              alias = "windowtitle",
88              defaultValue = "${project.name} ${project.version} Test API")
89      private String testWindowtitle;
90  
91      // ----------------------------------------------------------------------
92      // Mojo Parameters (should be inline with options defined in TestJavadocJar)
93      // ----------------------------------------------------------------------
94  
95      /**
96       * Specifies the destination directory where test Javadoc saves the generated HTML files.
97       */
98      @Parameter(
99              property = "reportTestOutputDirectory",
100             defaultValue = "${project.reporting.outputDirectory}/testapidocs",
101             required = true)
102     private File reportOutputDirectory;
103 
104     /**
105      * The name of the destination directory.
106      * <br/>
107      */
108     @Parameter(property = "destDir", defaultValue = "testapidocs")
109     private String destDir;
110 
111     /**
112      * Specifies the Test Javadoc resources directory to be included in the Javadoc (i.e. package.html, images...).
113      * <br/>
114      * Could be used in addition of <code>docfilessubdirs</code> parameter.
115      * <br/>
116      * See <a href="#docfilessubdirs">docfilessubdirs</a>.
117      *
118      * @since 2.5
119      */
120     @Parameter(alias = "javadocDirectory", defaultValue = "${basedir}/src/test/javadoc")
121     private File testJavadocDirectory;
122 
123     // ----------------------------------------------------------------------
124     // Report Mojo Parameters
125     // ----------------------------------------------------------------------
126 
127     /**
128      * The name of the Test Javadoc report to be displayed in the Maven Generated Reports page
129      * (i.e. <code>project-reports.html</code>).
130      *
131      * @since 2.5
132      */
133     @Parameter(property = "testName", alias = "name")
134     private String testName;
135 
136     /**
137      * The description of the Test Javadoc report to be displayed in the Maven Generated Reports page
138      * (i.e. <code>project-reports.html</code>).
139      *
140      * @since 2.5
141      */
142     @Parameter(property = "testDescription", alias = "description")
143     private String testDescription;
144 
145     // ----------------------------------------------------------------------
146     // Report public methods
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      * Method to set the directory where the generated reports will be put
190      *
191      * @param reportOutputDirectory the directory file to be set
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     // Protected methods
216     // Important Note: should be inline with methods defined in TestJavadocJar
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      * Gets the resource bundle for the specified locale.
284      *
285      * @param locale The locale of the currently generated report.
286      * @return The resource bundle for the requested locale.
287      */
288     private ResourceBundle getBundle(Locale locale) {
289         return ResourceBundle.getBundle(
290                 "test-javadoc-report", locale, getClass().getClassLoader());
291     }
292 
293     /**
294      * Add the <code>../apidocs</code> to the links parameter so Test report could be linked to the Main report.
295      */
296     private void addMainJavadocLink() {
297         if (links == null) {
298             links = new ArrayList<>();
299         }
300 
301         // TODO the prerequisite is that the main report is in apidocs
302         File apidocs = new File(getReportOutputDirectory().getParentFile(), "apidocs");
303         if (apidocs.isDirectory() && !links.contains("../apidocs")) {
304             links.add("../apidocs");
305         }
306     }
307 
308     /**
309      * Overridden to enable the resolution of -test-sources jar files.
310      *
311      * {@inheritDoc}
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 }