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  
20  import java.util.Collection;
21  import java.util.Map;
22  import org.apache.maven.plugin.MojoExecution;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.Parameter;
26  import org.codehaus.mojo.buildplan.display.ListPluginTableDescriptor;
27  import org.codehaus.mojo.buildplan.display.MojoExecutionDisplay;
28  import org.codehaus.mojo.buildplan.display.TableDescriptor;
29  import org.codehaus.mojo.buildplan.util.Multimap;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /** List plugin executions by plugin for the current project. */
33  @Mojo(name = "list-plugin", threadSafe = true)
34  public class ListPluginMojo extends AbstractLifecycleMojo {
35  
36      /** Display plugin executions only for the specified plugin. */
37      @Parameter(property = "buildplan.plugin")
38      private String plugin;
39  
40      public void executeInternal() throws MojoFailureException {
41  
42          Multimap<String, MojoExecution> plan =
43                  Groups.ByPlugin.of(calculateExecutionPlan().getMojoExecutions(), plugin);
44  
45          if (plan.isEmpty()) {
46              getLog().warn("No plugin found with artifactId: " + plugin);
47          } else {
48              StringBuilder output = new StringBuilder();
49              TableDescriptor descriptor = ListPluginTableDescriptor.of(plan.values(), defaultLifecycles);
50              for (Map.Entry<String, Collection<MojoExecution>> executions :
51                      plan.asMap().entrySet()) {
52                  output.append(lineSeparator()).append(pluginTitleLine(descriptor, executions.getKey()));
53                  for (MojoExecution execution : executions.getValue()) {
54                      output.append(lineSeparator()).append(line(descriptor.rowFormat(), execution));
55                  }
56              }
57              handleOutput(output.toString());
58          }
59      }
60  
61      private String line(String rowFormat, MojoExecution execution) {
62  
63          MojoExecutionDisplay display = new MojoExecutionDisplay(execution);
64  
65          return String.format(rowFormat, display.getPhase(), display.getGoal(), display.getExecutionId());
66      }
67  
68      private String pluginTitleLine(TableDescriptor descriptor, String key) {
69          return key + " " + StringUtils.repeat("-", descriptor.width() - key.length());
70      }
71  }