1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.plugins.javadoc;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.util.Collection;
24  import java.util.Locale;
25  import java.util.Map;
26  import java.util.ResourceBundle;
27  import java.util.stream.Collectors;
28  
29  import org.apache.maven.doxia.sink.Sink;
30  import org.apache.maven.doxia.sink.SinkFactory;
31  import org.apache.maven.doxia.siterenderer.RenderingContext;
32  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.plugins.annotations.Execute;
36  import org.apache.maven.plugins.annotations.LifecyclePhase;
37  import org.apache.maven.plugins.annotations.Mojo;
38  import org.apache.maven.plugins.annotations.Parameter;
39  import org.apache.maven.plugins.annotations.ResolutionScope;
40  import org.apache.maven.reporting.MavenMultiPageReport;
41  import org.apache.maven.reporting.MavenReportException;
42  import org.codehaus.plexus.util.StringUtils;
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 extends AbstractJavadocMojo implements MavenMultiPageReport {
56      
57      
58      
59  
60      
61  
62  
63      @Parameter(
64              property = "reportOutputDirectory",
65              defaultValue = "${project.reporting.outputDirectory}/apidocs",
66              required = true)
67      private File reportOutputDirectory;
68  
69      
70  
71  
72  
73  
74  
75      @Parameter(property = "destDir", defaultValue = "apidocs")
76      private String destDir;
77  
78      
79  
80  
81  
82  
83  
84      @Parameter(property = "name")
85      private String name;
86  
87      
88  
89  
90  
91  
92  
93      @Parameter(property = "description")
94      private String description;
95  
96      
97      
98      
99  
100     
101     @Override
102     public String getName(Locale locale) {
103         if (StringUtils.isEmpty(name)) {
104             return getBundle(locale).getString("report.javadoc.name");
105         }
106 
107         return name;
108     }
109 
110     
111     @Override
112     public String getDescription(Locale locale) {
113         if (StringUtils.isEmpty(description)) {
114             return getBundle(locale).getString("report.javadoc.description");
115         }
116 
117         return description;
118     }
119 
120     
121     @Override
122     public void generate(org.codehaus.doxia.sink.Sink sink, Locale locale) throws MavenReportException {
123         generate(sink, null, locale);
124     }
125 
126     public void generate(Sink sink, Locale locale) throws MavenReportException {
127         generate(sink, null, locale);
128     }
129 
130     
131     @Override
132     public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException {
133         outputDirectory = getReportOutputDirectory();
134 
135         try {
136             executeReport(locale);
137         } catch (MavenReportException | RuntimeException e) {
138             if (failOnError) {
139                 throw e;
140             }
141             getLog().error("Error while creating javadoc report: " + e.getMessage(), e);
142         }
143     }
144 
145     
146     @Override
147     public String getOutputName() {
148         return destDir + "/index";
149     }
150 
151     
152     @Override
153     public boolean isExternalReport() {
154         return true;
155     }
156 
157     
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     @Override
222     public boolean canGenerateReport() {
223         boolean canGenerate = false;
224 
225         if (this.isAggregator() || !"pom".equals(this.project.getPackaging())) {
226             Collection<Path> sourcePaths;
227             Map<Path, Collection<String>> files;
228             try {
229                 sourcePaths = getSourcePaths().stream()
230                         .flatMap(e -> e.getSourcePaths().stream())
231                         .collect(Collectors.toList());
232                 files = getFiles(sourcePaths);
233             } catch (MavenReportException e) {
234                 getLog().error(e.getMessage(), e);
235                 return false;
236             }
237 
238             canGenerate = canGenerateReport(files);
239         }
240         if (getLog().isDebugEnabled()) {
241             getLog().debug(" canGenerateReport = " + canGenerate + " for project " + this.project);
242         }
243         return canGenerate;
244     }
245 
246     
247     @Override
248     public String getCategoryName() {
249         return CATEGORY_PROJECT_REPORTS;
250     }
251 
252     
253     @Override
254     public File getReportOutputDirectory() {
255         if (reportOutputDirectory == null) {
256             return outputDirectory;
257         }
258 
259         return reportOutputDirectory;
260     }
261 
262     
263 
264 
265 
266 
267     @Override
268     public void setReportOutputDirectory(File reportOutputDirectory) {
269         updateReportOutputDirectory(reportOutputDirectory, destDir);
270     }
271 
272     
273 
274 
275     public void setDestDir(String theDestDir) {
276         this.destDir = theDestDir;
277         updateReportOutputDirectory(reportOutputDirectory, theDestDir);
278     }
279 
280     private void updateReportOutputDirectory(File reportOutputDirectory, String destDir) {
281         if (reportOutputDirectory != null
282                 && destDir != null
283                 && !reportOutputDirectory.getAbsolutePath().endsWith(destDir)) {
284             this.reportOutputDirectory = new File(reportOutputDirectory, destDir);
285         } else {
286             this.reportOutputDirectory = reportOutputDirectory;
287         }
288     }
289 
290     
291     @Override
292     public void doExecute() throws MojoExecutionException, MojoFailureException {
293         if (skip) {
294             getLog().info("Skipping javadoc generation");
295             return;
296         }
297 
298         File outputDirectory = new File(getOutputDirectory());
299 
300         String filename = getOutputName() + ".html";
301 
302         Locale locale = Locale.getDefault();
303 
304         try {
305             
306             RenderingContext docRenderingContext = new RenderingContext(outputDirectory, filename, null);
307 
308             SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
309 
310             generate(sink, null, locale);
311 
312         } catch (MavenReportException | RuntimeException e) {
313             failOnError("An error has occurred in " + getName(Locale.ENGLISH) + " report generation", e);
314         }
315     }
316 
317     
318 
319 
320 
321 
322 
323     private ResourceBundle getBundle(Locale locale) {
324         return ResourceBundle.getBundle("javadoc-report", locale, getClass().getClassLoader());
325     }
326 }