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 java.util.Collections.emptyList;
19  import static java.util.Collections.emptyMap;
20  import static org.codehaus.mojo.buildplan.display.Output.lineSeparator;
21  
22  import java.util.Collection;
23  import java.util.Map;
24  import org.apache.maven.lifecycle.Lifecycle;
25  import org.apache.maven.plugin.MojoExecution;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.codehaus.mojo.buildplan.display.ListPhaseTableDescriptor;
30  import org.codehaus.mojo.buildplan.display.MojoExecutionDisplay;
31  import org.codehaus.mojo.buildplan.display.TableDescriptor;
32  import org.codehaus.mojo.buildplan.util.Multimap;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  /** List plugin executions by phase for the current project. */
36  @Mojo(name = "list-phase", threadSafe = true)
37  public class ListPhaseMojo extends AbstractLifecycleMojo {
38  
39      /** Display plugin executions only for the specified phase. */
40      @Parameter(property = "buildplan.phase")
41      private String phase;
42  
43      /** Will show in which lifecycle a phase was defined (if any) */
44      @Parameter(property = "buildplan.showLifecycles", defaultValue = "false")
45      private boolean showLifecycles;
46  
47      /** Will print all phases, even if no mapping to an execution is available */
48      @Parameter(property = "buildplan.showAllPhases", defaultValue = "false")
49      private boolean showAllPhases;
50  
51      public void executeInternal() throws MojoFailureException {
52          Groups.Options options = new Groups.Options(defaultLifecycles);
53          if (showAllPhases) {
54              options = options.showingAllPhases();
55          }
56          if (showLifecycles) {
57              options = options.showingLifecycles();
58          }
59  
60          Multimap<String, MojoExecution> phases =
61                  Groups.ByPhase.of(calculateExecutionPlan().getMojoExecutions(), options);
62  
63          if (!phases.isEmpty()) {
64              TableDescriptor descriptor = ListPhaseTableDescriptor.of(phases.values(), defaultLifecycles);
65              Lifecycle currentLifecycle = null;
66              StringBuilder output = new StringBuilder();
67              for (Map.Entry<String, Collection<MojoExecution>> currentPhase :
68                      phases.asMap().entrySet()) {
69                  if (showLifecycles) {
70                      Lifecycle lifecycleForPhase = defaultLifecycles.get(currentPhase.getKey());
71                      if (lifecycleForPhase == null) {
72                          lifecycleForPhase = new Lifecycle("", emptyList(), emptyMap());
73                      }
74                      if (!lifecycleForPhase.equals(currentLifecycle)) {
75                          currentLifecycle = lifecycleForPhase;
76                          output.append(lineSeparator())
77                                  .append(lineSeparator())
78                                  .append("[")
79                                  .append(currentLifecycle.getId())
80                                  .append("]");
81                      }
82                  }
83                  output.append(lineSeparator()).append(phaseTitleLine(descriptor, currentPhase.getKey()));
84                  currentPhase.getValue().stream()
85                          .filter(execution -> execution != NoMojoExecution.INSTANCE)
86                          .forEach(execution ->
87                                  output.append(lineSeparator()).append(line(descriptor.rowFormat(), execution)));
88              }
89              handleOutput(output.toString());
90          } else {
91              getLog().warn("No plugin execution found within phase: " + phase);
92          }
93      }
94  
95      private String line(String rowFormat, MojoExecution execution) {
96          MojoExecutionDisplay display = new MojoExecutionDisplay(execution);
97  
98          return String.format(rowFormat, display.getArtifactId(), display.getGoal(), display.getExecutionId());
99      }
100 
101     private String phaseTitleLine(TableDescriptor descriptor, String key) {
102         return key + " " + StringUtils.repeat("-", descriptor.width() - key.length());
103     }
104 }