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 java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.HashSet; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import java.util.Set; |
28 | |
import java.util.TreeMap; |
29 | |
|
30 | |
import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; |
31 | |
import org.apache.maven.plugin.descriptor.MojoDescriptor; |
32 | |
import org.apache.maven.plugin.descriptor.PluginDescriptor; |
33 | |
import org.apache.maven.project.MavenProject; |
34 | |
import org.apache.maven.tools.plugin.DefaultPluginToolsRequest; |
35 | |
import org.apache.maven.tools.plugin.PluginToolsRequest; |
36 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
37 | |
import org.codehaus.plexus.util.DirectoryScanner; |
38 | |
import org.codehaus.plexus.util.FileUtils; |
39 | |
import org.codehaus.plexus.util.StringUtils; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public abstract class AbstractScriptedMojoDescriptorExtractor |
46 | |
extends AbstractLogEnabled |
47 | |
implements MojoDescriptorExtractor |
48 | |
{ |
49 | |
|
50 | |
public List<MojoDescriptor> execute( MavenProject project, PluginDescriptor pluginDescriptor ) |
51 | |
throws ExtractionException, InvalidPluginDescriptorException |
52 | |
{ |
53 | 0 | return execute( new DefaultPluginToolsRequest( project, pluginDescriptor ) ); |
54 | |
} |
55 | |
|
56 | |
|
57 | |
public List<MojoDescriptor> execute( PluginToolsRequest request ) |
58 | |
throws ExtractionException, InvalidPluginDescriptorException |
59 | |
{ |
60 | 0 | getLogger().debug( "Running: " + getClass().getName() ); |
61 | 0 | String metadataExtension = getMetadataFileExtension( request ); |
62 | 0 | String scriptExtension = getScriptFileExtension( request ); |
63 | |
|
64 | 0 | MavenProject project = request.getProject(); |
65 | |
|
66 | |
@SuppressWarnings( "unchecked" ) |
67 | 0 | Map<String, Set<File>> scriptFilesKeyedByBasedir = |
68 | |
gatherFilesByBasedir( project.getBasedir(), project.getScriptSourceRoots(), scriptExtension, request ); |
69 | |
|
70 | |
List<MojoDescriptor> mojoDescriptors; |
71 | 0 | if ( !StringUtils.isEmpty( metadataExtension ) ) |
72 | |
{ |
73 | |
@SuppressWarnings( "unchecked" ) |
74 | 0 | Map<String, Set<File>> metadataFilesKeyedByBasedir = |
75 | |
gatherFilesByBasedir( project.getBasedir(), project.getScriptSourceRoots(), metadataExtension, |
76 | |
request ); |
77 | |
|
78 | 0 | mojoDescriptors = extractMojoDescriptorsFromMetadata( metadataFilesKeyedByBasedir, request ); |
79 | 0 | } |
80 | |
else |
81 | |
{ |
82 | 0 | mojoDescriptors = extractMojoDescriptors( scriptFilesKeyedByBasedir, request ); |
83 | |
} |
84 | |
|
85 | 0 | copyScriptsToOutputDirectory( scriptFilesKeyedByBasedir, project.getBuild().getOutputDirectory(), request ); |
86 | |
|
87 | 0 | return mojoDescriptors; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
protected void copyScriptsToOutputDirectory( Map<String, Set<File>> scriptFilesKeyedByBasedir, String outputDirectory, PluginToolsRequest request ) |
96 | |
throws ExtractionException |
97 | |
{ |
98 | 0 | File outputDir = new File( outputDirectory ); |
99 | |
|
100 | 0 | if ( !outputDir.exists() ) |
101 | |
{ |
102 | 0 | outputDir.mkdirs(); |
103 | |
} |
104 | |
|
105 | 0 | for ( Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet() ) |
106 | |
{ |
107 | 0 | File sourceDir = new File( entry.getKey() ); |
108 | |
|
109 | 0 | Set<File> scripts = entry.getValue(); |
110 | |
|
111 | 0 | for ( File scriptFile : scripts ) |
112 | |
{ |
113 | 0 | String relativePath = scriptFile.getPath().substring( sourceDir.getPath().length() ); |
114 | |
|
115 | 0 | if ( relativePath.charAt( 0 ) == File.separatorChar ) |
116 | |
{ |
117 | 0 | relativePath = relativePath.substring( 1 ); |
118 | |
} |
119 | |
|
120 | 0 | File outputFile = new File( outputDir, relativePath ).getAbsoluteFile(); |
121 | |
|
122 | 0 | if ( !outputFile.getParentFile().exists() ) |
123 | |
{ |
124 | 0 | outputFile.getParentFile().mkdirs(); |
125 | |
} |
126 | |
|
127 | |
try |
128 | |
{ |
129 | 0 | FileUtils.copyFile( scriptFile, outputFile ); |
130 | |
} |
131 | 0 | catch ( IOException e ) |
132 | |
{ |
133 | 0 | throw new ExtractionException( |
134 | |
"Cannot copy script file: " + scriptFile + " to output: " + outputFile, e ); |
135 | 0 | } |
136 | 0 | } |
137 | 0 | } |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
protected Map<String, Set<File>> gatherFilesByBasedir( File basedir, List<String> directories, String scriptFileExtension, PluginToolsRequest request ) |
147 | |
{ |
148 | 0 | Map<String, Set<File>> sourcesByBasedir = new TreeMap<String, Set<File>>(); |
149 | |
|
150 | 0 | for ( String resourceDir : directories ) |
151 | |
{ |
152 | 0 | Set<File> sources = new HashSet<File>(); |
153 | |
|
154 | 0 | getLogger().debug( "Scanning script dir: " + resourceDir + " with extractor: " + getClass().getName() ); |
155 | 0 | File dir = new File( resourceDir ); |
156 | 0 | if ( !dir.isAbsolute() ) |
157 | |
{ |
158 | 0 | dir = new File( basedir, resourceDir ).getAbsoluteFile(); |
159 | |
} |
160 | |
|
161 | 0 | resourceDir = dir.getPath(); |
162 | |
|
163 | 0 | if ( dir.exists() ) |
164 | |
{ |
165 | 0 | DirectoryScanner scanner = new DirectoryScanner(); |
166 | |
|
167 | 0 | scanner.setBasedir( dir ); |
168 | 0 | scanner.addDefaultExcludes(); |
169 | 0 | scanner.setIncludes( new String[]{"**/*" + scriptFileExtension} ); |
170 | 0 | scanner.scan(); |
171 | |
|
172 | 0 | String[] relativePaths = scanner.getIncludedFiles(); |
173 | |
|
174 | 0 | for ( String relativePath : relativePaths ) |
175 | |
{ |
176 | 0 | File scriptFile = new File( dir, relativePath ).getAbsoluteFile(); |
177 | |
|
178 | 0 | if ( scriptFile.isFile() && relativePath.endsWith( scriptFileExtension ) ) |
179 | |
{ |
180 | 0 | sources.add( scriptFile ); |
181 | |
} |
182 | |
} |
183 | |
|
184 | 0 | sourcesByBasedir.put( resourceDir, sources ); |
185 | |
} |
186 | 0 | } |
187 | |
|
188 | 0 | return sourcesByBasedir; |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
protected List<MojoDescriptor> extractMojoDescriptorsFromMetadata( Map<String, Set<File>> metadataFilesKeyedByBasedir, |
201 | |
PluginToolsRequest request ) |
202 | |
throws ExtractionException, InvalidPluginDescriptorException |
203 | |
{ |
204 | 0 | return null; |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
protected String getMetadataFileExtension( PluginToolsRequest request ) |
213 | |
{ |
214 | 0 | return null; |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
protected List<MojoDescriptor> extractMojoDescriptors( Map<String, Set<File>> scriptFilesKeyedByBasedir, PluginToolsRequest request ) |
227 | |
throws ExtractionException, InvalidPluginDescriptorException |
228 | |
{ |
229 | 0 | return null; |
230 | |
} |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
protected abstract String getScriptFileExtension( PluginToolsRequest request ); |
236 | |
|
237 | |
} |