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