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