1 package org.apache.maven.plugin.descriptor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.component.repository.ComponentDependency;
23 import org.codehaus.plexus.component.repository.ComponentRequirement;
24 import org.codehaus.plexus.configuration.PlexusConfiguration;
25 import org.codehaus.plexus.configuration.PlexusConfigurationException;
26 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
27 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
28 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.util.ArrayList;
33 import java.util.List;
34
35
36
37
38
39 public class PluginDescriptorBuilder
40 {
41 public PluginDescriptor build( Reader reader )
42 throws PlexusConfigurationException
43 {
44 return build( reader, null );
45 }
46
47 public PluginDescriptor build( Reader reader, String source )
48 throws PlexusConfigurationException
49 {
50 PlexusConfiguration c = buildConfiguration( reader );
51
52 PluginDescriptor pluginDescriptor = new PluginDescriptor();
53
54 pluginDescriptor.setSource( source );
55 pluginDescriptor.setGroupId( c.getChild( "groupId" ).getValue() );
56 pluginDescriptor.setArtifactId( c.getChild( "artifactId" ).getValue() );
57 pluginDescriptor.setVersion( c.getChild( "version" ).getValue() );
58 pluginDescriptor.setGoalPrefix( c.getChild( "goalPrefix" ).getValue() );
59
60 pluginDescriptor.setName( c.getChild( "name" ).getValue() );
61 pluginDescriptor.setDescription( c.getChild( "description" ).getValue() );
62
63 String isolatedRealm = c.getChild( "isolatedRealm" ).getValue();
64
65 if ( isolatedRealm != null )
66 {
67 pluginDescriptor.setIsolatedRealm( Boolean.valueOf( isolatedRealm ).booleanValue() );
68 }
69
70 String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
71
72 if ( inheritedByDefault != null )
73 {
74 pluginDescriptor.setInheritedByDefault( Boolean.valueOf( inheritedByDefault ).booleanValue() );
75 }
76
77
78
79
80
81 PlexusConfiguration[] mojoConfigurations = c.getChild( "mojos" ).getChildren( "mojo" );
82
83 for ( int i = 0; i < mojoConfigurations.length; i++ )
84 {
85 PlexusConfiguration component = mojoConfigurations[i];
86
87 MojoDescriptor mojoDescriptor = buildComponentDescriptor( component, pluginDescriptor );
88
89 pluginDescriptor.addMojo( mojoDescriptor );
90 }
91
92
93
94
95
96 PlexusConfiguration[] dependencyConfigurations = c.getChild( "dependencies" ).getChildren( "dependency" );
97
98 List dependencies = new ArrayList();
99
100 for ( int i = 0; i < dependencyConfigurations.length; i++ )
101 {
102 PlexusConfiguration d = dependencyConfigurations[i];
103
104 ComponentDependency cd = new ComponentDependency();
105
106 cd.setArtifactId( d.getChild( "artifactId" ).getValue() );
107
108 cd.setGroupId( d.getChild( "groupId" ).getValue() );
109
110 cd.setType( d.getChild( "type" ).getValue() );
111
112 cd.setVersion( d.getChild( "version" ).getValue() );
113
114 dependencies.add( cd );
115 }
116
117 pluginDescriptor.setDependencies( dependencies );
118
119 return pluginDescriptor;
120 }
121
122 public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor )
123 throws PlexusConfigurationException
124 {
125 MojoDescriptor mojo = new MojoDescriptor();
126 mojo.setPluginDescriptor( pluginDescriptor );
127
128 mojo.setGoal( c.getChild( "goal" ).getValue() );
129
130 mojo.setImplementation( c.getChild( "implementation" ).getValue() );
131
132 PlexusConfiguration langConfig = c.getChild( "language" );
133
134 if ( langConfig != null )
135 {
136 mojo.setLanguage( langConfig.getValue() );
137 }
138
139 PlexusConfiguration configuratorConfig = c.getChild( "configurator" );
140
141 if ( configuratorConfig != null )
142 {
143 mojo.setComponentConfigurator( configuratorConfig.getValue() );
144 }
145
146 PlexusConfiguration composerConfig = c.getChild( "composer" );
147
148 if ( composerConfig != null )
149 {
150 mojo.setComponentComposer( composerConfig.getValue() );
151 }
152
153 String since = c.getChild( "since" ).getValue();
154
155 if ( since != null )
156 {
157 mojo.setSince( since );
158 }
159
160 String phase = c.getChild( "phase" ).getValue();
161
162 if ( phase != null )
163 {
164 mojo.setPhase( phase );
165 }
166
167 String executePhase = c.getChild( "executePhase" ).getValue();
168
169 if ( executePhase != null )
170 {
171 mojo.setExecutePhase( executePhase );
172 }
173
174 String executeMojo = c.getChild( "executeGoal" ).getValue();
175
176 if ( executeMojo != null )
177 {
178 mojo.setExecuteGoal( executeMojo );
179 }
180
181 String executeLifecycle = c.getChild( "executeLifecycle" ).getValue();
182
183 if ( executeLifecycle != null )
184 {
185 mojo.setExecuteLifecycle( executeLifecycle );
186 }
187
188 mojo.setInstantiationStrategy( c.getChild( "instantiationStrategy" ).getValue() );
189
190 mojo.setDescription( c.getChild( "description" ).getValue() );
191
192 String dependencyResolution = c.getChild( "requiresDependencyResolution" ).getValue();
193
194 if ( dependencyResolution != null )
195 {
196 mojo.setDependencyResolutionRequired( dependencyResolution );
197 }
198
199 String directInvocationOnly = c.getChild( "requiresDirectInvocation" ).getValue();
200
201 if ( directInvocationOnly != null )
202 {
203 mojo.setDirectInvocationOnly( Boolean.valueOf( directInvocationOnly ).booleanValue() );
204 }
205
206 String requiresProject = c.getChild( "requiresProject" ).getValue();
207
208 if ( requiresProject != null )
209 {
210 mojo.setProjectRequired( Boolean.valueOf( requiresProject ).booleanValue() );
211 }
212
213 String requiresReports = c.getChild( "requiresReports" ).getValue();
214
215 if ( requiresReports != null )
216 {
217 mojo.setRequiresReports( Boolean.valueOf( requiresReports ).booleanValue() );
218 }
219
220 String aggregator = c.getChild( "aggregator" ).getValue();
221
222 if ( aggregator != null )
223 {
224 mojo.setAggregator( Boolean.valueOf( aggregator ).booleanValue() );
225 }
226
227 String requiresOnline = c.getChild( "requiresOnline" ).getValue();
228
229 if ( requiresOnline != null )
230 {
231 mojo.setOnlineRequired( Boolean.valueOf( requiresOnline ).booleanValue() );
232 }
233
234 String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
235
236 if ( inheritedByDefault != null )
237 {
238 mojo.setInheritedByDefault( Boolean.valueOf( inheritedByDefault ).booleanValue() );
239 }
240
241
242
243
244
245 PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" );
246
247 List parameters = new ArrayList();
248
249 for ( int i = 0; i < parameterConfigurations.length; i++ )
250 {
251 PlexusConfiguration d = parameterConfigurations[i];
252
253 Parameter parameter = new Parameter();
254
255 parameter.setName( d.getChild( "name" ).getValue() );
256
257 parameter.setAlias( d.getChild( "alias" ).getValue() );
258
259 parameter.setType( d.getChild( "type" ).getValue() );
260
261 String required = d.getChild( "required" ).getValue();
262
263 parameter.setRequired( Boolean.valueOf( required ).booleanValue() );
264
265 PlexusConfiguration editableConfig = d.getChild( "editable" );
266
267
268 if ( editableConfig != null )
269 {
270 String editable = d.getChild( "editable" ).getValue();
271
272 parameter.setEditable( editable == null || Boolean.valueOf( editable ).booleanValue() );
273 }
274
275 parameter.setDescription( d.getChild( "description" ).getValue() );
276
277 parameter.setDeprecated( d.getChild( "deprecated" ).getValue() );
278
279 parameter.setImplementation( d.getChild( "implementation" ).getValue() );
280
281 parameters.add( parameter );
282 }
283
284 mojo.setParameters( parameters );
285
286
287
288
289
290
291
292 mojo.setMojoConfiguration( c.getChild( "configuration" ) );
293
294
295
296
297
298
299
300
301 PlexusConfiguration[] requirements = c.getChild( "requirements" ).getChildren( "requirement" );
302
303 for ( int i = 0; i < requirements.length; i++ )
304 {
305 PlexusConfiguration requirement = requirements[i];
306
307 ComponentRequirement cr = new ComponentRequirement();
308
309 cr.setRole( requirement.getChild( "role" ).getValue() );
310
311 cr.setRoleHint( requirement.getChild( "role-hint" ).getValue() );
312
313 cr.setFieldName( requirement.getChild( "field-name" ).getValue() );
314
315 mojo.addRequirement( cr );
316 }
317
318 return mojo;
319 }
320
321
322
323
324
325 public PlexusConfiguration buildConfiguration( Reader configuration )
326 throws PlexusConfigurationException
327 {
328 try
329 {
330 return new XmlPlexusConfiguration( Xpp3DomBuilder.build( configuration ) );
331 }
332 catch ( IOException e )
333 {
334 throw new PlexusConfigurationException( "Error creating configuration", e );
335 }
336 catch ( XmlPullParserException e )
337 {
338 throw new PlexusConfigurationException( "Error creating configuration", e );
339 }
340 }
341 }