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.Output.lineSeparator;
19  import static org.codehaus.mojo.buildplan.display.TableColumn.ARTIFACT_ID;
20  import static org.codehaus.mojo.buildplan.display.TableColumn.EXECUTION_ID;
21  import static org.codehaus.mojo.buildplan.display.TableColumn.GOAL;
22  import static org.codehaus.mojo.buildplan.display.TableColumn.LIFECYCLE;
23  import static org.codehaus.mojo.buildplan.display.TableColumn.PHASE;
24  import static org.codehaus.mojo.buildplan.display.TableColumn.VERSION;
25  
26  import org.apache.maven.lifecycle.MavenExecutionPlan;
27  import org.apache.maven.plugin.MojoExecution;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.codehaus.mojo.buildplan.display.ListTableDescriptor;
32  import org.codehaus.mojo.buildplan.display.MojoExecutionDisplay;
33  import org.codehaus.mojo.buildplan.display.TableDescriptor;
34  import org.codehaus.plexus.util.StringUtils;
35  
36  /** List plugin executions for the current project. */
37  @Mojo(name = "list", threadSafe = true)
38  public class ListMojo extends AbstractLifecycleMojo {
39  
40      /** Will show in which lifecycle a phase was defined (if any) */
41      @Parameter(property = "buildplan.showLifecycles", defaultValue = "false")
42      private boolean showLifecycles;
43  
44      public void executeInternal() throws MojoFailureException {
45  
46          MavenExecutionPlan plan = calculateExecutionPlan();
47  
48          ListTableDescriptor descriptor = ListTableDescriptor.of(plan.getMojoExecutions(), defaultLifecycles);
49          if (!showLifecycles) {
50              descriptor.hideLifecycle();
51          }
52          String row = descriptor.rowFormat();
53          String head = descriptor.titleFormat();
54  
55          StringBuilder output = new StringBuilder()
56                  .append(lineSeparator())
57                  .append(titleSeparator(descriptor))
58                  .append(lineSeparator())
59                  .append(tableHead(head))
60                  .append(lineSeparator())
61                  .append(titleSeparator(descriptor));
62  
63          for (MojoExecution execution : plan.getMojoExecutions()) {
64              output.append(lineSeparator()).append(tableRow(row, execution));
65          }
66  
67          handleOutput(output.toString());
68      }
69  
70      private String tableRow(String row, MojoExecution execution) {
71  
72          MojoExecutionDisplay display = new MojoExecutionDisplay(execution);
73  
74          if (showLifecycles) {
75              return String.format(
76                      row,
77                      display.getLifecycle(defaultLifecycles),
78                      display.getPhase(),
79                      display.getArtifactId(),
80                      display.getVersion(),
81                      display.getGoal(),
82                      display.getExecutionId());
83          } else {
84              return String.format(
85                      row,
86                      display.getPhase(),
87                      display.getArtifactId(),
88                      display.getVersion(),
89                      display.getGoal(),
90                      display.getExecutionId());
91          }
92      }
93  
94      private String tableHead(String row) {
95          if (showLifecycles) {
96              return String.format(
97                      row,
98                      LIFECYCLE.title(),
99                      PHASE.title(),
100                     ARTIFACT_ID.title(),
101                     VERSION.title(),
102                     GOAL.title(),
103                     EXECUTION_ID.title());
104         } else {
105             return String.format(
106                     row, PHASE.title(), ARTIFACT_ID.title(), VERSION.title(), GOAL.title(), EXECUTION_ID.title());
107         }
108     }
109 
110     private String titleSeparator(TableDescriptor descriptor) {
111         return StringUtils.repeat("-", descriptor.width());
112     }
113 }