KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > tools > plugin > extractor > AbstractScriptedMojoDescriptorExtractor


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 JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.TreeMap JavaDoc;
19
20 /**
21  * @author jdcasey
22  */

23 public abstract class AbstractScriptedMojoDescriptorExtractor
24     extends AbstractLogEnabled
25     implements MojoDescriptorExtractor
26 {
27     public List JavaDoc execute( MavenProject project, PluginDescriptor pluginDescriptor )
28         throws ExtractionException, InvalidPluginDescriptorException
29     {
30         String JavaDoc metadataExtension = getMetadataFileExtension();
31         String JavaDoc scriptExtension = getScriptFileExtension();
32         
33         Map JavaDoc scriptFilesKeyedByBasedir =
34             gatherFilesByBasedir( project.getBasedir(), project.getScriptSourceRoots(), scriptExtension );
35         
36         List JavaDoc mojoDescriptors;
37         if ( !StringUtils.isEmpty( metadataExtension ) )
38         {
39             Map JavaDoc 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 JavaDoc scriptFilesKeyedByBasedir, String JavaDoc outputDirectory )
55         throws ExtractionException
56     {
57         File JavaDoc outputDir = new File JavaDoc( outputDirectory );
58
59         if ( !outputDir.exists() )
60         {
61             outputDir.mkdirs();
62         }
63
64         for ( Iterator JavaDoc it = scriptFilesKeyedByBasedir.entrySet().iterator(); it.hasNext(); )
65         {
66             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
67
68             File JavaDoc sourceDir = new File JavaDoc( (String JavaDoc) entry.getKey() );
69
70             Set JavaDoc scripts = (Set JavaDoc) entry.getValue();
71
72             for ( Iterator JavaDoc scriptIterator = scripts.iterator(); scriptIterator.hasNext(); )
73             {
74                 File JavaDoc scriptFile = (File JavaDoc) scriptIterator.next();
75
76                 String JavaDoc 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 JavaDoc outputFile = new File JavaDoc( 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 JavaDoc e )
95                 {
96                     throw new ExtractionException(
97                         "Cannot copy script file: " + scriptFile + " to output: " + outputFile, e );
98                 }
99             }
100         }
101     }
102
103     protected Map JavaDoc gatherFilesByBasedir( File JavaDoc basedir, List JavaDoc directories, String JavaDoc scriptFileExtension )
104     {
105         Map JavaDoc sourcesByBasedir = new TreeMap JavaDoc();
106
107         for ( Iterator JavaDoc it = directories.iterator(); it.hasNext(); )
108         {
109             Set JavaDoc sources = new HashSet JavaDoc();
110
111             String JavaDoc resourceDir = (String JavaDoc) it.next();
112             
113             File JavaDoc dir = new File JavaDoc( 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 JavaDoc[] { "**/*" + scriptFileExtension } );
124                 scanner.scan();
125
126                 String JavaDoc[] relativePaths = scanner.getIncludedFiles();
127
128                 for ( int i = 0; i < relativePaths.length; i++ )
129                 {
130                     String JavaDoc relativePath = relativePaths[i];
131                     File JavaDoc scriptFile = new File JavaDoc( 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 JavaDoc extractMojoDescriptorsFromMetadata( Map JavaDoc metadataFilesKeyedByBasedir, PluginDescriptor pluginDescriptor )
147         throws ExtractionException, InvalidPluginDescriptorException
148     {
149         return null;
150     }
151
152     protected String JavaDoc getMetadataFileExtension()
153     {
154         return null;
155     }
156
157     protected List JavaDoc extractMojoDescriptors( Map JavaDoc scriptFilesKeyedByBasedir, PluginDescriptor pluginDescriptor )
158         throws ExtractionException, InvalidPluginDescriptorException
159     {
160         return null;
161     }
162
163     protected abstract String JavaDoc getScriptFileExtension();
164
165 }
Popular Tags