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.plugins.invoker;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Locale;
26  
27  import org.apache.maven.plugins.annotations.Component;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.plugins.invoker.model.BuildJob;
31  import org.apache.maven.plugins.invoker.model.io.xpp3.BuildJobXpp3Reader;
32  import org.apache.maven.reporting.AbstractMavenReport;
33  import org.apache.maven.reporting.MavenReportException;
34  import org.codehaus.plexus.i18n.I18N;
35  import org.codehaus.plexus.util.ReaderFactory;
36  import org.codehaus.plexus.util.xml.XmlStreamReader;
37  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
38  
39  /**
40   * Generate a report based on the results of the Maven invocations. <strong>Note:</strong> This mojo doesn't fork any
41   * lifecycle, if you have a clean working copy, you have to use a command like
42   * <code>mvn clean integration-test site</code> to ensure the build results are present when this goal is invoked.
43   *
44   * @author Olivier Lamy
45   * @since 1.4
46   */
47  @Mojo(name = "report", threadSafe = true)
48  public class InvokerReport extends AbstractMavenReport {
49  
50      /**
51       * Base directory where all build reports have been written to.
52       */
53      @Parameter(defaultValue = "${project.build.directory}/invoker-reports", property = "invoker.reportsDirectory")
54      private File reportsDirectory;
55  
56      /**
57       * Internationalization component
58       */
59      @Component
60      protected I18N i18n;
61  
62      protected void executeReport(Locale locale) throws MavenReportException {
63          File[] reportFiles = getReportFiles();
64          BuildJobXpp3Reader buildJobReader = new BuildJobXpp3Reader();
65          List<BuildJob> buildJobs = new ArrayList<>(reportFiles.length);
66          for (File reportFile : reportFiles) {
67              try (XmlStreamReader xmlReader = ReaderFactory.newXmlReader(reportFile)) {
68                  buildJobs.add(buildJobReader.read(xmlReader));
69              } catch (XmlPullParserException e) {
70                  throw new MavenReportException("Failed to parse report file: " + reportFile, e);
71              } catch (IOException e) {
72                  throw new MavenReportException("Failed to read report file: " + reportFile, e);
73              }
74          }
75          InvokerReportRenderer r = new InvokerReportRenderer(getSink(), i18n, locale, getLog(), buildJobs);
76          r.render();
77      }
78  
79      /**
80       * @param locale The locale
81       * @param key The key to search for
82       * @return The text appropriate for the locale.
83       */
84      private String getI18nString(Locale locale, String key) {
85          return i18n.getString("invoker-report", locale, "report.invoker." + key);
86      }
87  
88      /** {@inheritDoc} */
89      public String getName(Locale locale) {
90          return getI18nString(locale, "name");
91      }
92  
93      /** {@inheritDoc} */
94      public String getDescription(Locale locale) {
95          return getI18nString(locale, "description");
96      }
97  
98      public String getOutputName() {
99          return "invoker-report";
100     }
101 
102     private File[] getReportFiles() {
103         return ReportUtils.getReportFiles(reportsDirectory);
104     }
105 
106     public boolean canGenerateReport() {
107         return getReportFiles().length > 0;
108     }
109 }