1   package org.apache.maven.plugins.javadoc;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import java.io.File;
23  import java.nio.file.Path;
24  import java.util.Collection;
25  import java.util.Locale;
26  import java.util.Map;
27  import java.util.ResourceBundle;
28  
29  import org.apache.maven.doxia.siterenderer.RenderingContext;
30  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.MojoFailureException;
33  import org.apache.maven.plugins.annotations.Execute;
34  import org.apache.maven.plugins.annotations.LifecyclePhase;
35  import org.apache.maven.plugins.annotations.Mojo;
36  import org.apache.maven.plugins.annotations.Parameter;
37  import org.apache.maven.plugins.annotations.ResolutionScope;
38  import org.apache.maven.reporting.MavenReport;
39  import org.apache.maven.reporting.MavenReportException;
40  import org.codehaus.doxia.sink.Sink;
41  import org.codehaus.plexus.util.StringUtils;
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
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      
61      
62  
63      
64  
65  
66      @Parameter( property = "reportOutputDirectory", defaultValue = "${project.reporting.outputDirectory}/apidocs",
67                  required = true )
68      private File reportOutputDirectory;
69  
70      
71  
72  
73  
74  
75  
76      @Parameter( property = "destDir", defaultValue = "apidocs" )
77      private String destDir;
78  
79      
80  
81  
82  
83  
84  
85      @Parameter( property = "name" )
86      private String name;
87  
88      
89  
90  
91  
92  
93  
94      @Parameter( property = "description" )
95      private String description;
96  
97      
98      
99      
100 
101     
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     
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     
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 | RuntimeException e )
137         {
138             if ( failOnError )
139             {
140                 throw e;
141             }
142             getLog().error( "Error while creating javadoc report: " + e.getMessage(), e );
143         }
144     }
145 
146     
147     @Override
148     public String getOutputName()
149     {
150         return destDir + "/index";
151     }
152 
153     
154     @Override
155     public boolean isExternalReport()
156     {
157         return true;
158     }
159 
160     
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224     @Override
225     public boolean canGenerateReport()
226     {
227         boolean canGenerate = false;
228 
229         if ( this.isAggregator() || !"pom".equals( this.project.getPackaging() ) )
230         {
231             Collection<Path> sourcePaths;
232             Map<Path, Collection<String>> files;
233             try
234             {
235                 sourcePaths = collect( getSourcePaths().values() );
236                 files = getFiles( sourcePaths );
237             }
238             catch ( MavenReportException e )
239             {
240                 getLog().error( e.getMessage(), e );
241                 return false;
242             }
243 
244             canGenerate = canGenerateReport( files );
245         }
246         if ( getLog().isDebugEnabled() )
247         {
248             getLog().debug( " canGenerateReport = " + canGenerate + " for project " + this.project );
249         }
250         return canGenerate;
251     }
252 
253     
254     @Override
255     public String getCategoryName()
256     {
257         return CATEGORY_PROJECT_REPORTS;
258     }
259 
260     
261     @Override
262     public File getReportOutputDirectory()
263     {
264         if ( reportOutputDirectory == null )
265         {
266             return outputDirectory;
267         }
268 
269         return reportOutputDirectory;
270     }
271 
272     
273 
274 
275 
276 
277     @Override
278     public void setReportOutputDirectory( File reportOutputDirectory )
279     {
280         updateReportOutputDirectory( reportOutputDirectory, destDir );
281     }
282 
283     
284 
285 
286     public void setDestDir( String theDestDir )
287     {
288         this.destDir = theDestDir;
289         updateReportOutputDirectory( reportOutputDirectory, theDestDir );
290     }
291 
292     private void updateReportOutputDirectory( File reportOutputDirectory, String destDir )
293     {
294         if ( reportOutputDirectory != null && destDir != null
295              && !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) )
296         {
297             this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
298         }
299         else
300         {
301             this.reportOutputDirectory = reportOutputDirectory;
302         }
303     }
304 
305     
306     @Override
307     public void doExecute()
308         throws MojoExecutionException, MojoFailureException
309     {
310         if ( skip )
311         {
312             getLog().info( "Skipping javadoc generation" );
313             return;
314         }
315 
316         try
317         {
318             RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
319             SiteRendererSink sink = new SiteRendererSink( context );
320             Locale locale = Locale.getDefault();
321             generate( sink, locale );
322         }
323         catch ( MavenReportException | RuntimeException e )
324         {
325             failOnError( "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation", e );
326         }
327     }
328 
329     
330 
331 
332 
333 
334 
335     private ResourceBundle getBundle( Locale locale )
336     {
337         return ResourceBundle.getBundle( "javadoc-report", locale, getClass().getClassLoader() );
338     }
339 }