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.Execute;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.plugins.annotations.ResolutionScope;
28  import org.apache.maven.plugins.javadoc.resolver.SourceResolverConfig;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.reporting.MavenReportException;
31  import org.apache.maven.shared.artifact.filter.resolve.ScopeFilter;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  import java.io.File;
35  import java.util.ArrayList;
36  import java.util.Collections;
37  import java.util.LinkedList;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.ResourceBundle;
41  
42  /**
43   * Generates documentation for the <code>Java Test code</code> in an <b>NON aggregator</b> project using the standard
44   * <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/">Javadoc Tool</a>.
45   *
46   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
47   * @since 2.3
48   * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/">Javadoc Tool</a>
49   * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#options">Javadoc Options </a>
50   */
51  @Mojo( name = "test-javadoc", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
52  @Execute( phase = LifecyclePhase.GENERATE_TEST_SOURCES )
53  public class TestJavadocReport
54      extends JavadocReport
55  {
56      // ----------------------------------------------------------------------
57      // Javadoc Options (should be inline with options defined in TestJavadocJar)
58      // ----------------------------------------------------------------------
59  
60      /**
61       * Specifies the Test title to be placed near the top of the overview summary file.
62       * <br/>
63       * See <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#doctitle">doctitle</a>.
64       * <br/>
65       *
66       * @since 2.5
67       */
68      @Parameter( property = "testDoctitle", alias = "doctitle",
69                  defaultValue = "${project.name} ${project.version} Test API" )
70      private String testDoctitle;
71  
72      /**
73       * Specifies that Javadoc should retrieve the text for the Test overview documentation from the "source" file
74       * specified by path/filename and place it on the Overview page (overview-summary.html).
75       * <br/>
76       * <b>Note</b>: could be in conflict with &lt;nooverview/&gt;.
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 TestJavadocJar)
102     // ----------------------------------------------------------------------
103 
104     /**
105      * Specifies the destination directory where test Javadoc saves the generated HTML files.
106      */
107     @Parameter( property = "reportTestOutputDirectory",
108                 defaultValue = "${project.reporting.outputDirectory}/testapidocs", required = true )
109     private File reportOutputDirectory;
110 
111     /**
112      * The name of the destination directory.
113      * <br/>
114      */
115     @Parameter( property = "destDir", defaultValue = "testapidocs" )
116     private String destDir;
117 
118     /**
119      * Specifies the Test Javadoc resources directory to be included in the Javadoc (i.e. package.html, images...).
120      * <br/>
121      * Could be used in addition of <code>docfilessubdirs</code> parameter.
122      * <br/>
123      * See <a href="#docfilessubdirs">docfilessubdirs</a>.
124      *
125      * @since 2.5
126      */
127     @Parameter( alias = "javadocDirectory", defaultValue = "${basedir}/src/test/javadoc" )
128     private File testJavadocDirectory;
129 
130     // ----------------------------------------------------------------------
131     // Report Mojo Parameters
132     // ----------------------------------------------------------------------
133 
134     /**
135      * The name of the Test Javadoc report to be displayed in the Maven Generated Reports page
136      * (i.e. <code>project-reports.html</code>).
137      *
138      * @since 2.5
139      */
140     @Parameter( property = "testName", alias = "name" )
141     private String testName;
142 
143     /**
144      * The description of the Test Javadoc report to be displayed in the Maven Generated Reports page
145      * (i.e. <code>project-reports.html</code>).
146      *
147      * @since 2.5
148      */
149     @Parameter( property = "testDescription", alias = "description" )
150     private String testDescription;
151 
152     // ----------------------------------------------------------------------
153     // Report public methods
154     // ----------------------------------------------------------------------
155 
156     @Override
157     protected void executeReport( Locale unusedLocale )
158         throws MavenReportException
159     {
160         addMainJavadocLink();
161 
162         super.executeReport( unusedLocale );
163     }
164 
165     @Override
166     public String getName( Locale locale )
167     {
168         if ( StringUtils.isEmpty( testName ) )
169         {
170             return getBundle( locale ).getString( "report.test-javadoc.name" );
171         }
172 
173         return testName;
174     }
175 
176     @Override
177     public String getDescription( Locale locale )
178     {
179         if ( StringUtils.isEmpty( testDescription ) )
180         {
181             return getBundle( locale ).getString( "report.test-javadoc.description" );
182         }
183 
184         return testDescription;
185     }
186 
187     @Override
188     public String getOutputName()
189     {
190         return destDir + "/index";
191     }
192 
193     @Override
194     public File getReportOutputDirectory()
195     {
196         if ( reportOutputDirectory == null )
197         {
198             return outputDirectory;
199         }
200 
201         return reportOutputDirectory;
202     }
203 
204     /**
205      * Method to set the directory where the generated reports will be put
206      *
207      * @param reportOutputDirectory the directory file to be set
208      */
209     @Override
210     public void setReportOutputDirectory( File reportOutputDirectory )
211     {
212         updateReportOutputDirectory( reportOutputDirectory, destDir );
213     }
214 
215     @Override
216     public void setDestDir( String destDir )
217     {
218         this.destDir = destDir;
219         updateReportOutputDirectory( reportOutputDirectory, destDir );
220     }
221 
222     private void updateReportOutputDirectory( File reportOutputDirectory, String destDir )
223     {
224         if ( reportOutputDirectory != null && destDir != null
225              && !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) )
226         {
227             this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
228         }
229         else
230         {
231             this.reportOutputDirectory = reportOutputDirectory;
232         }
233     }
234 
235     // ----------------------------------------------------------------------
236     // Protected methods
237     // Important Note: should be inline with methods defined in TestJavadocJar
238     // ----------------------------------------------------------------------
239 
240     @Override
241     protected List<File> getProjectBuildOutputDirs( MavenProject p )
242     {
243         List<File> dirs = new ArrayList<>();
244         if ( StringUtils.isNotEmpty( p.getBuild().getOutputDirectory() ) )
245         {
246             dirs.add( new File( p.getBuild().getOutputDirectory() ) );
247         }
248         if ( StringUtils.isNotEmpty( p.getBuild().getTestOutputDirectory() ) )
249         {
250             dirs.add( new File( p.getBuild().getTestOutputDirectory() ) );
251         }
252 
253         return dirs;
254     }
255 
256     @Override
257     protected List<String> getProjectSourceRoots( MavenProject p )
258     {
259         if ( "pom".equals( p.getPackaging().toLowerCase() ) )
260         {
261             return Collections.emptyList();
262         }
263 
264         return ( p.getTestCompileSourceRoots() == null ? Collections.<String>emptyList()
265                         : new LinkedList<>( p.getTestCompileSourceRoots() ) );
266     }
267 
268     @Override
269     protected List<String> getExecutionProjectSourceRoots( MavenProject p )
270     {
271         if ( "pom".equals( p.getExecutionProject().getPackaging().toLowerCase() ) )
272         {
273             return Collections.emptyList();
274         }
275 
276         return ( p.getExecutionProject().getTestCompileSourceRoots() == null ? Collections.<String>emptyList()
277                         : new LinkedList<>( p.getExecutionProject().getTestCompileSourceRoots() ) );
278     }
279 
280     @Override
281     protected File getJavadocDirectory()
282     {
283         return testJavadocDirectory;
284     }
285 
286     @Override
287     protected String getDoctitle()
288     {
289         return testDoctitle;
290     }
291 
292     @Override
293     protected File getOverview()
294     {
295         return testOverview;
296     }
297 
298     @Override
299     protected String getWindowtitle()
300     {
301         return testWindowtitle;
302     }
303 
304     @Override
305     protected ScopeFilter getDependencyScopeFilter()
306     {
307         return ScopeFilter.including( Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_SYSTEM,
308                                       Artifact.SCOPE_TEST );
309     }
310 
311     /**
312      * Gets the resource bundle for the specified locale.
313      *
314      * @param locale The locale of the currently generated report.
315      * @return The resource bundle for the requested locale.
316      */
317     private ResourceBundle getBundle( Locale locale )
318     {
319         return ResourceBundle.getBundle( "test-javadoc-report", locale, getClass().getClassLoader() );
320     }
321 
322     /**
323      * Add the <code>../apidocs</code> to the links parameter so Test report could be linked to the Main report.
324      */
325     private void addMainJavadocLink()
326     {
327         if ( links == null )
328         {
329             links = new ArrayList<>();
330         }
331 
332         // TODO the prerequisite is that the main report is in apidocs
333         File apidocs = new File( getReportOutputDirectory().getParentFile(), "apidocs" );
334         if ( apidocs.isDirectory() && !links.contains( "../apidocs" ) )
335         {
336             links.add( "../apidocs" );
337         }
338     }
339     
340     /**
341      * Overridden to enable the resolution of -test-sources jar files.
342      * 
343      * {@inheritDoc}
344      */
345     @Override
346     protected SourceResolverConfigourceResolverConfig">SourceResolverConfig configureDependencySourceResolution( final SourceResolverConfig config )
347     {
348         return super.configureDependencySourceResolution( config ).withoutCompileSources().withTestSources();
349     }
350 
351     @Override
352     protected boolean isTest()
353     {
354         return true;
355     }
356 }