View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.reporting;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  import java.util.Date;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Locale;
30  import java.util.Map;
31  
32  import org.apache.maven.archiver.MavenArchiver;
33  import org.apache.maven.artifact.Artifact;
34  import org.apache.maven.doxia.sink.Sink;
35  import org.apache.maven.doxia.sink.SinkFactory;
36  import org.apache.maven.doxia.site.SiteModel;
37  import org.apache.maven.doxia.siterenderer.DocumentRenderingContext;
38  import org.apache.maven.doxia.siterenderer.Renderer;
39  import org.apache.maven.doxia.siterenderer.RendererException;
40  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
41  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
42  import org.apache.maven.doxia.tools.SiteTool;
43  import org.apache.maven.doxia.tools.SiteToolException;
44  import org.apache.maven.plugin.AbstractMojo;
45  import org.apache.maven.plugin.MojoExecution;
46  import org.apache.maven.plugin.MojoExecutionException;
47  import org.apache.maven.plugins.annotations.Component;
48  import org.apache.maven.plugins.annotations.Parameter;
49  import org.apache.maven.project.MavenProject;
50  import org.apache.maven.shared.utils.WriterFactory;
51  import org.codehaus.plexus.PlexusContainer;
52  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
53  import org.codehaus.plexus.util.ReaderFactory;
54  import org.eclipse.aether.RepositorySystemSession;
55  import org.eclipse.aether.repository.RemoteRepository;
56  
57  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
58  
59  /**
60   * The basis for a Maven report which can be generated both as part of a site generation or
61   * as a direct standalone goal invocation.
62   * Both invocations are delegated to <code>abstract executeReport( Locale )</code> from:
63   * <ul>
64   * <li>Mojo's <code>execute()</code> method, see maven-plugin-api</li>
65   * <li>MavenMultiPageReport's <code>generate( Sink, SinkFactory, Locale )</code>, see maven-reporting-api</li>
66   * </ul>
67   *
68   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
69   * @since 2.0
70   * @see #execute() <code>Mojo.execute()</code>, from maven-plugin-api
71   * @see #generate(Sink, SinkFactory, Locale) <code>MavenMultiPageReport.generate( Sink, SinkFactory, Locale )</code>,
72   *  from maven-reporting-api
73   * @see #executeReport(Locale) <code>abstract executeReport( Locale )</code>
74   */
75  public abstract class AbstractMavenReport extends AbstractMojo implements MavenMultiPageReport {
76      /**
77       * The output directory for the report. Note that this parameter is only evaluated if the goal is run directly from
78       * the command line. If the goal is run indirectly as part of a site generation, the output directory configured in
79       * the Maven Site Plugin is used instead.
80       */
81      @Parameter(defaultValue = "${project.reporting.outputDirectory}", readonly = true, required = true)
82      protected File outputDirectory;
83  
84      /**
85       * The Maven Project.
86       */
87      @Parameter(defaultValue = "${project}", readonly = true, required = true)
88      protected MavenProject project;
89  
90      /**
91       * The mojo execution
92       */
93      @Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
94      protected MojoExecution mojoExecution;
95  
96      /**
97       * The reactor projects.
98       */
99      @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
100     protected List<MavenProject> reactorProjects;
101 
102     /**
103      * Specifies the input encoding.
104      */
105     @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}", readonly = true)
106     private String inputEncoding;
107 
108     /**
109      * Specifies the output encoding.
110      */
111     @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true)
112     private String outputEncoding;
113 
114     /**
115      * The repository system session.
116      */
117     @Parameter(defaultValue = "${repositorySystemSession}", readonly = true, required = true)
118     protected RepositorySystemSession repoSession;
119 
120     /**
121      * Remote project repositories used for the project.
122      */
123     @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
124     protected List<RemoteRepository> remoteProjectRepositories;
125 
126     /**
127      * Directory containing the <code>site.xml</code> file.
128      */
129     @Parameter(defaultValue = "${basedir}/src/site")
130     protected File siteDirectory;
131 
132     /**
133      * The locale to use  when the report generation is invoked directly as a standalone Mojo.
134      * <p>
135      * <b>Default value is</b>: {@link SiteTool#DEFAULT_LOCALE}
136      *
137      * @see SiteTool#getSiteLocales(String)
138      */
139     @Parameter(defaultValue = "default")
140     protected String locale;
141 
142     /**
143      * SiteTool.
144      */
145     @Component
146     protected SiteTool siteTool;
147 
148     /**
149      * Doxia Site Renderer component.
150      */
151     @Component
152     protected Renderer siteRenderer;
153 
154     /** The current sink to use */
155     private Sink sink;
156 
157     /** The sink factory to use */
158     private SinkFactory sinkFactory;
159 
160     /** The current report output directory to use */
161     private File reportOutputDirectory;
162 
163     /**
164      * The report output format: null by default, to represent a site, but can be configured to a Doxia Sink id.
165      */
166     @Parameter(property = "output.format")
167     protected String outputFormat;
168 
169     @Component
170     private PlexusContainer container;
171 
172     /**
173      * This method is called when the report generation is invoked directly as a standalone Mojo.
174      *
175      * @throws MojoExecutionException if an error occurs when generating the report
176      * @see org.apache.maven.plugin.Mojo#execute()
177      */
178     @Override
179     public void execute() throws MojoExecutionException {
180         if (!canGenerateReport()) {
181             return;
182         }
183 
184         if (outputFormat != null) {
185             reportToMarkup();
186         } else {
187             reportToSite();
188         }
189     }
190 
191     private void reportToMarkup() throws MojoExecutionException {
192         getLog().info("Rendering to " + outputFormat + " markup");
193 
194         if (!isExternalReport()) {
195             String filename = getOutputName() + '.' + outputFormat;
196             try {
197                 sinkFactory = container.lookup(SinkFactory.class, outputFormat);
198                 sink = sinkFactory.createSink(outputDirectory, filename);
199             } catch (ComponentLookupException cle) {
200                 throw new MojoExecutionException(
201                         "Cannot find SinkFactory for Doxia output format: " + outputFormat, cle);
202             } catch (IOException ioe) {
203                 throw new MojoExecutionException("Cannot create sink to " + new File(outputDirectory, filename), ioe);
204             }
205         }
206 
207         try {
208             Locale locale = getLocale();
209             generate(sink, sinkFactory, locale);
210         } catch (MavenReportException e) {
211             throw new MojoExecutionException(
212                     "An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
213         } finally {
214             if (sink != null) {
215                 sink.close();
216             }
217         }
218     }
219 
220     private void reportToSite() throws MojoExecutionException {
221         File outputDirectory = new File(getOutputDirectory());
222 
223         String filename = getOutputName() + ".html";
224 
225         Locale locale = getLocale();
226 
227         try {
228             SiteRenderingContext siteContext = createSiteRenderingContext(locale);
229 
230             // copy resources
231             getSiteRenderer().copyResources(siteContext, outputDirectory);
232 
233             String reportMojoInfo = mojoExecution.getPlugin().getId() + ":" + mojoExecution.getGoal();
234             DocumentRenderingContext docRenderingContext =
235                     new DocumentRenderingContext(outputDirectory, getOutputName(), reportMojoInfo);
236 
237             SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
238 
239             // TODO Compared to Maven Site Plugin multipage reports will not work and fail with a NPE
240             generate(sink, null, locale);
241 
242             if (!isExternalReport()) // MSHARED-204: only render Doxia sink if not an external report
243             {
244                 outputDirectory.mkdirs();
245 
246                 try (Writer writer = new OutputStreamWriter(
247                         new FileOutputStream(new File(outputDirectory, filename)), getOutputEncoding())) {
248                     // render report
249                     getSiteRenderer().mergeDocumentIntoSite(writer, sink, siteContext);
250                 }
251             }
252 
253             // copy generated resources also
254             getSiteRenderer().copyResources(siteContext, outputDirectory);
255         } catch (RendererException | IOException | MavenReportException | SiteToolException e) {
256             throw new MojoExecutionException(
257                     "An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
258         }
259     }
260 
261     private SiteRenderingContext createSiteRenderingContext(Locale locale)
262             throws MavenReportException, IOException, SiteToolException {
263         SiteModel siteModel = siteTool.getSiteModel(
264                 siteDirectory, locale, project, reactorProjects, repoSession, remoteProjectRepositories);
265 
266         Map<String, Object> templateProperties = new HashMap<>();
267         // We tell the skin that we are rendering in standalone mode
268         templateProperties.put("standalone", Boolean.TRUE);
269         templateProperties.put("project", getProject());
270         templateProperties.put("inputEncoding", getInputEncoding());
271         templateProperties.put("outputEncoding", getOutputEncoding());
272         // Put any of the properties in directly into the Velocity context
273         for (Map.Entry<Object, Object> entry : getProject().getProperties().entrySet()) {
274             templateProperties.put((String) entry.getKey(), entry.getValue());
275         }
276 
277         SiteRenderingContext context;
278         try {
279             Artifact skinArtifact =
280                     siteTool.getSkinArtifactFromRepository(repoSession, remoteProjectRepositories, siteModel.getSkin());
281 
282             getLog().info(buffer().a("Rendering content with ")
283                     .strong(skinArtifact.getId() + " skin")
284                     .toString());
285 
286             context = siteRenderer.createContextForSkin(
287                     skinArtifact, templateProperties, siteModel, project.getName(), locale);
288         } catch (SiteToolException e) {
289             throw new MavenReportException("Failed to retrieve skin artifact", e);
290         } catch (RendererException e) {
291             throw new MavenReportException("Failed to create context for skin", e);
292         }
293 
294         // Add publish date
295         String outputTimestamp = getProject().getProperties().getProperty("project.build.outputTimestamp");
296         MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).ifPresent(v -> {
297             context.setPublishDate(Date.from(v));
298         });
299 
300         // Generate static site
301         context.setRootDirectory(project.getBasedir());
302 
303         return context;
304     }
305 
306     /**
307      * Generate a report.
308      *
309      * @param sink the sink to use for the generation.
310      * @param locale the wanted locale to generate the report, could be null.
311      * @throws MavenReportException if any
312      * @deprecated use {@link #generate(Sink, SinkFactory, Locale)} instead.
313      */
314     @Deprecated
315     @Override
316     public void generate(Sink sink, Locale locale) throws MavenReportException {
317         generate(sink, null, locale);
318     }
319 
320     /**
321      * This method is called when the report generation is invoked by maven-site-plugin.
322      *
323      * @param sink
324      * @param sinkFactory
325      * @param locale
326      * @throws MavenReportException
327      */
328     @Override
329     public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException {
330         if (!canGenerateReport()) {
331             getLog().info("This report cannot be generated as part of the current build. "
332                     + "The report name should be referenced in this line of output.");
333             return;
334         }
335 
336         this.sink = sink;
337 
338         this.sinkFactory = sinkFactory;
339 
340         executeReport(locale);
341 
342         closeReport();
343     }
344 
345     /**
346      * @return CATEGORY_PROJECT_REPORTS
347      */
348     @Override
349     public String getCategoryName() {
350         return CATEGORY_PROJECT_REPORTS;
351     }
352 
353     @Override
354     public File getReportOutputDirectory() {
355         if (reportOutputDirectory == null) {
356             reportOutputDirectory = new File(getOutputDirectory());
357         }
358 
359         return reportOutputDirectory;
360     }
361 
362     @Override
363     public void setReportOutputDirectory(File reportOutputDirectory) {
364         this.reportOutputDirectory = reportOutputDirectory;
365         this.outputDirectory = reportOutputDirectory;
366     }
367 
368     protected String getOutputDirectory() {
369         return outputDirectory.getAbsolutePath();
370     }
371 
372     protected MavenProject getProject() {
373         return project;
374     }
375 
376     protected Renderer getSiteRenderer() {
377         return siteRenderer;
378     }
379 
380     /**
381      * Gets the input files encoding.
382      *
383      * @return The input files encoding, never <code>null</code>.
384      */
385     protected String getInputEncoding() {
386         return (inputEncoding == null) ? ReaderFactory.FILE_ENCODING : inputEncoding;
387     }
388 
389     /**
390      * Gets the effective reporting output files encoding.
391      *
392      * @return The effective reporting output file encoding, never <code>null</code>.
393      */
394     protected String getOutputEncoding() {
395         return (outputEncoding == null) ? WriterFactory.UTF_8 : outputEncoding;
396     }
397 
398     /**
399      * Gets the locale
400      *
401      * @return the locale for this standalone report
402      */
403     protected Locale getLocale() {
404         return siteTool.getSiteLocales(locale).get(0);
405     }
406 
407     /**
408      * Actions when closing the report.
409      */
410     protected void closeReport() {
411         if (getSink() != null) {
412             getSink().close();
413         }
414     }
415 
416     /**
417      * @return the sink used
418      */
419     public Sink getSink() {
420         return sink;
421     }
422 
423     /**
424      * @return the sink factory used
425      */
426     public SinkFactory getSinkFactory() {
427         return sinkFactory;
428     }
429 
430     /**
431      * @see org.apache.maven.reporting.MavenReport#isExternalReport()
432      * @return {@code false} by default.
433      */
434     @Override
435     public boolean isExternalReport() {
436         return false;
437     }
438 
439     @Override
440     public boolean canGenerateReport() {
441         return true;
442     }
443 
444     /**
445      * Execute the generation of the report.
446      *
447      * @param locale the wanted locale to return the report's description, could be <code>null</code>.
448      * @throws MavenReportException if any
449      */
450     protected abstract void executeReport(Locale locale) throws MavenReportException;
451 }