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<>();
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     @SuppressWarnings( "checkstyle:methodlength" )
118     public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor )
119         throws PlexusConfigurationException
120     {
121         MojoDescriptor mojo = new MojoDescriptor();
122         mojo.setPluginDescriptor( pluginDescriptor );
123 
124         mojo.setGoal( c.getChild( "goal" ).getValue() );
125 
126         mojo.setImplementation( c.getChild( "implementation" ).getValue() );
127 
128         PlexusConfiguration langConfig = c.getChild( "language" );
129 
130         if ( langConfig != null )
131         {
132             mojo.setLanguage( langConfig.getValue() );
133         }
134 
135         PlexusConfiguration configuratorConfig = c.getChild( "configurator" );
136 
137         if ( configuratorConfig != null )
138         {
139             mojo.setComponentConfigurator( configuratorConfig.getValue() );
140         }
141 
142         PlexusConfiguration composerConfig = c.getChild( "composer" );
143 
144         if ( composerConfig != null )
145         {
146             mojo.setComponentComposer( composerConfig.getValue() );
147         }
148 
149         String since = c.getChild( "since" ).getValue();
150 
151         if ( since != null )
152         {
153             mojo.setSince( since );
154         }
155 
156         PlexusConfiguration deprecated = c.getChild( "deprecated", false );
157 
158         if ( deprecated != null )
159         {
160             mojo.setDeprecated( deprecated.getValue() );
161         }
162 
163         String phase = c.getChild( "phase" ).getValue();
164 
165         if ( phase != null )
166         {
167             mojo.setPhase( phase );
168         }
169 
170         String executePhase = c.getChild( "executePhase" ).getValue();
171 
172         if ( executePhase != null )
173         {
174             mojo.setExecutePhase( executePhase );
175         }
176 
177         String executeMojo = c.getChild( "executeGoal" ).getValue();
178 
179         if ( executeMojo != null )
180         {
181             mojo.setExecuteGoal( executeMojo );
182         }
183 
184         String executeLifecycle = c.getChild( "executeLifecycle" ).getValue();
185 
186         if ( executeLifecycle != null )
187         {
188             mojo.setExecuteLifecycle( executeLifecycle );
189         }
190 
191         mojo.setInstantiationStrategy( c.getChild( "instantiationStrategy" ).getValue() );
192 
193         mojo.setDescription( c.getChild( "description" ).getValue() );
194 
195         PlexusConfiguration dependencyResolution = c.getChild( "requiresDependencyResolution", false );
196 
197         if ( dependencyResolution != null )
198         {
199             mojo.setDependencyResolutionRequired( dependencyResolution.getValue() );
200         }
201 
202         PlexusConfiguration dependencyCollection = c.getChild( "requiresDependencyCollection", false );
203 
204         if ( dependencyCollection != null )
205         {
206             mojo.setDependencyCollectionRequired( dependencyCollection.getValue() );
207         }
208 
209         String directInvocationOnly = c.getChild( "requiresDirectInvocation" ).getValue();
210 
211         if ( directInvocationOnly != null )
212         {
213             mojo.setDirectInvocationOnly( Boolean.parseBoolean( directInvocationOnly ) );
214         }
215 
216         String requiresProject = c.getChild( "requiresProject" ).getValue();
217 
218         if ( requiresProject != null )
219         {
220             mojo.setProjectRequired( Boolean.parseBoolean( requiresProject ) );
221         }
222 
223         String requiresReports = c.getChild( "requiresReports" ).getValue();
224 
225         if ( requiresReports != null )
226         {
227             mojo.setRequiresReports( Boolean.parseBoolean( requiresReports ) );
228         }
229 
230         String aggregator = c.getChild( "aggregator" ).getValue();
231 
232         if ( aggregator != null )
233         {
234             mojo.setAggregator( Boolean.parseBoolean( aggregator ) );
235         }
236 
237         String requiresOnline = c.getChild( "requiresOnline" ).getValue();
238 
239         if ( requiresOnline != null )
240         {
241             mojo.setOnlineRequired( Boolean.parseBoolean( requiresOnline ) );
242         }
243 
244         String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
245 
246         if ( inheritedByDefault != null )
247         {
248             mojo.setInheritedByDefault( Boolean.parseBoolean( inheritedByDefault ) );
249         }
250 
251         String threadSafe = c.getChild( "threadSafe" ).getValue();
252 
253         if ( threadSafe != null )
254         {
255             mojo.setThreadSafe( Boolean.parseBoolean( threadSafe ) );
256         }
257 
258         // ----------------------------------------------------------------------
259         // Configuration
260         // ----------------------------------------------------------------------
261 
262         PlexusConfiguration mojoConfig = c.getChild( "configuration" );
263         mojo.setMojoConfiguration( mojoConfig );
264 
265         // ----------------------------------------------------------------------
266         // Parameters
267         // ----------------------------------------------------------------------
268 
269         PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" );
270 
271         List<Parameter> parameters = new ArrayList<>();
272 
273         for ( PlexusConfiguration d : parameterConfigurations )
274         {
275             Parameter parameter = new Parameter();
276 
277             parameter.setName( d.getChild( "name" ).getValue() );
278 
279             parameter.setAlias( d.getChild( "alias" ).getValue() );
280 
281             parameter.setType( d.getChild( "type" ).getValue() );
282 
283             String required = d.getChild( "required" ).getValue();
284 
285             parameter.setRequired( Boolean.parseBoolean( required ) );
286 
287             PlexusConfiguration editableConfig = d.getChild( "editable" );
288 
289             // we need the null check for pre-build legacy plugins...
290             if ( editableConfig != null )
291             {
292                 String editable = d.getChild( "editable" ).getValue();
293 
294                 parameter.setEditable( editable == null || Boolean.parseBoolean( editable ) );
295             }
296 
297             parameter.setDescription( d.getChild( "description" ).getValue() );
298 
299             parameter.setDeprecated( d.getChild( "deprecated" ).getValue() );
300 
301             parameter.setImplementation( d.getChild( "implementation" ).getValue() );
302 
303             parameter.setSince( d.getChild( "since" ).getValue() );
304 
305             PlexusConfiguration paramConfig = mojoConfig.getChild( parameter.getName(), false );
306             if ( paramConfig != null )
307             {
308                 parameter.setExpression( paramConfig.getValue( null ) );
309                 parameter.setDefaultValue( paramConfig.getAttribute( "default-value" ) );
310             }
311 
312             parameters.add( parameter );
313         }
314 
315         mojo.setParameters( parameters );
316 
317         // TODO this should not need to be handed off...
318 
319         // ----------------------------------------------------------------------
320         // Requirements
321         // ----------------------------------------------------------------------
322 
323         PlexusConfiguration[] requirements = c.getChild( "requirements" ).getChildren( "requirement" );
324 
325         for ( PlexusConfiguration requirement : requirements )
326         {
327             ComponentRequirement cr = new ComponentRequirement();
328 
329             cr.setRole( requirement.getChild( "role" ).getValue() );
330 
331             cr.setRoleHint( requirement.getChild( "role-hint" ).getValue() );
332 
333             cr.setFieldName( requirement.getChild( "field-name" ).getValue() );
334 
335             mojo.addRequirement( cr );
336         }
337 
338         return mojo;
339     }
340 
341     // ----------------------------------------------------------------------
342     //
343     // ----------------------------------------------------------------------
344 
345     public PlexusConfiguration buildConfiguration( Reader configuration )
346         throws PlexusConfigurationException
347     {
348         try
349         {
350             return new XmlPlexusConfiguration( Xpp3DomBuilder.build( configuration ) );
351         }
352         catch ( IOException | XmlPullParserException e )
353         {
354             throw new PlexusConfigurationException( e.getMessage(), e );
355         }
356     }
357 }