1 package org.apache.maven.tools.plugin.extractor.beanshell; 2 3 18 19 import bsh.EvalError; 20 import bsh.Interpreter; 21 22 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; 23 import org.apache.maven.plugin.descriptor.MojoDescriptor; 24 import org.apache.maven.plugin.descriptor.PluginDescriptor; 25 import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor; 26 import org.apache.maven.tools.plugin.extractor.ExtractionException; 27 28 import java.io.File ; 29 import java.io.InputStreamReader ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 36 42 public class BeanshellMojoDescriptorExtractor 43 extends AbstractScriptedMojoDescriptorExtractor 44 { 45 private MojoDescriptor createMojoDescriptor( String basedir, String resource, PluginDescriptor pluginDescriptor ) 46 throws InvalidPluginDescriptorException 47 { 48 MojoDescriptor mojoDescriptor = new MojoDescriptor(); 49 mojoDescriptor.setPluginDescriptor( pluginDescriptor ); 50 51 mojoDescriptor.setLanguage( "bsh" ); 52 mojoDescriptor.setComponentConfigurator( "bsh" ); 53 54 mojoDescriptor.setImplementation( resource ); 55 56 Interpreter interpreter = new Interpreter(); 57 58 try 59 { 60 interpreter.set( "file", new File ( basedir, resource ) ); 61 62 interpreter.set( "mojoDescriptor", mojoDescriptor ); 63 64 interpreter.eval( new InputStreamReader ( getClass().getResourceAsStream( "/extractor.bsh" ) ) ); 65 } 66 catch ( EvalError evalError ) 67 { 68 throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError ); 69 } 70 71 return mojoDescriptor; 72 } 73 74 protected String getScriptFileExtension() 75 { 76 return ".bsh"; 77 } 78 79 protected List extractMojoDescriptors( Map scriptFilesKeyedByBasedir, PluginDescriptor pluginDescriptor ) 80 throws ExtractionException, InvalidPluginDescriptorException 81 { 82 List descriptors = new ArrayList (); 83 84 for ( Iterator mapIterator = scriptFilesKeyedByBasedir.entrySet().iterator(); mapIterator.hasNext(); ) 85 { 86 Map.Entry entry = (Map.Entry ) mapIterator.next(); 87 88 String basedir = (String ) entry.getKey(); 89 Set metadataFiles = (Set ) entry.getValue(); 90 91 for ( Iterator it = metadataFiles.iterator(); it.hasNext(); ) 92 { 93 File scriptFile = (File ) it.next(); 94 95 String relativePath = null; 96 97 if ( basedir.endsWith( "/" ) ) 98 { 99 basedir = basedir.substring( 0, basedir.length() - 2 ); 100 } 101 102 relativePath = scriptFile.getPath().substring( basedir.length() ); 103 104 relativePath = relativePath.replace( '\\', '/' ); 105 106 MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, relativePath, pluginDescriptor ); 107 descriptors.add( mojoDescriptor ); 108 } 109 } 110 111 return descriptors; 112 } 113 } | Popular Tags |