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 Test Javadoc resources directory to be included in the Javadoc (i.e. package.html, images...).
97       * <br/>
98       * Could be used in addition of <code>docfilessubdirs</code> parameter.
99       * <br/>
100      * See <a href="#docfilessubdirs">docfilessubdirs</a>.
101      *
102      * @since 2.5
103      */
104     @Parameter(alias = "javadocDirectory", defaultValue = "${basedir}/src/test/javadoc")
105     private File testJavadocDirectory;
106 
107     // ----------------------------------------------------------------------
108     // Report Mojo Parameters
109     // ----------------------------------------------------------------------
110 
111     /**
112      * The name of the Test Javadoc report to be displayed in the Maven Generated Reports page
113      * (i.e. <code>project-reports.html</code>).
114      *
115      * @since 2.5
116      */
117     @Parameter(property = "testName", alias = "name")
118     private String testName;
119 
120     /**
121      * The description of the Test Javadoc report to be displayed in the Maven Generated Reports page
122      * (i.e. <code>project-reports.html</code>).
123      *
124      * @since 2.5
125      */
126     @Parameter(property = "testDescription", alias = "description")
127     private String testDescription;
128 
129     // ----------------------------------------------------------------------
130     // Report public methods
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     // Protected methods
160     // Important Note: should be inline with methods defined in TestJavadocJar
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      * Gets the resource bundle for the specified locale.
228      *
229      * @param locale The locale of the currently generated report.
230      * @return The resource bundle for the requested locale.
231      */
232     private ResourceBundle getBundle(Locale locale) {
233         return ResourceBundle.getBundle(
234                 "test-javadoc-report", locale, getClass().getClassLoader());
235     }
236 
237     /**
238      * Add the <code>../apidocs</code> to the links parameter so Test report could be linked to the Main report.
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      * Overridden to enable the resolution of -test-sources jar files.
253      *
254      * {@inheritDoc}
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 }