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