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