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         // Parameters
259         // ----------------------------------------------------------------------
260 
261         PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" );
262 
263         List<Parameter> parameters = new ArrayList<Parameter>();
264 
265         for ( PlexusConfiguration d : parameterConfigurations )
266         {
267             Parameter parameter = new Parameter();
268 
269             parameter.setName( d.getChild( "name" ).getValue() );
270 
271             parameter.setAlias( d.getChild( "alias" ).getValue() );
272 
273             parameter.setType( d.getChild( "type" ).getValue() );
274 
275             String required = d.getChild( "required" ).getValue();
276 
277             parameter.setRequired( Boolean.parseBoolean( required ) );
278 
279             PlexusConfiguration editableConfig = d.getChild( "editable" );
280 
281             // we need the null check for pre-build legacy plugins...
282             if ( editableConfig != null )
283             {
284                 String editable = d.getChild( "editable" ).getValue();
285 
286                 parameter.setEditable( editable == null || Boolean.parseBoolean( editable ) );
287             }
288 
289             parameter.setDescription( d.getChild( "description" ).getValue() );
290 
291             parameter.setDeprecated( d.getChild( "deprecated" ).getValue() );
292 
293             parameter.setImplementation( d.getChild( "implementation" ).getValue() );
294 
295             parameters.add( parameter );
296         }
297 
298         mojo.setParameters( parameters );
299 
300         // TODO: this should not need to be handed off...
301 
302         // ----------------------------------------------------------------------
303         // Configuration
304         // ----------------------------------------------------------------------
305 
306         mojo.setMojoConfiguration( c.getChild( "configuration" ) );
307 
308         // TODO: Go back to this when we get the container ready to configure mojos...
309         //        mojo.setConfiguration( c.getChild( "configuration" ) );
310 
311         // ----------------------------------------------------------------------
312         // Requirements
313         // ----------------------------------------------------------------------
314 
315         PlexusConfiguration[] requirements = c.getChild( "requirements" ).getChildren( "requirement" );
316 
317         for ( PlexusConfiguration requirement : requirements )
318         {
319             ComponentRequirement cr = new ComponentRequirement();
320 
321             cr.setRole( requirement.getChild( "role" ).getValue() );
322 
323             cr.setRoleHint( requirement.getChild( "role-hint" ).getValue() );
324 
325             cr.setFieldName( requirement.getChild( "field-name" ).getValue() );
326 
327             mojo.addRequirement( cr );
328         }
329 
330         return mojo;
331     }
332 
333     // ----------------------------------------------------------------------
334     //
335     // ----------------------------------------------------------------------
336 
337     public PlexusConfiguration buildConfiguration( Reader configuration )
338         throws PlexusConfigurationException
339     {
340         try
341         {
342             return new XmlPlexusConfiguration( Xpp3DomBuilder.build( configuration ) );
343         }
344         catch ( IOException e )
345         {
346             throw new PlexusConfigurationException( e.getMessage(), e );
347         }
348         catch ( XmlPullParserException e )
349         {
350             throw new PlexusConfigurationException( e.getMessage(), e );
351         }
352     }
353 }