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