1 package org.apache.maven.tools.plugin.generator; 2 3 18 19 import org.apache.maven.plugin.descriptor.MojoDescriptor; 20 import org.apache.maven.plugin.descriptor.Parameter; 21 import org.apache.maven.plugin.descriptor.PluginDescriptor; 22 import org.apache.maven.plugin.descriptor.Requirement; 23 import org.apache.maven.tools.plugin.util.PluginUtils; 24 import org.codehaus.plexus.util.IOUtil; 25 import org.codehaus.plexus.util.StringUtils; 26 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; 27 import org.codehaus.plexus.util.xml.XMLWriter; 28 29 import java.io.File ; 30 import java.io.FileWriter ; 31 import java.io.IOException ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 44 public class PluginDescriptorGenerator 45 implements Generator 46 { 47 public void execute( File destinationDirectory, PluginDescriptor pluginDescriptor ) 48 throws IOException 49 { 50 File f = new File ( destinationDirectory, "plugin.xml" ); 51 52 if ( !f.getParentFile().exists() ) 53 { 54 f.getParentFile().mkdirs(); 55 } 56 57 FileWriter writer = null; 58 try 59 { 60 writer = new FileWriter ( f ); 61 62 XMLWriter w = new PrettyPrintXMLWriter( writer ); 63 64 w.startElement( "plugin" ); 65 66 element( w, "description", pluginDescriptor.getDescription() ); 67 68 element( w, "groupId", pluginDescriptor.getGroupId() ); 69 70 element( w, "artifactId", pluginDescriptor.getArtifactId() ); 71 72 element( w, "version", pluginDescriptor.getVersion() ); 73 74 element( w, "goalPrefix", pluginDescriptor.getGoalPrefix() ); 75 76 element( w, "isolatedRealm", "" + pluginDescriptor.isIsolatedRealm() ); 77 78 element( w, "inheritedByDefault", "" + pluginDescriptor.isInheritedByDefault() ); 79 80 w.startElement( "mojos" ); 81 82 if ( pluginDescriptor.getMojos() != null ) 83 { 84 for ( Iterator it = pluginDescriptor.getMojos().iterator(); it.hasNext(); ) 85 { 86 MojoDescriptor descriptor = (MojoDescriptor) it.next(); 87 processMojoDescriptor( descriptor, w ); 88 } 89 } 90 91 w.endElement(); 92 93 PluginUtils.writeDependencies( w, pluginDescriptor ); 94 95 w.endElement(); 96 97 writer.flush(); 98 } 99 finally 100 { 101 IOUtil.close( writer ); 102 } 103 } 104 105 protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w ) 106 { 107 w.startElement( "mojo" ); 108 109 113 w.startElement( "goal" ); 114 115 w.writeText( mojoDescriptor.getGoal() ); 116 117 w.endElement(); 118 119 123 String description = mojoDescriptor.getDescription(); 124 125 if ( description != null ) 126 { 127 w.startElement( "description" ); 128 129 w.writeText( mojoDescriptor.getDescription() ); 130 131 w.endElement(); 132 } 133 134 138 if ( mojoDescriptor.isDependencyResolutionRequired() != null ) 139 { 140 element( w, "requiresDependencyResolution", mojoDescriptor.isDependencyResolutionRequired() ); 141 } 142 143 147 element( w, "requiresDirectInvocation", "" + mojoDescriptor.isDirectInvocationOnly() ); 148 149 153 element( w, "requiresProject", "" + mojoDescriptor.isProjectRequired() ); 154 155 159 element( w, "requiresReports", "" + mojoDescriptor.isRequiresReports() ); 160 161 165 element( w, "aggregator", "" + mojoDescriptor.isAggregator() ); 166 167 171 element( w, "requiresOnline", "" + mojoDescriptor.isOnlineRequired() ); 172 173 177 element( w, "inheritedByDefault", "" + mojoDescriptor.isInheritedByDefault() ); 178 179 183 if ( mojoDescriptor.getPhase() != null ) 184 { 185 element( w, "phase", mojoDescriptor.getPhase() ); 186 } 187 188 192 if ( mojoDescriptor.getExecutePhase() != null ) 193 { 194 element( w, "executePhase", mojoDescriptor.getExecutePhase() ); 195 } 196 197 if ( mojoDescriptor.getExecuteGoal() != null ) 198 { 199 element( w, "executeGoal", mojoDescriptor.getExecuteGoal() ); 200 } 201 202 if ( mojoDescriptor.getExecuteLifecycle() != null ) 203 { 204 element( w, "executeLifecycle", mojoDescriptor.getExecuteLifecycle() ); 205 } 206 207 211 w.startElement( "implementation" ); 212 213 w.writeText( mojoDescriptor.getImplementation() ); 214 215 w.endElement(); 216 217 221 w.startElement( "language" ); 222 223 w.writeText( mojoDescriptor.getLanguage() ); 224 225 w.endElement(); 226 227 231 if ( mojoDescriptor.getComponentConfigurator() != null ) 232 { 233 w.startElement( "configurator" ); 234 235 w.writeText( mojoDescriptor.getComponentConfigurator() ); 236 237 w.endElement(); 238 } 239 240 244 if ( mojoDescriptor.getComponentComposer() != null ) 245 { 246 w.startElement( "composer" ); 247 248 w.writeText( mojoDescriptor.getComponentComposer() ); 249 250 w.endElement(); 251 } 252 253 257 w.startElement( "instantiationStrategy" ); 258 259 w.writeText( mojoDescriptor.getInstantiationStrategy() ); 260 261 w.endElement(); 262 263 w.startElement( "executionStrategy" ); 268 269 w.writeText( mojoDescriptor.getExecutionStrategy() ); 270 271 w.endElement(); 272 273 277 List parameters = mojoDescriptor.getParameters(); 278 279 w.startElement( "parameters" ); 280 281 Map requirements = new HashMap (); 282 283 Set configuration = new HashSet (); 284 285 if ( parameters != null ) 286 { 287 for ( int j = 0; j < parameters.size(); j++ ) 288 { 289 Parameter parameter = (Parameter) parameters.get( j ); 290 291 String expression = parameter.getExpression(); 292 293 if ( StringUtils.isNotEmpty( expression ) && expression.startsWith( "${component." ) ) 294 { 295 297 String role = expression.substring( "${component.".length(), expression.length() - 1 ); 299 300 String roleHint = null; 301 302 int posRoleHintSeparator; 303 304 if ( ( posRoleHintSeparator = role.indexOf( "#" ) ) > 0 ) 305 { 306 roleHint = role.substring( posRoleHintSeparator + 1 ); 307 308 role = role.substring( 0, posRoleHintSeparator ); 309 } 310 311 requirements.put( parameter.getName(), new Requirement( role, roleHint ) ); 313 } 314 else if ( parameter.getRequirement() != null ) 315 { 316 requirements.put( parameter.getName(), parameter.getRequirement() ); 317 } 318 else 319 { 320 322 w.startElement( "parameter" ); 323 324 element( w, "name", parameter.getName() ); 325 326 if ( parameter.getAlias() != null ) 327 { 328 element( w, "alias", parameter.getAlias() ); 329 } 330 331 element( w, "type", parameter.getType() ); 332 333 if ( parameter.getDeprecated() != null ) 334 { 335 element( w, "deprecated", parameter.getDeprecated() ); 336 } 337 338 element( w, "required", Boolean.toString( parameter.isRequired() ) ); 339 340 element( w, "editable", Boolean.toString( parameter.isEditable() ) ); 341 342 element( w, "description", parameter.getDescription() ); 343 344 if ( StringUtils.isNotEmpty( parameter.getDefaultValue() ) || 345 StringUtils.isNotEmpty( parameter.getExpression() ) ) 346 { 347 configuration.add( parameter ); 348 } 349 350 w.endElement(); 351 } 352 353 } 354 } 355 356 w.endElement(); 357 358 362 if ( !configuration.isEmpty() ) 363 { 364 w.startElement( "configuration" ); 365 366 for ( Iterator i = configuration.iterator(); i.hasNext(); ) 367 { 368 Parameter parameter = (Parameter) i.next(); 369 370 w.startElement( parameter.getName() ); 371 372 String type = parameter.getType(); 373 if ( type != null ) 374 { 375 w.addAttribute( "implementation", type ); 376 } 377 378 if ( parameter.getDefaultValue() != null ) 379 { 380 w.addAttribute( "default-value", parameter.getDefaultValue() ); 381 } 382 383 if ( parameter.getExpression() != null ) 384 { 385 w.writeText( parameter.getExpression() ); 386 } 387 388 w.endElement(); 389 } 390 391 w.endElement(); 392 } 393 394 398 if ( !requirements.isEmpty() ) 399 { 400 w.startElement( "requirements" ); 401 402 for ( Iterator i = requirements.keySet().iterator(); i.hasNext(); ) 403 { 404 String key = (String ) i.next(); 405 Requirement requirement = (Requirement) requirements.get( key ); 406 407 w.startElement( "requirement" ); 408 409 element( w, "role", requirement.getRole() ); 410 411 if ( requirement.getRoleHint() != null ) 412 { 413 element( w, "role-hint", requirement.getRoleHint() ); 414 } 415 416 element( w, "field-name", key ); 417 418 w.endElement(); 419 } 420 421 w.endElement(); 422 } 423 424 428 w.endElement(); 429 } 430 431 public void element( XMLWriter w, String name, String value ) 432 { 433 w.startElement( name ); 434 435 if ( value == null ) 436 { 437 value = ""; 438 } 439 440 w.writeText( value ); 441 442 w.endElement(); 443 } 444 } 445 | Popular Tags |