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