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