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