1 package org.apache.maven.tools.plugin.extractor; 2 3 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; 4 import org.apache.maven.plugin.descriptor.PluginDescriptor; 5 import org.apache.maven.project.MavenProject; 6 import org.codehaus.plexus.logging.AbstractLogEnabled; 7 import org.codehaus.plexus.util.DirectoryScanner; 8 import org.codehaus.plexus.util.FileUtils; 9 import org.codehaus.plexus.util.StringUtils; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.Set ; 18 import java.util.TreeMap ; 19 20 23 public abstract class AbstractScriptedMojoDescriptorExtractor 24 extends AbstractLogEnabled 25 implements MojoDescriptorExtractor 26 { 27 public List execute( MavenProject project, PluginDescriptor pluginDescriptor ) 28 throws ExtractionException, InvalidPluginDescriptorException 29 { 30 String metadataExtension = getMetadataFileExtension(); 31 String scriptExtension = getScriptFileExtension(); 32 33 Map scriptFilesKeyedByBasedir = 34 gatherFilesByBasedir( project.getBasedir(), project.getScriptSourceRoots(), scriptExtension ); 35 36 List mojoDescriptors; 37 if ( !StringUtils.isEmpty( metadataExtension ) ) 38 { 39 Map metadataFilesKeyedByBasedir = 40 gatherFilesByBasedir( project.getBasedir(), project.getScriptSourceRoots(), metadataExtension ); 41 42 mojoDescriptors = extractMojoDescriptorsFromMetadata( metadataFilesKeyedByBasedir, pluginDescriptor ); 43 } 44 else 45 { 46 mojoDescriptors = extractMojoDescriptors( scriptFilesKeyedByBasedir, pluginDescriptor ); 47 } 48 49 copyScriptsToOutputDirectory( scriptFilesKeyedByBasedir, project.getBuild().getOutputDirectory() ); 50 51 return mojoDescriptors; 52 } 53 54 protected void copyScriptsToOutputDirectory( Map scriptFilesKeyedByBasedir, String outputDirectory ) 55 throws ExtractionException 56 { 57 File outputDir = new File ( outputDirectory ); 58 59 if ( !outputDir.exists() ) 60 { 61 outputDir.mkdirs(); 62 } 63 64 for ( Iterator it = scriptFilesKeyedByBasedir.entrySet().iterator(); it.hasNext(); ) 65 { 66 Map.Entry entry = (Map.Entry ) it.next(); 67 68 File sourceDir = new File ( (String ) entry.getKey() ); 69 70 Set scripts = (Set ) entry.getValue(); 71 72 for ( Iterator scriptIterator = scripts.iterator(); scriptIterator.hasNext(); ) 73 { 74 File scriptFile = (File ) scriptIterator.next(); 75 76 String relativePath = scriptFile.getPath().substring( sourceDir.getPath().length() ); 77 78 if ( relativePath.charAt( 0 ) == File.separatorChar ) 79 { 80 relativePath = relativePath.substring( 1 ); 81 } 82 83 File outputFile = new File ( outputDir, relativePath ).getAbsoluteFile(); 84 85 if ( !outputFile.getParentFile().exists() ) 86 { 87 outputFile.getParentFile().mkdirs(); 88 } 89 90 try 91 { 92 FileUtils.copyFile( scriptFile, outputFile ); 93 } 94 catch ( IOException e ) 95 { 96 throw new ExtractionException( 97 "Cannot copy script file: " + scriptFile + " to output: " + outputFile, e ); 98 } 99 } 100 } 101 } 102 103 protected Map gatherFilesByBasedir( File basedir, List directories, String scriptFileExtension ) 104 { 105 Map sourcesByBasedir = new TreeMap (); 106 107 for ( Iterator it = directories.iterator(); it.hasNext(); ) 108 { 109 Set sources = new HashSet (); 110 111 String resourceDir = (String ) it.next(); 112 113 File dir = new File ( basedir, resourceDir ).getAbsoluteFile(); 114 115 resourceDir = dir.getPath(); 116 117 if ( dir.exists() ) 118 { 119 DirectoryScanner scanner = new DirectoryScanner(); 120 121 scanner.setBasedir( dir ); 122 scanner.addDefaultExcludes(); 123 scanner.setIncludes( new String [] { "**/*" + scriptFileExtension } ); 124 scanner.scan(); 125 126 String [] relativePaths = scanner.getIncludedFiles(); 127 128 for ( int i = 0; i < relativePaths.length; i++ ) 129 { 130 String relativePath = relativePaths[i]; 131 File scriptFile = new File ( dir, relativePath ).getAbsoluteFile(); 132 133 if ( scriptFile.isFile() && relativePath.endsWith( scriptFileExtension ) ) 134 { 135 sources.add( scriptFile ); 136 } 137 } 138 139 sourcesByBasedir.put( resourceDir, sources ); 140 } 141 } 142 143 return sourcesByBasedir; 144 } 145 146 protected List extractMojoDescriptorsFromMetadata( Map metadataFilesKeyedByBasedir, PluginDescriptor pluginDescriptor ) 147 throws ExtractionException, InvalidPluginDescriptorException 148 { 149 return null; 150 } 151 152 protected String getMetadataFileExtension() 153 { 154 return null; 155 } 156 157 protected List extractMojoDescriptors( Map scriptFilesKeyedByBasedir, PluginDescriptor pluginDescriptor ) 158 throws ExtractionException, InvalidPluginDescriptorException 159 { 160 return null; 161 } 162 163 protected abstract String getScriptFileExtension(); 164 165 } | Popular Tags |