View Javadoc

1   package org.apache.maven.plugin.descriptor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.codehaus.plexus.component.repository.ComponentDependency;
28  import org.codehaus.plexus.component.repository.ComponentRequirement;
29  import org.codehaus.plexus.configuration.PlexusConfiguration;
30  import org.codehaus.plexus.configuration.PlexusConfigurationException;
31  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
32  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
33  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34  
35  /**
36   * @author Jason van Zyl
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          PlexusConfiguration c = buildConfiguration( reader );
50  
51          PluginDescriptor pluginDescriptor = new PluginDescriptor();
52  
53          pluginDescriptor.setSource( source );
54          pluginDescriptor.setGroupId( c.getChild( "groupId" ).getValue() );
55          pluginDescriptor.setArtifactId( c.getChild( "artifactId" ).getValue() );
56          pluginDescriptor.setVersion( c.getChild( "version" ).getValue() );
57          pluginDescriptor.setGoalPrefix( c.getChild( "goalPrefix" ).getValue() );
58  
59          pluginDescriptor.setName( c.getChild( "name" ).getValue() );
60          pluginDescriptor.setDescription( c.getChild( "description" ).getValue() );
61  
62          String isolatedRealm = c.getChild( "isolatedRealm" ).getValue();
63  
64          if ( isolatedRealm != null )
65          {
66              pluginDescriptor.setIsolatedRealm( Boolean.parseBoolean( isolatedRealm ) );
67          }
68  
69          String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
70  
71          if ( inheritedByDefault != null )
72          {
73              pluginDescriptor.setInheritedByDefault( Boolean.parseBoolean( inheritedByDefault ) );
74          }
75  
76          // ----------------------------------------------------------------------
77          // Components
78          // ----------------------------------------------------------------------
79  
80          PlexusConfiguration[] mojoConfigurations = c.getChild( "mojos" ).getChildren( "mojo" );
81  
82          for ( PlexusConfiguration component : mojoConfigurations )
83          {
84              MojoDescriptor mojoDescriptor = buildComponentDescriptor( component, pluginDescriptor );
85  
86              pluginDescriptor.addMojo( mojoDescriptor );
87          }
88  
89          // ----------------------------------------------------------------------
90          // Dependencies
91          // ----------------------------------------------------------------------
92  
93          PlexusConfiguration[] dependencyConfigurations = c.getChild( "dependencies" ).getChildren( "dependency" );
94  
95          List<ComponentDependency> dependencies = new ArrayList<ComponentDependency>();
96  
97          for ( PlexusConfiguration d : dependencyConfigurations )
98          {
99              ComponentDependency cd = new ComponentDependency();
100 
101             cd.setArtifactId( d.getChild( "artifactId" ).getValue() );
102 
103             cd.setGroupId( d.getChild( "groupId" ).getValue() );
104 
105             cd.setType( d.getChild( "type" ).getValue() );
106 
107             cd.setVersion( d.getChild( "version" ).getValue() );
108 
109             dependencies.add( cd );
110         }
111 
112         pluginDescriptor.setDependencies( dependencies );
113 
114         return pluginDescriptor;
115     }
116 
117     public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor )
118         throws PlexusConfigurationException
119     {
120         MojoDescriptor mojo = new MojoDescriptor();
121         mojo.setPluginDescriptor( pluginDescriptor );
122 
123         mojo.setGoal( c.getChild( "goal" ).getValue() );
124 
125         mojo.setImplementation( c.getChild( "implementation" ).getValue() );
126 
127         PlexusConfiguration langConfig = c.getChild( "language" );
128 
129         if ( langConfig != null )
130         {
131             mojo.setLanguage( langConfig.getValue() );
132         }
133 
134         PlexusConfiguration configuratorConfig = c.getChild( "configurator" );
135 
136         if ( configuratorConfig != null )
137         {
138             mojo.setComponentConfigurator( configuratorConfig.getValue() );
139         }
140 
141         PlexusConfiguration composerConfig = c.getChild( "composer" );
142 
143         if ( composerConfig != null )
144         {
145             mojo.setComponentComposer( composerConfig.getValue() );
146         }
147 
148         String since = c.getChild( "since" ).getValue();
149 
150         if ( since != null )
151         {
152             mojo.setSince( since );
153         }
154 
155         PlexusConfiguration deprecated = c.getChild( "deprecated", false );
156 
157         if ( deprecated != null )
158         {
159             mojo.setDeprecated( deprecated.getValue() );
160         }
161 
162         String phase = c.getChild( "phase" ).getValue();
163 
164         if ( phase != null )
165         {
166             mojo.setPhase( phase );
167         }
168 
169         String executePhase = c.getChild( "executePhase" ).getValue();
170 
171         if ( executePhase != null )
172         {
173             mojo.setExecutePhase( executePhase );
174         }
175 
176         String executeMojo = c.getChild( "executeGoal" ).getValue();
177 
178         if ( executeMojo != null )
179         {
180             mojo.setExecuteGoal( executeMojo );
181         }
182 
183         String executeLifecycle = c.getChild( "executeLifecycle" ).getValue();
184 
185         if ( executeLifecycle != null )
186         {
187             mojo.setExecuteLifecycle( executeLifecycle );
188         }
189 
190         mojo.setInstantiationStrategy( c.getChild( "instantiationStrategy" ).getValue() );
191 
192         mojo.setDescription( c.getChild( "description" ).getValue() );
193 
194         PlexusConfiguration dependencyResolution = c.getChild( "requiresDependencyResolution", false );
195 
196         if ( dependencyResolution != null )
197         {
198             mojo.setDependencyResolutionRequired( dependencyResolution.getValue() );
199         }
200 
201         PlexusConfiguration dependencyCollection = c.getChild( "requiresDependencyCollection", false );
202 
203         if ( dependencyCollection != null )
204         {
205             mojo.setDependencyCollectionRequired( dependencyCollection.getValue() );
206         }
207 
208         String directInvocationOnly = c.getChild( "requiresDirectInvocation" ).getValue();
209 
210         if ( directInvocationOnly != null )
211         {
212             mojo.setDirectInvocationOnly( Boolean.parseBoolean( directInvocationOnly ) );
213         }
214 
215         String requiresProject = c.getChild( "requiresProject" ).getValue();
216 
217         if ( requiresProject != null )
218         {
219             mojo.setProjectRequired( Boolean.parseBoolean( requiresProject ) );
220         }
221 
222         String requiresReports = c.getChild( "requiresReports" ).getValue();
223 
224         if ( requiresReports != null )
225         {
226             mojo.setRequiresReports( Boolean.parseBoolean( requiresReports ) );
227         }
228 
229         String aggregator = c.getChild( "aggregator" ).getValue();
230 
231         if ( aggregator != null )
232         {
233             mojo.setAggregator( Boolean.parseBoolean( aggregator ) );
234         }
235 
236         String requiresOnline = c.getChild( "requiresOnline" ).getValue();
237 
238         if ( requiresOnline != null )
239         {
240             mojo.setOnlineRequired( Boolean.parseBoolean( requiresOnline ) );
241         }
242 
243         String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
244 
245         if ( inheritedByDefault != null )
246         {
247             mojo.setInheritedByDefault( Boolean.parseBoolean( inheritedByDefault ) );
248         }
249 
250         String threadSafe = c.getChild( "threadSafe" ).getValue();
251 
252         if ( threadSafe != null )
253         {
254             mojo.setThreadSafe( Boolean.parseBoolean( threadSafe ) );
255         }
256 
257         // ----------------------------------------------------------------------
258         // Configuration
259         // ----------------------------------------------------------------------
260 
261         PlexusConfiguration mojoConfig = c.getChild( "configuration" );
262         mojo.setMojoConfiguration( mojoConfig );
263 
264         // ----------------------------------------------------------------------
265         // Parameters
266         // ----------------------------------------------------------------------
267 
268         PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" );
269 
270         List<Parameter> parameters = new ArrayList<Parameter>();
271 
272         for ( PlexusConfiguration d : parameterConfigurations )
273         {
274             Parameter parameter = new Parameter();
275 
276             parameter.setName( d.getChild( "name" ).getValue() );
277 
278             parameter.setAlias( d.getChild( "alias" ).getValue() );
279 
280             parameter.setType( d.getChild( "type" ).getValue() );
281 
282             String required = d.getChild( "required" ).getValue();
283 
284             parameter.setRequired( Boolean.parseBoolean( required ) );
285 
286             PlexusConfiguration editableConfig = d.getChild( "editable" );
287 
288             // we need the null check for pre-build legacy plugins...
289             if ( editableConfig != null )
290             {
291                 String editable = d.getChild( "editable" ).getValue();
292 
293                 parameter.setEditable( editable == null || Boolean.parseBoolean( editable ) );
294             }
295 
296             parameter.setDescription( d.getChild( "description" ).getValue() );
297 
298             parameter.setDeprecated( d.getChild( "deprecated" ).getValue() );
299 
300             parameter.setImplementation( d.getChild( "implementation" ).getValue() );
301 
302             PlexusConfiguration paramConfig = mojoConfig.getChild( parameter.getName(), false );
303             if ( paramConfig != null )
304             {
305                 parameter.setExpression( paramConfig.getValue( null ) );
306                 parameter.setDefaultValue( paramConfig.getAttribute( "default-value" ) );
307             }
308 
309             parameters.add( parameter );
310         }
311 
312         mojo.setParameters( parameters );
313 
314         // TODO: this should not need to be handed off...
315 
316         // ----------------------------------------------------------------------
317         // Requirements
318         // ----------------------------------------------------------------------
319 
320         PlexusConfiguration[] requirements = c.getChild( "requirements" ).getChildren( "requirement" );
321 
322         for ( PlexusConfiguration requirement : requirements )
323         {
324             ComponentRequirement cr = new ComponentRequirement();
325 
326             cr.setRole( requirement.getChild( "role" ).getValue() );
327 
328             cr.setRoleHint( requirement.getChild( "role-hint" ).getValue() );
329 
330             cr.setFieldName( requirement.getChild( "field-name" ).getValue() );
331 
332             mojo.addRequirement( cr );
333         }
334 
335         return mojo;
336     }
337 
338     // ----------------------------------------------------------------------
339     //
340     // ----------------------------------------------------------------------
341 
342     public PlexusConfiguration buildConfiguration( Reader configuration )
343         throws PlexusConfigurationException
344     {
345         try
346         {
347             return new XmlPlexusConfiguration( Xpp3DomBuilder.build( configuration ) );
348         }
349         catch ( IOException e )
350         {
351             throw new PlexusConfigurationException( e.getMessage(), e );
352         }
353         catch ( XmlPullParserException e )
354         {
355             throw new PlexusConfigurationException( e.getMessage(), e );
356         }
357     }
358 }