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 java.io.File;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.ResourceBundle;
27  
28  import org.apache.maven.doxia.siterenderer.RenderingContext;
29  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.MojoFailureException;
32  import org.apache.maven.plugins.annotations.Execute;
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.reporting.MavenReport;
38  import org.apache.maven.reporting.MavenReportException;
39  import org.codehaus.doxia.sink.Sink;
40  import org.codehaus.plexus.util.StringUtils;
41  
42  /**
43   * Generates documentation for the <code>Java 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:evenisse@apache.org">Emmanuel Venisse</a>
47   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
48   * @version $Id: JavadocReport.java 1800564 2017-07-02 14:08:18Z michaelo $
49   * @since 2.0
50   * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/">Javadoc Tool</a>
51   * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#options">Javadoc Options</a>
52   */
53  @Mojo( name = "javadoc", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
54  @Execute( phase = LifecyclePhase.GENERATE_SOURCES )
55  public class JavadocReport
56      extends AbstractJavadocMojo
57      implements MavenReport
58  {
59      // ----------------------------------------------------------------------
60      // Report Mojo Parameters
61      // ----------------------------------------------------------------------
62  
63      /**
64       * Specifies the destination directory where javadoc saves the generated HTML files.
65       */
66      @Parameter( property = "reportOutputDirectory", defaultValue = "${project.reporting.outputDirectory}/apidocs",
67                  required = true )
68      private File reportOutputDirectory;
69  
70      /**
71       * The name of the destination directory.
72       * <br/>
73       *
74       * @since 2.1
75       */
76      @Parameter( property = "destDir", defaultValue = "apidocs" )
77      private String destDir;
78  
79      /**
80       * The name of the Javadoc report to be displayed in the Maven Generated Reports page
81       * (i.e. <code>project-reports.html</code>).
82       *
83       * @since 2.1
84       */
85      @Parameter( property = "name" )
86      private String name;
87  
88      /**
89       * The description of the Javadoc report to be displayed in the Maven Generated Reports page
90       * (i.e. <code>project-reports.html</code>).
91       *
92       * @since 2.1
93       */
94      @Parameter( property = "description" )
95      private String description;
96  
97      // ----------------------------------------------------------------------
98      // Report public methods
99      // ----------------------------------------------------------------------
100 
101     /** {@inheritDoc} */
102     @Override
103     public String getName( Locale locale )
104     {
105         if ( StringUtils.isEmpty( name ) )
106         {
107             return getBundle( locale ).getString( "report.javadoc.name" );
108         }
109 
110         return name;
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public String getDescription( Locale locale )
116     {
117         if ( StringUtils.isEmpty( description ) )
118         {
119             return getBundle( locale ).getString( "report.javadoc.description" );
120         }
121 
122         return description;
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public void generate( Sink sink, Locale locale )
128         throws MavenReportException
129     {
130         outputDirectory = getReportOutputDirectory();
131 
132         try
133         {
134             executeReport( locale );
135         }
136         catch ( MavenReportException e )
137         {
138             if ( failOnError )
139             {
140                 throw e;
141             }
142             getLog().error( "Error while creating javadoc report: " + e.getMessage(), e );
143         }
144         catch ( RuntimeException e )
145         {
146             if ( failOnError )
147             {
148                 throw e;
149             }
150             getLog().error( "Error while creating javadoc report: " + e.getMessage(), e );
151         }
152     }
153 
154     /** {@inheritDoc} */
155     @Override
156     public String getOutputName()
157     {
158         return destDir + "/index";
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public boolean isExternalReport()
164     {
165         return true;
166     }
167 
168     /**
169      * {@inheritDoc}
170      *
171      * <br/>
172      * The logic is the following:
173      * <table>
174      *   <tbody>
175      *     <tr>
176      *       <th> isAggregator </th>
177      *       <th> hasSourceFiles </th>
178      *       <th> isRootProject </th>
179      *       <th> Generate Report </th>
180      *     </tr>
181      *     <tr>
182      *       <td>True</td>
183      *       <td>True</td>
184      *       <td>True</td>
185      *       <td>True</td>
186      *     </tr>
187      *     <tr>
188      *       <td>True</td>
189      *       <td>True</td>
190      *       <td>False</td>
191      *       <td>False</td>
192      *     </tr>
193      *     <tr>
194      *       <td>True</td>
195      *       <td>False</td>
196      *       <td>True</td>
197      *       <td>False</td>
198      *     </tr>
199      *     <tr>
200      *       <td>True</td>
201      *       <td>False</td>
202      *       <td>False</td>
203      *       <td>False</td>
204      *     </tr>
205      *     <tr>
206      *       <td>False</td>
207      *       <td>True</td>
208      *       <td>True</td>
209      *       <td>True</td>
210      *     </tr>
211      *     <tr>
212      *       <td>False</td>
213      *       <td>True</td>
214      *       <td>False</td>
215      *       <td>True</td>
216      *     </tr>
217      *     <tr>
218      *        <td>False</td>
219      *        <td>False</td>
220      *        <td>True</td>
221      *        <td>False</td>
222      *      </tr>
223      *      <tr>
224      *        <td>False</td>
225      *        <td>False</td>
226      *        <td>False</td>
227      *        <td>False</td>
228      *      </tr>
229      *    </tbody>
230      *  </table>
231      */
232     @Override
233     public boolean canGenerateReport()
234     {
235         boolean canGenerate = false;
236 
237         if ( !this.isAggregator() || ( this.isAggregator() && this.project.isExecutionRoot() ) )
238         {
239             Collection<String> sourcePaths;
240             List<String> files;
241             try
242             {
243                 sourcePaths = collect( getSourcePaths().values() );
244                 files = getFiles( sourcePaths );
245             }
246             catch ( MavenReportException e )
247             {
248                 getLog().error( e.getMessage(), e );
249                 return false;
250             }
251 
252             canGenerate = canGenerateReport( files );
253         }
254         if ( getLog().isDebugEnabled() )
255         {
256             getLog().debug( " canGenerateReport = " + canGenerate + " for project " + this.project );
257         }
258         return canGenerate;
259     }
260 
261     /** {@inheritDoc} */
262     @Override
263     public String getCategoryName()
264     {
265         return CATEGORY_PROJECT_REPORTS;
266     }
267 
268     /** {@inheritDoc} */
269     @Override
270     public File getReportOutputDirectory()
271     {
272         if ( reportOutputDirectory == null )
273         {
274             return outputDirectory;
275         }
276 
277         return reportOutputDirectory;
278     }
279 
280     /**
281      * Method to set the directory where the generated reports will be put
282      *
283      * @param reportOutputDirectory the directory file to be set
284      */
285     @Override
286     public void setReportOutputDirectory( File reportOutputDirectory )
287     {
288         updateReportOutputDirectory( reportOutputDirectory, destDir );
289     }
290 
291     /**
292      * @param theDestDir The destiation directory.
293      */
294     public void setDestDir( String theDestDir )
295     {
296         this.destDir = theDestDir;
297         updateReportOutputDirectory( reportOutputDirectory, theDestDir );
298     }
299 
300     private void updateReportOutputDirectory( File reportOutputDirectory, String destDir )
301     {
302         if ( reportOutputDirectory != null && destDir != null
303              && !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) )
304         {
305             this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
306         }
307         else
308         {
309             this.reportOutputDirectory = reportOutputDirectory;
310         }
311     }
312 
313     /** {@inheritDoc} */
314     @Override
315     public void doExecute()
316         throws MojoExecutionException, MojoFailureException
317     {
318         if ( skip )
319         {
320             getLog().info( "Skipping javadoc generation" );
321             return;
322         }
323 
324         try
325         {
326             RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
327             SiteRendererSink sink = new SiteRendererSink( context );
328             Locale locale = Locale.getDefault();
329             generate( sink, locale );
330         }
331         catch ( MavenReportException e )
332         {
333             failOnError( "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation", e );
334         }
335         catch ( RuntimeException e )
336         {
337             failOnError( "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation", e );
338         }
339     }
340 
341     /**
342      * Gets the resource bundle for the specified locale.
343      *
344      * @param locale The locale of the currently generated report.
345      * @return The resource bundle for the requested locale.
346      */
347     private ResourceBundle getBundle( Locale locale )
348     {
349         return ResourceBundle.getBundle( "javadoc-report", locale, getClass().getClassLoader() );
350     }
351 }