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.HashSet;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.maven.lifecycle.DefaultLifecycles;
32 import org.apache.maven.lifecycle.LifeCyclePluginAnalyzer;
33 import org.apache.maven.lifecycle.Lifecycle;
34 import org.apache.maven.lifecycle.mapping.LifecycleMapping;
35 import org.apache.maven.lifecycle.mapping.LifecycleMojo;
36 import org.apache.maven.lifecycle.mapping.LifecyclePhase;
37 import org.apache.maven.model.InputLocation;
38 import org.apache.maven.model.InputSource;
39 import org.apache.maven.model.Plugin;
40 import org.apache.maven.model.PluginExecution;
41 import org.codehaus.plexus.util.StringUtils;
42 import org.codehaus.plexus.util.xml.Xpp3Dom;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47
48
49
50
51
52
53
54
55 @Singleton
56 @Named
57 public class DefaultLifecyclePluginAnalyzer implements LifeCyclePluginAnalyzer {
58
59 private final Logger logger = LoggerFactory.getLogger(getClass());
60
61 @Inject
62 private Map<String, LifecycleMapping> lifecycleMappings;
63
64 @Inject
65 private DefaultLifecycles defaultLifeCycles;
66
67 public DefaultLifecyclePluginAnalyzer() {}
68
69
70
71
72
73
74
75
76
77
78
79 public Set<Plugin> getPluginsBoundByDefaultToAllLifecycles(String packaging) {
80 if (logger.isDebugEnabled()) {
81 logger.debug("Looking up lifecycle mappings for packaging " + packaging + " from "
82 + Thread.currentThread().getContextClassLoader());
83 }
84
85
86
87 LifecycleMapping lifecycleMappingForPackaging = lifecycleMappings.get(packaging);
88
89 if (lifecycleMappingForPackaging == null) {
90 return null;
91 }
92
93 Map<Plugin, Plugin> plugins = new LinkedHashMap<>();
94
95 for (Lifecycle lifecycle : defaultLifeCycles.getLifeCycles()) {
96 org.apache.maven.lifecycle.mapping.Lifecycle lifecycleConfiguration =
97 lifecycleMappingForPackaging.getLifecycles().get(lifecycle.getId());
98
99 Map<String, LifecyclePhase> phaseToGoalMapping = null;
100
101 if (lifecycleConfiguration != null) {
102 phaseToGoalMapping = lifecycleConfiguration.getLifecyclePhases();
103 } else if (lifecycle.getDefaultLifecyclePhases() != null) {
104 phaseToGoalMapping = lifecycle.getDefaultLifecyclePhases();
105 }
106
107 if (phaseToGoalMapping != null) {
108 for (Map.Entry<String, LifecyclePhase> goalsForLifecyclePhase : phaseToGoalMapping.entrySet()) {
109 String phase = goalsForLifecyclePhase.getKey();
110 LifecyclePhase goals = goalsForLifecyclePhase.getValue();
111 if (goals != null) {
112 parseLifecyclePhaseDefinitions(plugins, phase, goals);
113 }
114 }
115 }
116 }
117
118 return plugins.keySet();
119 }
120
121 private void parseLifecyclePhaseDefinitions(Map<Plugin, Plugin> plugins, String phase, LifecyclePhase goals) {
122 String modelId = "org.apache.maven:maven-core:"
123 + this.getClass().getPackage().getImplementationVersion() + ":default-lifecycle-bindings";
124 InputSource inputSource = new InputSource();
125 inputSource.setModelId(modelId);
126 InputLocation location = new InputLocation(-1, -1, inputSource);
127 location.setLocation(0, location);
128
129 List<LifecycleMojo> mojos = goals.getMojos();
130 if (mojos != null) {
131
132 for (int i = 0; i < mojos.size(); i++) {
133 LifecycleMojo mojo = mojos.get(i);
134
135 GoalSpec gs = parseGoalSpec(mojo.getGoal());
136
137 if (gs == null) {
138 logger.warn("Ignored invalid goal specification '" + mojo.getGoal()
139 + "' from lifecycle mapping for phase " + phase);
140 continue;
141 }
142
143 Plugin plugin = new Plugin();
144 plugin.setGroupId(gs.groupId);
145 plugin.setArtifactId(gs.artifactId);
146 plugin.setVersion(gs.version);
147
148 plugin.setLocation("", location);
149 plugin.setLocation("groupId", location);
150 plugin.setLocation("artifactId", location);
151 plugin.setLocation("version", location);
152
153 Plugin existing = plugins.get(plugin);
154 if (existing != null) {
155 if (existing.getVersion() == null) {
156 existing.setVersion(plugin.getVersion());
157 existing.setLocation("version", location);
158 }
159 plugin = existing;
160 } else {
161 plugins.put(plugin, plugin);
162 }
163
164 PluginExecution execution = new PluginExecution();
165 execution.setId(getExecutionId(plugin, gs.goal));
166 execution.setPhase(phase);
167 execution.setPriority(i - mojos.size());
168 execution.getGoals().add(gs.goal);
169
170 execution.setLocation("", location);
171 execution.setLocation("id", location);
172 execution.setLocation("phase", location);
173 execution.setLocation("goals", location);
174
175 Xpp3Dom lifecycleConfiguration = mojo.getConfiguration();
176 if (lifecycleConfiguration != null) {
177 execution.setConfiguration(new Xpp3Dom(lifecycleConfiguration));
178 }
179
180 plugin.setDependencies(mojo.getDependencies());
181 plugin.getExecutions().add(execution);
182 }
183 }
184 }
185
186 private GoalSpec parseGoalSpec(String goalSpec) {
187 GoalSpec gs = new GoalSpec();
188
189 String[] p = StringUtils.split(goalSpec.trim(), ":");
190
191 if (p.length == 3) {
192
193 gs.groupId = p[0];
194 gs.artifactId = p[1];
195 gs.goal = p[2];
196 } else if (p.length == 4) {
197
198 gs.groupId = p[0];
199 gs.artifactId = p[1];
200 gs.version = p[2];
201 gs.goal = p[3];
202 } else {
203
204 gs = null;
205 }
206
207 return gs;
208 }
209
210 private String getExecutionId(Plugin plugin, String goal) {
211 Set<String> existingIds = new HashSet<>();
212 for (PluginExecution execution : plugin.getExecutions()) {
213 existingIds.add(execution.getId());
214 }
215
216 String base = "default-" + goal;
217 String id = base;
218
219 for (int index = 1; existingIds.contains(id); index++) {
220 id = base + '-' + index;
221 }
222
223 return id;
224 }
225
226 static class GoalSpec {
227
228 String groupId;
229
230 String artifactId;
231
232 String version;
233
234 String goal;
235 }
236 }