1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36 @Mojo(name = "list-phase", threadSafe = true)
37 public class ListPhaseMojo extends AbstractLifecycleMojo {
38
39
40 @Parameter(property = "buildplan.phase")
41 private String phase;
42
43
44 @Parameter(property = "buildplan.showLifecycles", defaultValue = "false")
45 private boolean showLifecycles;
46
47
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 }