View Javadoc
1   package org.apache.maven.plugin.javadoc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
24  import org.apache.maven.plugin.javadoc.resolver.SourceResolverConfig;
25  import org.apache.maven.plugins.annotations.LifecyclePhase;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.plugins.annotations.ResolutionScope;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.List;
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="http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/">Javadoc Tool</a>.
40   *
41   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
42   * @version $Id: TestJavadocJar.html 943732 2015-03-14 00:06:30Z khmarbaise $
43   * @since 2.5
44   */
45  @Mojo( name = "test-jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.TEST,
46                  threadSafe = true )
47  public class TestJavadocJar
48      extends JavadocJar
49  {
50      // ----------------------------------------------------------------------
51      // Javadoc Options (should be inline with Javadoc options defined in TestJavadocReport)
52      // ----------------------------------------------------------------------
53  
54      /**
55       * Specifies the destination directory where Javadoc saves the generated HTML files.
56       * <br/>
57       * See <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#d">d</a>.
58       * <br/>
59       */
60      @Parameter( defaultValue = "${project.build.directory}/testapidocs", required = true )
61      private File outputDirectory;
62  
63      /**
64       * Specifies the Test title to be placed near the top of the overview summary file.
65       * <br/>
66       * See <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#doctitle">doctitle</a>.
67       * <br/>
68       *
69       * @since 2.5
70       */
71      @Parameter( property = "testDoctitle", alias = "doctitle",
72                  defaultValue = "${project.name} ${project.version} Test API" )
73      private String testDoctitle;
74  
75      /**
76       * Specifies that Javadoc should retrieve the text for the Test overview documentation from the "source" file
77       * specified by path/filename and place it on the Overview page (overview-summary.html).
78       * <br/>
79       * See <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#overview">overview</a>.
80       * <br/>
81       *
82       * @since 2.5
83       */
84      @Parameter( property = "testOverview", alias = "overview",
85                  defaultValue = "${basedir}/src/test/javadoc/overview.html" )
86      private File testOverview;
87  
88      /**
89       * Specifies the Test title to be placed in the HTML title tag.
90       * <br/>
91       * See
92       * <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#windowtitle">windowtitle</a>.
93       * <br/>
94       *
95       * @since 2.5
96       */
97      @Parameter( property = "testWindowtitle", alias = "windowtitle",
98                  defaultValue = "${project.name} ${project.version} Test API" )
99      private String testWindowtitle;
100 
101     // ----------------------------------------------------------------------
102     // Mojo Parameters (should be inline with options defined in TestJavadocReport)
103     // ----------------------------------------------------------------------
104 
105     /**
106      * Specifies the Test Javadoc resources directory to be included in the Javadoc (i.e. package.html, images...).
107      *
108      * @since 2.5
109      */
110     @Parameter( alias = "javadocDirectory", defaultValue = "${basedir}/src/test/javadoc" )
111     private File testJavadocDirectory;
112 
113     /**
114      * @since 2.10
115      */
116     @Parameter( property = "maven.javadoc.testClassifier", defaultValue = "test-javadoc", required = true )
117     private String testClassifier;
118 
119     // ----------------------------------------------------------------------
120     // Protected methods
121     // ----------------------------------------------------------------------
122 
123     @Override
124     protected String getClassifier()
125     {
126         return testClassifier;
127     }
128 
129     // ----------------------------------------------------------------------
130     // Important Note: should be inline with methods defined in TestJavadocReport
131     // ----------------------------------------------------------------------
132 
133     @Override
134     protected String getOutputDirectory()
135     {
136         return outputDirectory.getAbsoluteFile().toString();
137     }
138 
139     @Override
140     protected File getJavadocDirectory()
141     {
142         return testJavadocDirectory;
143     }
144 
145     @Override
146     protected String getDoctitle()
147     {
148         return testDoctitle;
149     }
150 
151     @Override
152     protected File getOverview()
153     {
154         return testOverview;
155     }
156 
157     @Override
158     protected String getWindowtitle()
159     {
160         return testWindowtitle;
161     }
162 
163     @Override
164     protected List<String> getProjectBuildOutputDirs( MavenProject p )
165     {
166         List<String> dirs = new ArrayList<String>();
167         if ( StringUtils.isNotEmpty( p.getBuild().getOutputDirectory() ) )
168         {
169             dirs.add( p.getBuild().getOutputDirectory() );
170         }
171         if ( StringUtils.isNotEmpty( p.getBuild().getTestOutputDirectory() ) )
172         {
173             dirs.add( p.getBuild().getTestOutputDirectory() );
174         }
175 
176         return dirs;
177     }
178 
179     @Override
180     protected List<String> getProjectSourceRoots( MavenProject p )
181     {
182         if ( "pom".equals( p.getPackaging().toLowerCase() ) )
183         {
184             return Collections.emptyList();
185         }
186 
187         return p.getTestCompileSourceRoots();
188     }
189 
190     @Override
191     protected List<String> getExecutionProjectSourceRoots( MavenProject p )
192     {
193         if ( "pom".equals( p.getExecutionProject().getPackaging().toLowerCase() ) )
194         {
195             return Collections.emptyList();
196         }
197 
198         return p.getExecutionProject().getTestCompileSourceRoots();
199     }
200 
201     @Override
202     protected List<Artifact> getProjectArtifacts( MavenProject p )
203     {
204         return p.getTestArtifacts();
205     }
206 
207     @Override
208     protected List<Artifact> getCompileArtifacts( ArtifactResolutionResult result )
209     {
210         return JavadocUtil.getCompileArtifacts( result.getArtifacts(), true );
211     }
212     
213     /**
214      * Overriden to enable the resolution of -test-sources jar files.
215      * 
216      * {@inheritDoc}
217      */
218     @Override
219     protected SourceResolverConfig configureDependencySourceResolution( final SourceResolverConfig config )
220     {
221         return super.configureDependencySourceResolution( config ).withoutCompileSources().withTestSources();
222     }
223 }