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