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.rat.mp;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  import java.util.ResourceBundle;
33  
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.doxia.sink.Sink;
36  import org.apache.maven.doxia.sink.SinkFactory;
37  import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
38  import org.apache.maven.doxia.site.SiteModel;
39  import org.apache.maven.doxia.siterenderer.DocumentRenderingContext;
40  import org.apache.maven.doxia.siterenderer.Renderer;
41  import org.apache.maven.doxia.siterenderer.RendererException;
42  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
43  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
44  import org.apache.maven.doxia.tools.SiteTool;
45  import org.apache.maven.doxia.tools.SiteToolException;
46  import org.apache.maven.execution.MavenSession;
47  import org.apache.maven.plugin.MojoExecutionException;
48  import org.apache.maven.plugins.annotations.Component;
49  import org.apache.maven.plugins.annotations.Mojo;
50  import org.apache.maven.plugins.annotations.Parameter;
51  import org.apache.maven.plugins.annotations.ResolutionScope;
52  import org.apache.maven.reporting.MavenMultiPageReport;
53  import org.apache.maven.reporting.MavenReportException;
54  import org.apache.rat.ReportConfiguration;
55  import org.apache.rat.Reporter;
56  import org.apache.rat.VersionInfo;
57  import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
58  import org.codehaus.plexus.util.ReaderFactory;
59  import org.eclipse.aether.repository.ArtifactRepository;
60  import org.eclipse.aether.repository.RemoteRepository;
61  
62  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
63  
64  /**
65   * Generates a report with RAT's output.
66   */
67  @Mojo(name = "rat", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
68  public class RatReportMojo extends AbstractRatMojo implements MavenMultiPageReport {
69  
70      /**
71       * The output directory for the report. Note that this parameter is only
72       * evaluated if the goal is run directly from the command line. If the goal is
73       * run indirectly as part of a site generation, the output directory configured
74       * in the Maven Site Plugin is used instead.
75       */
76      @Parameter(defaultValue = "${project.reporting.outputDirectory}", readonly = true, required = true)
77      protected File outputDirectory;
78  
79      /**
80       * Specifies the input encoding.
81       */
82      @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}", readonly = true)
83      private String inputEncoding;
84  
85      /**
86       * Specifies the output encoding.
87       */
88      @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true)
89      private String outputEncoding;
90  
91      /**
92       * The local repository.
93       */
94      @Parameter(defaultValue = "${session}", readonly = true, required = true)
95      protected MavenSession session;
96  
97      /**
98       * Remote repositories used for the project.
99       *
100      * @deprecated replaced by {@link #remoteRepositories}
101      */
102     @Parameter(defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true)
103     @Deprecated
104     protected List<ArtifactRepository> remoteArtifactRepositories;
105 
106     /**
107      * Remote repositories used for the project.
108      */
109     @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
110     protected List<RemoteRepository> remoteRepositories;
111 
112     /**
113      * SiteTool.
114      */
115     @Component
116     protected SiteTool siteTool;
117 
118     /**
119      * Doxia Site Renderer component.
120      */
121     @Component
122     protected Renderer siteRenderer;
123 
124     /**
125      * The current sink to use
126      */
127     private Sink sink;
128 
129     /**
130      * The sink factory to use
131      */
132     private SinkFactory sinkFactory;
133 
134     /**
135      * The current report output directory to use
136      */
137     private File reportOutputDirectory;
138 
139     /**
140      * This method is called when the report generation is invoked directly as a
141      * standalone Mojo.
142      *
143      * @throws MojoExecutionException if an error occurs when generating the report
144      * @see org.apache.maven.plugin.Mojo#execute()
145      */
146     @Override
147     public void execute() throws MojoExecutionException {
148         if (!canGenerateReport()) {
149             return;
150         }
151 
152         File outputDirectory = new File(getOutputDirectory());
153 
154         String filename = getOutputName() + ".html";
155 
156         Locale locale = Locale.getDefault();
157 
158         try {
159             SiteRenderingContext siteContext = createSiteRenderingContext(locale);
160 
161             // copy resources
162             getSiteRenderer().copyResources(siteContext, outputDirectory);
163 
164             // TODO Replace null with real value
165             DocumentRenderingContext docContext =
166                     new DocumentRenderingContext(outputDirectory, filename, "xhtml5");
167             SiteRendererSink sink = new SiteRendererSink(docContext);
168 
169             generate(sink, null, locale);
170 
171             // MSHARED-204: only render Doxia sink if not an external report
172             if (!isExternalReport()) {
173                 if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
174                     getLog().error("Unable to create output directory: " + outputDirectory);
175                 }
176                 try (Writer writer = new OutputStreamWriter(
177                         Files.newOutputStream(new File(outputDirectory, filename).toPath()),
178                         getOutputEncoding())) {
179                     // render report
180                     getSiteRenderer().mergeDocumentIntoSite(writer, sink, siteContext);
181                 }
182 
183             }
184 
185             // copy generated resources also
186             getSiteRenderer().copyResources(siteContext, outputDirectory);
187         } catch (RendererException | IOException | MavenReportException e) {
188             throw new MojoExecutionException(
189                     "An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
190         }
191     }
192 
193     private SiteRenderingContext createSiteRenderingContext(final Locale locale)
194             throws MavenReportException, IOException {
195         SiteModel siteModel = new SiteModel();
196 
197         Map<String, Object> templateProperties = new HashMap<>();
198         templateProperties.put("standalone", Boolean.TRUE);
199         templateProperties.put("project", getProject());
200         templateProperties.put("inputEncoding", getInputEncoding());
201         templateProperties.put("outputEncoding", getOutputEncoding());
202         for (Map.Entry<Object, Object> entry : getProject().getProperties().entrySet()) {
203             templateProperties.put((String) entry.getKey(), entry.getValue());
204         }
205 
206         org.apache.maven.doxia.site.Skin siteSkin = siteModel.getSkin();
207 
208         if (siteSkin == null || siteSkin.getGroupId() == null
209                 || siteSkin.getArtifactId() == null || siteSkin.getVersion() == null) {
210             getLog().debug("No skin configuration found in site.xml. Using default Maven skin configuration.");
211 
212             // Create a minimal default skin configuration
213             siteSkin = new org.apache.maven.doxia.site.Skin();
214             siteSkin.setGroupId("org.apache.maven.skins");
215             siteSkin.setArtifactId("maven-fluido-skin");
216         }
217 
218         try {
219             Artifact skinArtifact = siteTool.getSkinArtifactFromRepository(
220                     session.getRepositorySession(),
221                     remoteRepositories,
222                     siteSkin
223             );
224 
225             getLog().debug(buffer().a("Rendering content with ").strong(skinArtifact.getId() + " skin").a('.').build());
226 
227             SiteRenderingContext context = siteRenderer.createContextForSkin(
228                     skinArtifact,
229                     templateProperties,
230                     siteModel,
231                     project.getName(),
232                     locale
233             );
234             context.setRootDirectory(project.getBasedir());
235             return context;
236 
237         } catch (SiteToolException e) {
238             throw new MavenReportException("Failed to retrieve skin artifact", e);
239         } catch (RendererException e) {
240             throw new MavenReportException("Failed to create context for skin", e);
241         }
242     }
243 
244     /**
245      * This method is called when the report generation is invoked by
246      * maven-site-plugin.
247      *
248      * @param sink the sink to use for the generation.
249      * @param sinkFactory the sink factory to use for the generation.
250      * @param locale the wanted locale to generate the report, could be null.
251      * @throws MavenReportException if any
252      */
253     @Override
254     public void generate(final Sink sink, final SinkFactory sinkFactory, final Locale locale) throws MavenReportException {
255         if (!canGenerateReport()) {
256             // This report cannot be generated as part of the current build.
257             getLog().info("This report cannot be generated as part of the current build. "
258                     + "The report name should be referenced in this line of output.");
259         } else {
260             this.sink = sink;
261             this.sinkFactory = sinkFactory;
262 
263             if (!(sink instanceof SiteRendererSink)) {
264 
265                 generateReportManually(locale);
266             } else {
267                 executeReport(locale);
268             }
269 
270             closeReport();
271         }
272     }
273 
274     private void generateReportManually(final Locale locale) throws MavenReportException {
275         try {
276             File outputDir = new File(getOutputDirectory());
277             String filename = getOutputName() + ".html";
278 
279             SiteRenderingContext siteContext = createSiteRenderingContext(locale);
280 
281             if (!outputDir.exists() && !outputDir.mkdirs()) {
282                 getLog().error("Unable to create output directory: " + outputDir);
283             }
284 
285             DocumentRenderingContext docContext =
286                     new DocumentRenderingContext(outputDir, filename, "xhtml5");
287 
288             SiteRendererSink sink = new SiteRendererSink(docContext);
289 
290             this.sink = sink;
291             this.sinkFactory = null;
292 
293             executeReport(locale);
294 
295             try (Writer writer = new OutputStreamWriter(
296                     Files.newOutputStream(new File(outputDir, filename).toPath()),
297                     getOutputEncoding())) {
298                 getSiteRenderer().mergeDocumentIntoSite(writer, sink, siteContext);
299             }
300 
301         } catch (IOException | RendererException e) {
302             throw new MavenReportException("Failed to render RAT report manually", e);
303         }
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     public void generate(final Sink sink, final Locale locale) throws MavenReportException {
316         generate(sink, null, locale);
317     }
318 
319     /**
320      * @return CATEGORY_PROJECT_REPORTS
321      */
322     @Override
323     public String getCategoryName() {
324         return CATEGORY_PROJECT_REPORTS;
325     }
326 
327     @Override
328     public File getReportOutputDirectory() {
329         if (reportOutputDirectory == null) {
330             reportOutputDirectory = new File(getOutputDirectory());
331         }
332 
333         return reportOutputDirectory;
334     }
335 
336     @Override
337     public void setReportOutputDirectory(final File reportOutputDirectory) {
338         this.reportOutputDirectory = reportOutputDirectory;
339         this.outputDirectory = reportOutputDirectory;
340     }
341 
342     protected String getOutputDirectory() {
343         return outputDirectory.getAbsolutePath();
344     }
345 
346     protected Renderer getSiteRenderer() {
347         return siteRenderer;
348     }
349 
350     /**
351      * Gets the input files encoding.
352      *
353      * @return The input files encoding, never <code>null</code>.
354      */
355     protected String getInputEncoding() {
356         return (inputEncoding == null) ? ReaderFactory.FILE_ENCODING : inputEncoding;
357     }
358 
359     /**
360      * Gets the effective reporting output files encoding.
361      *
362      * @return The effective reporting output file encoding, never
363      * <code>null</code>.
364      */
365     protected String getOutputEncoding() {
366         return (outputEncoding == null) ? StandardCharsets.UTF_8.toString() : outputEncoding;
367     }
368 
369     /**
370      * Actions when closing the report.
371      */
372     protected void closeReport() {
373         getSink().close();
374     }
375 
376     /**
377      * @return the sink used
378      */
379     public Sink getSink() {
380         return sink;
381     }
382 
383     /**
384      * @return the sink factory used
385      */
386     public SinkFactory getSinkFactory() {
387         return sinkFactory;
388     }
389 
390     /**
391      * @return {@code false} by default.
392      * @see org.apache.maven.reporting.MavenReport#isExternalReport()
393      */
394     @Override
395     public boolean isExternalReport() {
396         return false;
397     }
398 
399     @Override
400     public boolean canGenerateReport() {
401         return !skip;
402     }
403 
404     /**
405      * Writes the report to the Doxia sink.
406      *
407      * @param locale The locale to use for writing the report.
408      * @throws MavenReportException Writing the report failed.
409      */
410     protected void executeReport(final Locale locale) throws MavenReportException {
411         ResourceBundle bundle = getBundle(locale);
412         final String title = bundle.getString("report.rat.title");
413         sink.head();
414         sink.title();
415         sink.text(title);
416         sink.title_();
417         sink.head_();
418 
419         sink.body();
420 
421         sink.section1();
422         sink.sectionTitle1();
423         sink.text(title);
424         sink.sectionTitle1_();
425 
426         sink.paragraph();
427         sink.text(bundle.getString("report.rat.link") + " ");
428         sink.link(bundle.getString("report.rat.url"));
429         sink.text(bundle.getString("report.rat.fullName"));
430         sink.link_();
431         final String ratVersion = new VersionInfo(RatReportMojo.class).toString();
432         if (ratVersion != null) {
433             sink.text(" " + ratVersion);
434         }
435         sink.text(".");
436         sink.paragraph_();
437 
438         sink.paragraph();
439         sink.verbatim(new SinkEventAttributeSet());
440         try {
441             ReportConfiguration config = getConfiguration();
442             config.setFrom(getDefaultsBuilder().build());
443             logLicenses(config.getLicenses(LicenseFilter.ALL));
444             ByteArrayOutputStream baos = new ByteArrayOutputStream();
445             config.setOut(() -> baos);
446             new Reporter(config).output();
447             sink.text(baos.toString(StandardCharsets.UTF_8.name()));
448         } catch (Exception e) {
449             throw new MavenReportException(e.getMessage(), e);
450         }
451         sink.verbatim_();
452         sink.paragraph_();
453         sink.section1_();
454         sink.body_();
455     }
456 
457     /**
458      * Returns the reports bundle
459      *
460      * @param locale Requested locale of the bundle
461      * @return The bundle, which is used to read localized strings.
462      */
463     private ResourceBundle getBundle(final Locale locale) {
464         return ResourceBundle.getBundle("org/apache/rat/mp/rat-report", locale, getClass().getClassLoader());
465     }
466 
467     /**
468      * Returns the reports description.
469      *
470      * @param locale Requested locale of the bundle
471      * @return Report description, as given by the key "report.rat.description" in
472      * the bundle.
473      */
474     @Override
475     public String getDescription(final Locale locale) {
476         return getBundle(locale).getString("report.rat.description");
477     }
478 
479     /**
480      * Returns the reports name.
481      *
482      * @param locale Requested locale of the bundle
483      * @return Report name, as given by the key "report.rat.name" in the bundle.
484      */
485     @Override
486     public String getName(final Locale locale) {
487         return getBundle(locale).getString("report.rat.name");
488     }
489 
490     /**
491      * Returns the reports file name.
492      *
493      * @return "rat-report"
494      */
495     @Override
496     public String getOutputName() {
497         return "rat-report";
498     }
499 }