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 javax.inject.Inject;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Locale;
28  
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.plugins.invoker.model.BuildJob;
32  import org.apache.maven.plugins.invoker.model.io.xpp3.BuildJobXpp3Reader;
33  import org.apache.maven.reporting.AbstractMavenReport;
34  import org.apache.maven.reporting.MavenReportException;
35  import org.codehaus.plexus.i18n.I18N;
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      protected final I18N i18n;
60  
61      @Inject
62      public InvokerReport(I18N i18n) {
63          this.i18n = i18n;
64      }
65  
66      protected void executeReport(Locale locale) throws MavenReportException {
67          File[] reportFiles = getReportFiles();
68          BuildJobXpp3Reader buildJobReader = new BuildJobXpp3Reader();
69          List<BuildJob> buildJobs = new ArrayList<>(reportFiles.length);
70          for (File reportFile : reportFiles) {
71              try (XmlStreamReader xmlReader = new XmlStreamReader(reportFile)) {
72                  buildJobs.add(buildJobReader.read(xmlReader));
73              } catch (XmlPullParserException e) {
74                  throw new MavenReportException("Failed to parse report file: " + reportFile, e);
75              } catch (IOException e) {
76                  throw new MavenReportException("Failed to read report file: " + reportFile, e);
77              }
78          }
79          InvokerReportRenderer r = new InvokerReportRenderer(getSink(), i18n, locale, getLog(), buildJobs);
80          r.render();
81      }
82  
83      /**
84       * @param locale The locale
85       * @param key The key to search for
86       * @return The text appropriate for the locale.
87       */
88      private String getI18nString(Locale locale, String key) {
89          return i18n.getString("invoker-report", locale, "report.invoker." + key);
90      }
91  
92      /** {@inheritDoc} */
93      public String getName(Locale locale) {
94          return getI18nString(locale, "name");
95      }
96  
97      /** {@inheritDoc} */
98      public String getDescription(Locale locale) {
99          return getI18nString(locale, "description");
100     }
101 
102     public String getOutputName() {
103         return "invoker";
104     }
105 
106     private File[] getReportFiles() {
107         return ReportUtils.getReportFiles(reportsDirectory);
108     }
109 
110     @Override
111     public boolean canGenerateReport() {
112         return getReportFiles().length > 0;
113     }
114 }