View Javadoc
1   /*
2    * Copyright (C) 2012 Jean-Christophe Gay (contact@jeanchristophegay.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.buildplan;
17  
18  import static org.codehaus.mojo.buildplan.display.TableColumn.ARTIFACT_ID;
19  import static org.codehaus.mojo.buildplan.display.TableColumn.EXECUTION_ID;
20  import static org.codehaus.mojo.buildplan.display.TableColumn.GOAL;
21  import static org.codehaus.mojo.buildplan.display.TableColumn.LIFECYCLE;
22  import static org.codehaus.mojo.buildplan.display.TableColumn.PHASE;
23  
24  import java.util.Locale;
25  import org.apache.maven.doxia.sink.Sink;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.lifecycle.DefaultLifecycles;
28  import org.apache.maven.lifecycle.LifecycleExecutor;
29  import org.apache.maven.lifecycle.MavenExecutionPlan;
30  import org.apache.maven.plugin.MojoExecution;
31  import org.apache.maven.plugin.MojoFailureException;
32  import org.apache.maven.plugin.logging.Log;
33  import org.apache.maven.plugins.annotations.Component;
34  import org.apache.maven.plugins.annotations.LifecyclePhase;
35  import org.apache.maven.plugins.annotations.Mojo;
36  import org.apache.maven.plugins.annotations.Parameter;
37  import org.apache.maven.reporting.AbstractMavenReport;
38  import org.apache.maven.reporting.MavenReportException;
39  import org.codehaus.mojo.buildplan.display.MojoExecutionDisplay;
40  import org.codehaus.mojo.buildplan.display.PlainTextMojoExecutionDisplay;
41  import org.codehaus.mojo.buildplan.display.TableColumn;
42  
43  /** Report plugin executions for the current project. */
44  @Mojo(name = "report", defaultPhase = LifecyclePhase.SITE)
45  public class ReportMojo extends AbstractMavenReport {
46  
47      @Component
48      private LifecycleExecutor lifecycleExecutor;
49  
50      @Component(role = DefaultLifecycles.class)
51      DefaultLifecycles defaultLifecycles;
52  
53      @Parameter(defaultValue = "${session}", readonly = true)
54      private MavenSession session;
55  
56      @Override
57      protected void executeReport(Locale locale) throws MavenReportException {
58  
59          MavenExecutionPlan plan;
60          try {
61              plan = calculateExecutionPlan();
62          } catch (MojoFailureException e) {
63              throw new MavenReportException("Cannot calculate execution plan...", e);
64          }
65  
66          Log logger = getLog();
67          logger.info(
68                  "Generating " + getOutputName() + ".html" + " for " + project.getName() + " " + project.getVersion());
69  
70          // Get the Maven Doxia Sink, which will be used to generate the
71          // various elements of the document
72          Sink mainSink = getSink();
73          if (mainSink == null) {
74              throw new MavenReportException("Could not get the Doxia sink");
75          }
76  
77          // Page title
78          mainSink.head();
79          mainSink.title();
80          mainSink.text("Build Plan for " + project.getName() + " " + project.getVersion());
81          mainSink.title_();
82          mainSink.head_();
83  
84          mainSink.body();
85  
86          mainSink.header();
87          mainSink.rawText("<h1 id=\"titleContent\">" + getDescription(locale) + "</h1>");
88          mainSink.header_();
89  
90          mainSink.table();
91          mainSink.tableRows(null, false);
92  
93          mainSink.tableRow();
94          tableHead(mainSink, LIFECYCLE);
95          tableHead(mainSink, PHASE);
96          tableHead(mainSink, ARTIFACT_ID);
97          tableHead(mainSink, GOAL);
98          tableHead(mainSink, EXECUTION_ID);
99          mainSink.tableRow_();
100 
101         for (MojoExecution execution : plan.getMojoExecutions()) {
102             MojoExecutionDisplay display = new PlainTextMojoExecutionDisplay(execution);
103             mainSink.tableRow();
104             tableCell(mainSink, display.getLifecycle(defaultLifecycles));
105             tableCell(mainSink, display.getPhase());
106             tableCell(mainSink, display.getArtifactId());
107             tableCell(mainSink, display.getGoal());
108             tableCell(mainSink, display.getExecutionId());
109             mainSink.tableRow_();
110         }
111 
112         mainSink.tableRows_();
113         mainSink.table_();
114 
115         mainSink.body_();
116     }
117 
118     private void tableCell(Sink mainSink, String content) {
119         mainSink.tableCell();
120         mainSink.text(content);
121         mainSink.tableCell_();
122     }
123 
124     private void tableHead(Sink mainSink, TableColumn lifecycle) {
125         mainSink.tableHeaderCell();
126         mainSink.text(lifecycle.title());
127         mainSink.tableHeaderCell_();
128     }
129 
130     @Override
131     public String getOutputName() {
132         return "buildplan-report";
133     }
134 
135     @Override
136     public String getName(Locale locale) {
137         return "Build Plan";
138     }
139 
140     @Override
141     public String getDescription(Locale locale) {
142         return "List plugin executions for " + currentProject();
143     }
144 
145     private String currentProject() {
146         return session.getCurrentProject().getGroupId()
147                 + ":"
148                 + session.getCurrentProject().getArtifactId();
149     }
150 
151     protected MavenExecutionPlan calculateExecutionPlan() throws MojoFailureException {
152         try {
153             return lifecycleExecutor.calculateExecutionPlan(session, "deploy");
154         } catch (Exception e) {
155             throw new MojoFailureException(
156                     String.format("Cannot calculate Maven execution plan, caused by: %s", e.getMessage()), e);
157         }
158     }
159 }