1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.descriptor;
20
21 import javax.xml.stream.XMLStreamException;
22 import javax.xml.stream.XMLStreamReader;
23
24 import java.io.InputStream;
25 import java.io.Reader;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Optional;
29
30 import com.ctc.wstx.stax.WstxInputFactory;
31 import org.apache.maven.internal.xml.XmlNodeBuilder;
32 import org.apache.maven.internal.xml.XmlPlexusConfiguration;
33 import org.codehaus.plexus.component.repository.ComponentDependency;
34 import org.codehaus.plexus.component.repository.ComponentRequirement;
35 import org.codehaus.plexus.configuration.PlexusConfiguration;
36 import org.codehaus.plexus.configuration.PlexusConfigurationException;
37
38
39
40 public class PluginDescriptorBuilder {
41 public PluginDescriptor build(Reader reader) throws PlexusConfigurationException {
42 return build(reader, null);
43 }
44
45 public PluginDescriptor build(Reader reader, String source) throws PlexusConfigurationException {
46 return build(source, buildConfiguration(reader));
47 }
48
49 public PluginDescriptor build(InputStream input, String source) throws PlexusConfigurationException {
50 return build(source, buildConfiguration(input));
51 }
52
53 private PluginDescriptor build(String source, PlexusConfiguration c) throws PlexusConfigurationException {
54 PluginDescriptor pluginDescriptor = new PluginDescriptor();
55
56 pluginDescriptor.setSource(source);
57 pluginDescriptor.setGroupId(extractGroupId(c));
58 pluginDescriptor.setArtifactId(extractArtifactId(c));
59 pluginDescriptor.setVersion(extractVersion(c));
60 pluginDescriptor.setGoalPrefix(extractGoalPrefix(c));
61
62 pluginDescriptor.setName(extractName(c));
63 pluginDescriptor.setDescription(extractDescription(c));
64
65 pluginDescriptor.setIsolatedRealm(extractIsolatedRealm(c));
66 pluginDescriptor.setInheritedByDefault(extractInheritedByDefault(c));
67 pluginDescriptor.setRequiredJavaVersion(extractRequiredJavaVersion(c).orElse(null));
68 pluginDescriptor.setRequiredMavenVersion(extractRequiredMavenVersion(c).orElse(null));
69
70 pluginDescriptor.addMojos(extractMojos(c, pluginDescriptor));
71
72 pluginDescriptor.setDependencies(extractComponentDependencies(c));
73
74 return pluginDescriptor;
75 }
76
77 private String extractGroupId(PlexusConfiguration c) {
78 return c.getChild("groupId").getValue();
79 }
80
81 private String extractArtifactId(PlexusConfiguration c) {
82 return c.getChild("artifactId").getValue();
83 }
84
85 private String extractVersion(PlexusConfiguration c) {
86 return c.getChild("version").getValue();
87 }
88
89 private String extractGoalPrefix(PlexusConfiguration c) {
90 return c.getChild("goalPrefix").getValue();
91 }
92
93 private String extractName(PlexusConfiguration c) {
94 return c.getChild("name").getValue();
95 }
96
97 private String extractDescription(PlexusConfiguration c) {
98 return c.getChild("description").getValue();
99 }
100
101 private List<MojoDescriptor> extractMojos(PlexusConfiguration c, PluginDescriptor pluginDescriptor)
102 throws PlexusConfigurationException {
103 List<MojoDescriptor> mojos = new ArrayList<>();
104
105 PlexusConfiguration[] mojoConfigurations = c.getChild("mojos").getChildren("mojo");
106
107 for (PlexusConfiguration component : mojoConfigurations) {
108 mojos.add(buildComponentDescriptor(component, pluginDescriptor));
109 }
110 return mojos;
111 }
112
113 private boolean extractInheritedByDefault(PlexusConfiguration c) {
114 String inheritedByDefault = c.getChild("inheritedByDefault").getValue();
115
116 if (inheritedByDefault != null) {
117 return Boolean.parseBoolean(inheritedByDefault);
118 }
119 return false;
120 }
121
122 private boolean extractIsolatedRealm(PlexusConfiguration c) {
123 String isolatedRealm = c.getChild("isolatedRealm").getValue();
124
125 if (isolatedRealm != null) {
126 return Boolean.parseBoolean(isolatedRealm);
127 }
128 return false;
129 }
130
131 private Optional<String> extractRequiredJavaVersion(PlexusConfiguration c) {
132 return Optional.ofNullable(c.getChild("requiredJavaVersion")).map(PlexusConfiguration::getValue);
133 }
134
135 private Optional<String> extractRequiredMavenVersion(PlexusConfiguration c) {
136 return Optional.ofNullable(c.getChild("requiredMavenVersion")).map(PlexusConfiguration::getValue);
137 }
138
139 private List<ComponentDependency> extractComponentDependencies(PlexusConfiguration c) {
140
141 PlexusConfiguration[] dependencyConfigurations =
142 c.getChild("dependencies").getChildren("dependency");
143
144 List<ComponentDependency> dependencies = new ArrayList<>();
145
146 for (PlexusConfiguration d : dependencyConfigurations) {
147 dependencies.add(extractComponentDependency(d));
148 }
149 return dependencies;
150 }
151
152 private ComponentDependency extractComponentDependency(PlexusConfiguration d) {
153 ComponentDependency cd = new ComponentDependency();
154
155 cd.setArtifactId(extractArtifactId(d));
156
157 cd.setGroupId(extractGroupId(d));
158
159 cd.setType(d.getChild("type").getValue());
160
161 cd.setVersion(extractVersion(d));
162 return cd;
163 }
164
165 @SuppressWarnings("checkstyle:methodlength")
166 public MojoDescriptor buildComponentDescriptor(PlexusConfiguration c, PluginDescriptor pluginDescriptor)
167 throws PlexusConfigurationException {
168 MojoDescriptor mojo = new MojoDescriptor();
169 mojo.setPluginDescriptor(pluginDescriptor);
170
171 mojo.setGoal(c.getChild("goal").getValue());
172
173 mojo.setImplementation(c.getChild("implementation").getValue());
174
175 PlexusConfiguration langConfig = c.getChild("language");
176
177 if (langConfig != null) {
178 mojo.setLanguage(langConfig.getValue());
179 }
180
181 PlexusConfiguration configuratorConfig = c.getChild("configurator");
182
183 if (configuratorConfig != null) {
184 mojo.setComponentConfigurator(configuratorConfig.getValue());
185 }
186
187 PlexusConfiguration composerConfig = c.getChild("composer");
188
189 if (composerConfig != null) {
190 mojo.setComponentComposer(composerConfig.getValue());
191 }
192
193 String since = c.getChild("since").getValue();
194
195 if (since != null) {
196 mojo.setSince(since);
197 }
198
199 PlexusConfiguration deprecated = c.getChild("deprecated", false);
200
201 if (deprecated != null) {
202 mojo.setDeprecated(deprecated.getValue());
203 }
204
205 String phase = c.getChild("phase").getValue();
206
207 if (phase != null) {
208 mojo.setPhase(phase);
209 }
210
211 String executePhase = c.getChild("executePhase").getValue();
212
213 if (executePhase != null) {
214 mojo.setExecutePhase(executePhase);
215 }
216
217 String executeMojo = c.getChild("executeGoal").getValue();
218
219 if (executeMojo != null) {
220 mojo.setExecuteGoal(executeMojo);
221 }
222
223 String executeLifecycle = c.getChild("executeLifecycle").getValue();
224
225 if (executeLifecycle != null) {
226 mojo.setExecuteLifecycle(executeLifecycle);
227 }
228
229 mojo.setInstantiationStrategy(c.getChild("instantiationStrategy").getValue());
230
231 mojo.setDescription(extractDescription(c));
232
233 PlexusConfiguration dependencyResolution = c.getChild("requiresDependencyResolution", false);
234
235 if (dependencyResolution != null) {
236 mojo.setDependencyResolutionRequired(dependencyResolution.getValue());
237 }
238
239 PlexusConfiguration dependencyCollection = c.getChild("requiresDependencyCollection", false);
240
241 if (dependencyCollection != null) {
242 mojo.setDependencyCollectionRequired(dependencyCollection.getValue());
243 }
244
245 String directInvocationOnly = c.getChild("requiresDirectInvocation").getValue();
246
247 if (directInvocationOnly != null) {
248 mojo.setDirectInvocationOnly(Boolean.parseBoolean(directInvocationOnly));
249 }
250
251 String requiresProject = c.getChild("requiresProject").getValue();
252
253 if (requiresProject != null) {
254 mojo.setProjectRequired(Boolean.parseBoolean(requiresProject));
255 }
256
257 String requiresReports = c.getChild("requiresReports").getValue();
258
259 if (requiresReports != null) {
260 mojo.setRequiresReports(Boolean.parseBoolean(requiresReports));
261 }
262
263 String aggregator = c.getChild("aggregator").getValue();
264
265 if (aggregator != null) {
266 mojo.setAggregator(Boolean.parseBoolean(aggregator));
267 }
268
269 String requiresOnline = c.getChild("requiresOnline").getValue();
270
271 if (requiresOnline != null) {
272 mojo.setOnlineRequired(Boolean.parseBoolean(requiresOnline));
273 }
274
275 String inheritedByDefault = c.getChild("inheritedByDefault").getValue();
276
277 if (inheritedByDefault != null) {
278 mojo.setInheritedByDefault(Boolean.parseBoolean(inheritedByDefault));
279 }
280
281 String threadSafe = c.getChild("threadSafe").getValue();
282
283 if (threadSafe != null) {
284 mojo.setThreadSafe(Boolean.parseBoolean(threadSafe));
285 }
286
287 String v4Api = c.getChild("v4Api").getValue();
288
289 if (v4Api != null) {
290 mojo.setV4Api(Boolean.parseBoolean(v4Api));
291 }
292
293
294
295
296
297 PlexusConfiguration mojoConfig = c.getChild("configuration");
298 mojo.setMojoConfiguration(mojoConfig);
299
300
301
302
303
304 PlexusConfiguration[] parameterConfigurations = c.getChild("parameters").getChildren("parameter");
305
306 List<Parameter> parameters = new ArrayList<>();
307
308 for (PlexusConfiguration d : parameterConfigurations) {
309 Parameter parameter = new Parameter();
310
311 parameter.setName(extractName(d));
312
313 parameter.setAlias(d.getChild("alias").getValue());
314
315 parameter.setType(d.getChild("type").getValue());
316
317 String required = d.getChild("required").getValue();
318
319 parameter.setRequired(Boolean.parseBoolean(required));
320
321 PlexusConfiguration editableConfig = d.getChild("editable");
322
323
324 if (editableConfig != null) {
325 String editable = d.getChild("editable").getValue();
326
327 parameter.setEditable(editable == null || Boolean.parseBoolean(editable));
328 }
329
330 parameter.setDescription(extractDescription(d));
331
332 parameter.setDeprecated(d.getChild("deprecated").getValue());
333
334 parameter.setImplementation(d.getChild("implementation").getValue());
335
336 parameter.setSince(d.getChild("since").getValue());
337
338 PlexusConfiguration paramConfig = mojoConfig.getChild(parameter.getName(), false);
339 if (paramConfig != null) {
340 parameter.setExpression(paramConfig.getValue(null));
341 parameter.setDefaultValue(paramConfig.getAttribute("default-value"));
342 }
343
344 parameters.add(parameter);
345 }
346
347 mojo.setParameters(parameters);
348
349
350
351
352
353
354
355 PlexusConfiguration[] requirements = c.getChild("requirements").getChildren("requirement");
356
357 for (PlexusConfiguration requirement : requirements) {
358 ComponentRequirement cr = new ComponentRequirement();
359
360 cr.setRole(requirement.getChild("role").getValue());
361
362 cr.setRoleHint(requirement.getChild("role-hint").getValue());
363
364 cr.setFieldName(requirement.getChild("field-name").getValue());
365
366 mojo.addRequirement(cr);
367 }
368
369 return mojo;
370 }
371
372
373
374
375
376 public PlexusConfiguration buildConfiguration(Reader configuration) throws PlexusConfigurationException {
377 try {
378 XMLStreamReader reader = WstxInputFactory.newFactory().createXMLStreamReader(configuration);
379 return XmlPlexusConfiguration.toPlexusConfiguration(XmlNodeBuilder.build(reader, true, null));
380 } catch (XMLStreamException e) {
381 throw new PlexusConfigurationException(e.getMessage(), e);
382 }
383 }
384
385 public PlexusConfiguration buildConfiguration(InputStream configuration) throws PlexusConfigurationException {
386 try {
387 XMLStreamReader reader = WstxInputFactory.newFactory().createXMLStreamReader(configuration);
388 return XmlPlexusConfiguration.toPlexusConfiguration(XmlNodeBuilder.build(reader, true, null));
389 } catch (XMLStreamException e) {
390 throw new PlexusConfigurationException(e.getMessage(), e);
391 }
392 }
393 }