View Javadoc
1   package org.apache.maven.plugins.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.plugins.annotations.LifecyclePhase;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.Parameter;
26  import org.apache.maven.plugins.annotations.ResolutionScope;
27  import org.apache.maven.plugins.javadoc.resolver.SourceResolverConfig;
28  import org.apache.maven.project.MavenProject;
29  import org.apache.maven.shared.artifact.filter.resolve.ScopeFilter;
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   * @since 2.5
43   */
44  @Mojo( name = "test-jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.TEST,
45                  threadSafe = true )
46  public class TestJavadocJar
47      extends JavadocJar
48  {
49      // ----------------------------------------------------------------------
50      // Javadoc Options (should be inline with Javadoc options defined in TestJavadocReport)
51      // ----------------------------------------------------------------------
52  
53      /**
54       * Specifies the destination directory where Javadoc saves the generated HTML files.
55       * <br/>
56       * See <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#d">d</a>.
57       * <br/>
58       */
59      @Parameter( defaultValue = "${project.build.directory}/testapidocs", required = true )
60      private File outputDirectory;
61  
62      /**
63       * Specifies the Test title to be placed near the top of the overview summary file.
64       * <br/>
65       * See <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#doctitle">doctitle</a>.
66       * <br/>
67       *
68       * @since 2.5
69       */
70      @Parameter( property = "testDoctitle", alias = "doctitle",
71                  defaultValue = "${project.name} ${project.version} Test API" )
72      private String testDoctitle;
73  
74      /**
75       * Specifies that Javadoc should retrieve the text for the Test overview documentation from the "source" file
76       * specified by path/filename and place it on the Overview page (overview-summary.html).
77       * <br/>
78       * See <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#overview">overview</a>.
79       * <br/>
80       *
81       * @since 2.5
82       */
83      @Parameter( property = "testOverview", alias = "overview",
84                  defaultValue = "${basedir}/src/test/javadoc/overview.html" )
85      private File testOverview;
86  
87      /**
88       * Specifies the Test title to be placed in the HTML title tag.
89       * <br/>
90       * See
91       * <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#windowtitle">windowtitle</a>.
92       * <br/>
93       *
94       * @since 2.5
95       */
96      @Parameter( property = "testWindowtitle", alias = "windowtitle",
97                  defaultValue = "${project.name} ${project.version} Test API" )
98      private String testWindowtitle;
99  
100     // ----------------------------------------------------------------------
101     // Mojo Parameters (should be inline with options defined in TestJavadocReport)
102     // ----------------------------------------------------------------------
103 
104     /**
105      * Specifies the Test Javadoc resources directory to be included in the Javadoc (i.e. package.html, images...).
106      *
107      * @since 2.5
108      */
109     @Parameter( alias = "javadocDirectory", defaultValue = "${basedir}/src/test/javadoc" )
110     private File testJavadocDirectory;
111 
112     /**
113      * @since 2.10
114      */
115     @Parameter( property = "maven.javadoc.testClassifier", defaultValue = "test-javadoc", required = true )
116     private String testClassifier;
117 
118     // ----------------------------------------------------------------------
119     // Protected methods
120     // ----------------------------------------------------------------------
121 
122     @Override
123     protected String getClassifier()
124     {
125         return testClassifier;
126     }
127 
128     // ----------------------------------------------------------------------
129     // Important Note: should be inline with methods defined in TestJavadocReport
130     // ----------------------------------------------------------------------
131 
132     @Override
133     protected String getOutputDirectory()
134     {
135         return outputDirectory.getAbsoluteFile().toString();
136     }
137 
138     @Override
139     protected File getJavadocDirectory()
140     {
141         return testJavadocDirectory;
142     }
143 
144     @Override
145     protected String getDoctitle()
146     {
147         return testDoctitle;
148     }
149 
150     @Override
151     protected File getOverview()
152     {
153         return testOverview;
154     }
155 
156     @Override
157     protected String getWindowtitle()
158     {
159         return testWindowtitle;
160     }
161 
162     @Override
163     protected List<File> getProjectBuildOutputDirs( MavenProject p )
164     {
165         List<File> dirs = new ArrayList<>();
166         if ( StringUtils.isNotEmpty( p.getBuild().getOutputDirectory() ) )
167         {
168             dirs.add( new File( p.getBuild().getOutputDirectory() ) );
169         }
170         if ( StringUtils.isNotEmpty( p.getBuild().getTestOutputDirectory() ) )
171         {
172             dirs.add( new File( p.getBuild().getTestOutputDirectory() ) );
173         }
174 
175         return dirs;
176     }
177 
178     @Override
179     protected List<String> getProjectSourceRoots( MavenProject p )
180     {
181         if ( "pom".equals( p.getPackaging().toLowerCase() ) )
182         {
183             return Collections.emptyList();
184         }
185 
186         return p.getTestCompileSourceRoots();
187     }
188 
189     @Override
190     protected List<String> getExecutionProjectSourceRoots( MavenProject p )
191     {
192         if ( "pom".equals( p.getExecutionProject().getPackaging().toLowerCase() ) )
193         {
194             return Collections.emptyList();
195         }
196 
197         return p.getExecutionProject().getTestCompileSourceRoots();
198     }
199 
200     @Override
201     protected ScopeFilter getDependencyScopeFilter()
202     {
203         return ScopeFilter.including( Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_SYSTEM,
204                                       Artifact.SCOPE_TEST );
205     }
206     
207     /**
208      * Overriden to enable the resolution of -test-sources jar files.
209      * 
210      * {@inheritDoc}
211      */
212     @Override
213     protected SourceResolverConfigourceResolverConfig">SourceResolverConfig configureDependencySourceResolution( final SourceResolverConfig config )
214     {
215         return super.configureDependencySourceResolution( config ).withoutCompileSources().withTestSources();
216     }
217 
218     @Override
219     protected boolean isTest()
220     {
221         return true;
222     }
223 }