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