1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.lifecycle.internal;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.ArrayList;
26 import java.util.Comparator;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.TreeMap;
31 import java.util.stream.Collectors;
32
33 import org.apache.maven.execution.MavenSession;
34 import org.apache.maven.lifecycle.Lifecycle;
35 import org.apache.maven.lifecycle.LifecycleMappingDelegate;
36 import org.apache.maven.model.Plugin;
37 import org.apache.maven.model.PluginExecution;
38 import org.apache.maven.plugin.BuildPluginManager;
39 import org.apache.maven.plugin.InvalidPluginDescriptorException;
40 import org.apache.maven.plugin.MojoExecution;
41 import org.apache.maven.plugin.MojoNotFoundException;
42 import org.apache.maven.plugin.PluginDescriptorParsingException;
43 import org.apache.maven.plugin.PluginNotFoundException;
44 import org.apache.maven.plugin.PluginResolutionException;
45 import org.apache.maven.plugin.descriptor.MojoDescriptor;
46 import org.apache.maven.project.MavenProject;
47
48
49
50
51
52
53 @Named(DefaultLifecycleMappingDelegate.HINT)
54 @Singleton
55 public class DefaultLifecycleMappingDelegate implements LifecycleMappingDelegate {
56 public static final String HINT = "default";
57
58 private final BuildPluginManager pluginManager;
59
60 @Inject
61 public DefaultLifecycleMappingDelegate(BuildPluginManager pluginManager) {
62 this.pluginManager = pluginManager;
63 }
64
65 @Override
66 public Map<String, List<MojoExecution>> calculateLifecycleMappings(
67 MavenSession session, MavenProject project, Lifecycle lifecycle, String lifecyclePhase)
68 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
69 MojoNotFoundException, InvalidPluginDescriptorException {
70
71
72
73
74
75 Map<String, Map<PhaseId, List<MojoExecution>>> mappings =
76 new TreeMap<>(new PhaseComparator(lifecycle.getPhases()));
77
78 Map<String, String> aliases = lifecycle.getDelegate().aliases().stream()
79 .collect(Collectors.toMap(a -> a.v3Phase(), a -> a.v4Phase()));
80
81 if (aliases.containsKey(lifecyclePhase)) {
82 lifecyclePhase = PhaseId.of(aliases.get(lifecyclePhase)).phase();
83 }
84
85 for (String phase : lifecycle.getPhases()) {
86 Map<PhaseId, List<MojoExecution>> phaseBindings =
87 new TreeMap<>(Comparator.comparing(PhaseId::toString, new PhaseComparator(lifecycle.getPhases())));
88
89 mappings.put(phase, phaseBindings);
90
91 if (phase.equals(lifecyclePhase)) {
92 break;
93 }
94 }
95
96
97
98
99
100
101
102
103 for (Plugin plugin : project.getBuild().getPlugins()) {
104 for (PluginExecution execution : plugin.getExecutions()) {
105
106
107 String phase = execution.getPhase();
108 if (aliases.containsKey(phase)) {
109 phase = aliases.get(phase);
110 }
111 if (phase != null) {
112 Map<PhaseId, List<MojoExecution>> phaseBindings = getPhaseBindings(mappings, phase);
113 if (phaseBindings != null) {
114 for (String goal : execution.getGoals()) {
115 MojoExecution mojoExecution = new MojoExecution(plugin, goal, execution.getId());
116 mojoExecution.setLifecyclePhase(phase);
117 PhaseId phaseId = PhaseId.of(phase);
118 if (phaseId.priority() == 0) {
119 phaseId = PhaseId.of(phase + "[" + execution.getPriority() + "]");
120 }
121 addMojoExecution(phaseBindings, mojoExecution, phaseId);
122 }
123 }
124 }
125
126 else {
127 for (String goal : execution.getGoals()) {
128 MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor(
129 plugin, goal, project.getRemotePluginRepositories(), session.getRepositorySession());
130
131 phase = mojoDescriptor.getPhase();
132 if (aliases.containsKey(phase)) {
133 phase = aliases.get(phase);
134 }
135 Map<PhaseId, List<MojoExecution>> phaseBindings = getPhaseBindings(mappings, phase);
136 if (phaseBindings != null) {
137 MojoExecution mojoExecution = new MojoExecution(mojoDescriptor, execution.getId());
138 mojoExecution.setLifecyclePhase(phase);
139 PhaseId phaseId = PhaseId.of(phase + "[" + execution.getPriority() + "]");
140 addMojoExecution(phaseBindings, mojoExecution, phaseId);
141 }
142 }
143 }
144 }
145 }
146
147 Map<String, List<MojoExecution>> lifecycleMappings = new LinkedHashMap<>();
148
149 for (Map.Entry<String, Map<PhaseId, List<MojoExecution>>> entry : mappings.entrySet()) {
150 List<MojoExecution> mojoExecutions = new ArrayList<>();
151
152 for (List<MojoExecution> executions : entry.getValue().values()) {
153 mojoExecutions.addAll(executions);
154 }
155
156 lifecycleMappings.put(entry.getKey(), mojoExecutions);
157 }
158
159 return lifecycleMappings;
160 }
161
162 private Map<PhaseId, List<MojoExecution>> getPhaseBindings(
163 Map<String, Map<PhaseId, List<MojoExecution>>> mappings, String phase) {
164 if (phase != null) {
165 PhaseId id = PhaseId.of(phase);
166 return mappings.get(id.phase());
167 }
168 return null;
169 }
170
171 private void addMojoExecution(
172 Map<PhaseId, List<MojoExecution>> phaseBindings, MojoExecution mojoExecution, PhaseId phaseId) {
173 List<MojoExecution> mojoExecutions = phaseBindings.computeIfAbsent(phaseId, k -> new ArrayList<>());
174
175 mojoExecutions.add(mojoExecution);
176 }
177 }