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 javax.inject.Inject;
22  
23  import java.io.File;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
31  import org.apache.maven.doxia.tools.SiteTool;
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.ResourceResolver;
37  import org.apache.maven.plugins.javadoc.resolver.SourceResolverConfig;
38  import org.apache.maven.project.MavenProject;
39  import org.apache.maven.project.MavenProjectHelper;
40  import org.apache.maven.project.ProjectBuilder;
41  import org.apache.maven.toolchain.ToolchainManager;
42  import org.codehaus.plexus.archiver.jar.JarArchiver;
43  import org.codehaus.plexus.archiver.manager.ArchiverManager;
44  import org.codehaus.plexus.util.StringUtils;
45  import org.eclipse.aether.RepositorySystem;
46  import org.eclipse.aether.util.filter.ScopeDependencyFilter;
47  
48  /**
49   * Bundles the Javadoc documentation for <code>test Java code</code> in a <b>NON aggregator</b> project into
50   * a jar using the standard <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html">
51   * Javadoc Tool</a>.
52   *
53   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
54   * @since 2.5
55   */
56  @Mojo(
57          name = "test-jar",
58          defaultPhase = LifecyclePhase.PACKAGE,
59          requiresDependencyResolution = ResolutionScope.TEST,
60          threadSafe = true)
61  public class TestJavadocJarMojo extends JavadocJarMojo {
62  
63      // CHECKSTYLE_OFF: ParameterNumber
64      @Inject
65      public TestJavadocJarMojo(
66              MavenProjectHelper projectHelper,
67              JarArchiver jarArchiver,
68              SiteTool siteTool,
69              ArchiverManager archiverManager,
70              ResourceResolver resourceResolver,
71              RepositorySystem repoSystem,
72              ArtifactHandlerManager artifactHandlerManager,
73              ProjectBuilder mavenProjectBuilder,
74              ToolchainManager toolchainManager) {
75          super(
76                  projectHelper,
77                  jarArchiver,
78                  siteTool,
79                  archiverManager,
80                  resourceResolver,
81                  repoSystem,
82                  artifactHandlerManager,
83                  mavenProjectBuilder,
84                  toolchainManager);
85      }
86      // CHECKSTYLE_ON: ParameterNumber
87  
88      // ----------------------------------------------------------------------
89      // Javadoc Options (should be inline with Javadoc options defined in TestJavadocReport)
90      // ----------------------------------------------------------------------
91  
92      /**
93       * Specifies the Test title to be placed near the top of the overview summary file.
94       * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html#standard-doclet-options">Doclet option doctitle</a>.
95       * @since 2.5
96       */
97      @Parameter(
98              property = "testDoctitle",
99              alias = "doctitle",
100             defaultValue = "${project.name} ${project.version} Test API")
101     private String testDoctitle;
102 
103     /**
104      * Specifies that Javadoc should retrieve the text for the Test overview documentation from the "source" file
105      * specified by path/filename and place it on the Overview page (overview-summary.html).
106      * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html#standard-doclet-options">Doclet option overview</a>.
107      * @since 2.5
108      */
109     @Parameter(
110             property = "testOverview",
111             alias = "overview",
112             defaultValue = "${basedir}/src/test/javadoc/overview.html")
113     private File testOverview;
114 
115     /**
116      * Specifies the Test title to be placed in the HTML title tag.
117      * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html#standard-doclet-options">Doclet option windowtitle</a>.
118      * @since 2.5
119      */
120     @Parameter(
121             property = "testWindowtitle",
122             alias = "windowtitle",
123             defaultValue = "${project.name} ${project.version} Test API")
124     private String testWindowtitle;
125 
126     // ----------------------------------------------------------------------
127     // Mojo Parameters (should be inline with options defined in TestJavadocReport)
128     // ----------------------------------------------------------------------
129 
130     /**
131      * Specifies the Test Javadoc resources directory to be included in the Javadoc (i.e. package.html, images...).
132      *
133      * @since 2.5
134      */
135     @Parameter(alias = "javadocDirectory", defaultValue = "${basedir}/src/test/javadoc")
136     private File testJavadocDirectory;
137 
138     /**
139      * @since 2.10
140      */
141     @Parameter(property = "maven.javadoc.testClassifier", defaultValue = "test-javadoc", required = true)
142     private String testClassifier;
143 
144     // ----------------------------------------------------------------------
145     // Protected methods
146     // ----------------------------------------------------------------------
147 
148     @Override
149     protected String getClassifier() {
150         return testClassifier;
151     }
152 
153     // ----------------------------------------------------------------------
154     // Important Note: should be inline with methods defined in TestJavadocReport
155     // ----------------------------------------------------------------------
156 
157     @Override
158     protected File getJavadocDirectory() {
159         return testJavadocDirectory;
160     }
161 
162     @Override
163     protected String getDoctitle() {
164         return testDoctitle;
165     }
166 
167     @Override
168     protected File getOverview() {
169         return testOverview;
170     }
171 
172     @Override
173     protected String getWindowtitle() {
174         return testWindowtitle;
175     }
176 
177     @Override
178     protected List<File> getProjectBuildOutputDirs(MavenProject p) {
179         List<File> dirs = new ArrayList<>();
180         if (StringUtils.isNotEmpty(p.getBuild().getOutputDirectory())) {
181             dirs.add(new File(p.getBuild().getOutputDirectory()));
182         }
183         if (StringUtils.isNotEmpty(p.getBuild().getTestOutputDirectory())) {
184             dirs.add(new File(p.getBuild().getTestOutputDirectory()));
185         }
186 
187         return dirs;
188     }
189 
190     @Override
191     protected List<String> getProjectSourceRoots(MavenProject p) {
192         if ("pom".equals(p.getPackaging().toLowerCase())) {
193             return Collections.emptyList();
194         }
195 
196         return p.getTestCompileSourceRoots();
197     }
198 
199     @Override
200     protected List<String> getExecutionProjectSourceRoots(MavenProject p) {
201         if ("pom".equals(p.getExecutionProject().getPackaging().toLowerCase())) {
202             return Collections.emptyList();
203         }
204 
205         return p.getExecutionProject().getTestCompileSourceRoots();
206     }
207 
208     @Override
209     protected ScopeDependencyFilter getDependencyScopeFilter() {
210         return new ScopeDependencyFilter(
211                 Arrays.asList(
212                         Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_TEST),
213                 null);
214     }
215 
216     /**
217      * Overridden to enable the resolution of -test-sources jar files.
218      *
219      * {@inheritDoc}
220      */
221     @Override
222     protected SourceResolverConfig configureDependencySourceResolution(final SourceResolverConfig config) {
223         return super.configureDependencySourceResolution(config)
224                 .withoutCompileSources()
225                 .withTestSources();
226     }
227 
228     @Override
229     protected boolean isTest() {
230         return true;
231     }
232 }