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