1 package org.apache.maven.tools.plugin.extractor.ant; 2 3 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; 4 import org.apache.maven.plugin.descriptor.MojoDescriptor; 5 import org.apache.maven.plugin.descriptor.Parameter; 6 import org.apache.maven.plugin.descriptor.PluginDescriptor; 7 import org.apache.maven.plugin.tools.model.PluginMetadataParseException; 8 import org.apache.maven.plugin.tools.model.PluginMetadataParser; 9 import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor; 10 import org.apache.maven.tools.plugin.extractor.ExtractionException; 11 import org.codehaus.plexus.util.StringUtils; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 public class AntMojoDescriptorExtractor 21 extends AbstractScriptedMojoDescriptorExtractor 22 { 23 24 private static final String METADATA_FILE_EXTENSION = ".mojos.xml"; 25 26 private static final String SCRIPT_FILE_EXTENSION = ".build.xml"; 27 28 protected List extractMojoDescriptorsFromMetadata( Map metadataFilesKeyedByBasedir, PluginDescriptor pluginDescriptor ) 29 throws ExtractionException, InvalidPluginDescriptorException 30 { 31 List descriptors = new ArrayList (); 32 33 PluginMetadataParser parser = new PluginMetadataParser(); 34 35 for ( Iterator mapIterator = metadataFilesKeyedByBasedir.entrySet().iterator(); mapIterator.hasNext(); ) 36 { 37 Map.Entry entry = (Map.Entry ) mapIterator.next(); 38 39 String basedir = (String ) entry.getKey(); 40 Set metadataFiles = (Set ) entry.getValue(); 41 42 for ( Iterator it = metadataFiles.iterator(); it.hasNext(); ) 43 { 44 File metadataFile = (File ) it.next(); 45 46 String basename = metadataFile.getName(); 47 basename = basename.substring( 0, basename.length() - METADATA_FILE_EXTENSION.length() ); 48 49 File scriptFile = new File ( metadataFile.getParentFile(), basename + SCRIPT_FILE_EXTENSION ); 50 51 if ( !scriptFile.exists() ) 52 { 53 throw new InvalidPluginDescriptorException( "Found orphaned plugin metadata file: " 54 + metadataFile ); 55 } 56 57 String relativePath = null; 58 59 if ( basedir.endsWith( "/" ) ) 60 { 61 basedir = basedir.substring( 0, basedir.length() - 2 ); 62 } 63 64 relativePath = scriptFile.getPath().substring( basedir.length() ); 65 66 relativePath = relativePath.replace( '\\', '/' ); 67 68 try 69 { 70 Set mojoDescriptors = parser.parseMojoDescriptors( metadataFile ); 71 72 for ( Iterator discoveredMojoIterator = mojoDescriptors.iterator(); discoveredMojoIterator 73 .hasNext(); ) 74 { 75 MojoDescriptor descriptor = (MojoDescriptor) discoveredMojoIterator.next(); 76 77 Map paramMap = descriptor.getParameterMap(); 78 79 if ( !paramMap.containsKey( "basedir" ) ) 80 { 81 Parameter param = new Parameter(); 82 param.setName( "basedir" ); 83 param.setAlias( "ant.basedir" ); 84 param.setExpression( "${antBasedir}" ); 85 param.setDefaultValue( "${basedir}" ); 86 param.setType( "java.io.File" ); 87 param.setDescription( "The base directory from which to execute the Ant script." ); 88 param.setEditable( true ); 89 param.setRequired( true ); 90 91 descriptor.addParameter( param ); 92 } 93 94 if ( !paramMap.containsKey( "antMessageLevel" ) ) 95 { 96 Parameter param = new Parameter(); 97 param.setName( "messageLevel" ); 98 param.setAlias( "ant.messageLevel" ); 99 param.setExpression( "${antMessageLevel}" ); 100 param.setDefaultValue( "info" ); 101 param.setType( "java.lang.String" ); 102 param.setDescription( "The message-level used to tune the verbosity of Ant logging." ); 103 param.setEditable( true ); 104 param.setRequired( false ); 105 106 descriptor.addParameter( param ); 107 } 108 109 String implementation = relativePath; 110 111 String dImpl = descriptor.getImplementation(); 112 if ( StringUtils.isNotEmpty( dImpl ) ) 113 { 114 implementation = relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() ); 115 } 116 117 descriptor.setImplementation( implementation ); 118 119 descriptor.setLanguage( "ant-mojo" ); 120 descriptor.setComponentComposer( "map-oriented" ); 121 descriptor.setComponentConfigurator( "map-oriented" ); 122 123 descriptor.setPluginDescriptor( pluginDescriptor ); 124 125 descriptors.add( descriptor ); 126 } 127 } 128 catch ( PluginMetadataParseException e ) 129 { 130 throw new ExtractionException( "Error extracting mojo descriptor from script: " 131 + metadataFile, e ); 132 } 133 } 134 } 135 136 return descriptors; 137 } 138 139 protected String getScriptFileExtension() 140 { 141 return SCRIPT_FILE_EXTENSION; 142 } 143 144 protected String getMetadataFileExtension() 145 { 146 return METADATA_FILE_EXTENSION; 147 } 148 } 149 | Popular Tags |