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