1 package org.apache.maven.plugin.resources.remote.stub;
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.FileOutputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28
29 import org.apache.maven.model.Build;
30 import org.codehaus.plexus.util.FileUtils;
31
32
33
34
35 public class MavenProjectBuildStub
36 extends MavenProjectBasicStub
37 {
38 public static final int RESOURCES_FILE = 1;
39
40 public static final int ROOT_FILE = 2;
41
42 public static final int OUTPUT_FILE = 3;
43
44 public static final int SOURCE_FILE = 4;
45
46 protected Build build;
47
48 protected String srcDirectory;
49
50 protected String targetDirectory;
51
52 protected String buildDirectory;
53
54 protected String outputDirectory;
55
56 protected String testOutputDirectory;
57
58 protected String resourcesDirectory;
59
60 protected String testResourcesDirectory;
61
62 protected String targetResourceDirectory;
63
64 protected String targetTestResourcesDirectory;
65
66 protected ArrayList<String> targetClassesList;
67
68 protected ArrayList<String> sourceFileList;
69
70 protected ArrayList<String> resourcesFileList;
71
72 protected ArrayList<String> rootFileList;
73
74 protected ArrayList<String> directoryList;
75
76 protected HashMap<String, String> dataMap;
77
78 public MavenProjectBuildStub( String key )
79 throws Exception
80 {
81 super( key );
82
83 build = new Build();
84 resourcesFileList = new ArrayList<>();
85 sourceFileList = new ArrayList<>();
86 rootFileList = new ArrayList<>();
87 directoryList = new ArrayList<>();
88 targetClassesList = new ArrayList<>();
89 dataMap = new HashMap<>();
90 setupBuild();
91 }
92
93 public void addDirectory( String name )
94 {
95 if ( isValidPath( name ) )
96 {
97 directoryList.add( name );
98 }
99 }
100
101 public void setOutputDirectory( String dir )
102 {
103 outputDirectory = buildDirectory + "/" + dir;
104 build.setOutputDirectory( outputDirectory );
105 }
106
107 public void addFile( String name, int type )
108 {
109 if ( isValidPath( name ) )
110 {
111 List<String> list = getList( type );
112
113 list.add( name );
114 }
115 }
116
117 public void addFile( String name, String data, int type )
118 {
119 File fileName = new File( name );
120
121 addFile( name, type );
122 dataMap.put( fileName.getName(), data );
123 }
124
125 public String getOutputDirectory()
126 {
127 return outputDirectory;
128 }
129
130 public String getTestOutputDirectory()
131 {
132 return testOutputDirectory;
133 }
134
135 public String getResourcesDirectory()
136 {
137 return resourcesDirectory;
138 }
139
140 public String getTestResourcesDirectory()
141 {
142 return testResourcesDirectory;
143 }
144
145 @Override
146 public Build getBuild()
147 {
148 return build;
149 }
150
151
152
153
154
155
156
157
158
159
160 private boolean isValidPath( String path )
161 {
162 boolean bRetVal = true;
163
164 if ( path.startsWith( "c:" ) || path.startsWith( ".." ) || path.startsWith( "/" ) || path.startsWith( "\\" ) )
165 {
166 bRetVal = false;
167 }
168
169 return bRetVal;
170 }
171
172 private void setupBuild()
173 {
174
175
176
177 srcDirectory = testRootDir + "/src";
178 buildDirectory = testRootDir + "/target";
179 outputDirectory = buildDirectory + "/classes";
180 testOutputDirectory = buildDirectory + "/test-classes";
181 resourcesDirectory = srcDirectory + "/main/resources/";
182 testResourcesDirectory = srcDirectory + "/test/resources/";
183
184 build.setDirectory( buildDirectory );
185 build.setOutputDirectory( outputDirectory );
186 build.setTestOutputDirectory( testOutputDirectory );
187 }
188
189 public void setupBuildEnvironment()
190 throws Exception
191 {
192
193
194
195 if ( !FileUtils.fileExists( resourcesDirectory ) )
196 {
197 FileUtils.mkdir( resourcesDirectory );
198 }
199
200 if ( !FileUtils.fileExists( testResourcesDirectory ) )
201 {
202 FileUtils.mkdir( testResourcesDirectory );
203 }
204
205 createDirectories( resourcesDirectory, testResourcesDirectory );
206 createFiles( resourcesDirectory, testResourcesDirectory );
207 setupRootFiles();
208
209
210 if ( !FileUtils.fileExists( outputDirectory ) )
211 {
212 FileUtils.mkdir( outputDirectory );
213 }
214
215 if ( !FileUtils.fileExists( testOutputDirectory ) )
216 {
217 FileUtils.mkdir( testOutputDirectory );
218 }
219
220 setupTargetFiles();
221 }
222
223 private void createDirectories( String parent, String testparent )
224 {
225 File currentDirectory;
226
227 for ( String aDirectoryList : directoryList )
228 {
229 currentDirectory = new File( parent, "/" + aDirectoryList );
230
231 if ( !currentDirectory.exists() )
232 {
233 currentDirectory.mkdirs();
234 }
235
236
237 currentDirectory = new File( testparent, "/" + aDirectoryList );
238
239 if ( !currentDirectory.exists() )
240 {
241 currentDirectory.mkdirs();
242 }
243 }
244 }
245
246 private List<String> getList( int type )
247 {
248 ArrayList<String> retVal = null;
249
250 switch ( type )
251 {
252 case SOURCE_FILE :
253 retVal = sourceFileList;
254 break;
255 case OUTPUT_FILE :
256 retVal = targetClassesList;
257 break;
258 case RESOURCES_FILE :
259 retVal = resourcesFileList;
260 break;
261 case ROOT_FILE :
262 retVal = rootFileList;
263 break;
264 }
265
266 return retVal;
267 }
268
269 private void createFiles( String parent, int type )
270 {
271 File currentFile;
272 List<String> list = getList( type );
273
274
275 if ( list == null )
276 {
277 return;
278 }
279
280 for ( String aList : list )
281 {
282 currentFile = new File( parent, aList );
283
284
285
286 if ( !currentFile.getParentFile().exists() )
287 {
288 currentFile.getParentFile().mkdirs();
289 }
290
291 if ( !currentFile.exists() )
292 {
293 try
294 {
295 currentFile.createNewFile();
296 populateFile( currentFile, RESOURCES_FILE );
297 }
298 catch ( IOException io )
299 {
300
301 }
302 }
303 }
304 }
305
306 private void setupRootFiles()
307 {
308 createFiles( testRootDir, ROOT_FILE );
309 }
310
311 private void setupTargetFiles()
312 {
313 createFiles( getOutputDirectory(), OUTPUT_FILE );
314 }
315
316 private void createFiles( String parent, String testparent )
317 {
318 createFiles( parent, RESOURCES_FILE );
319 createFiles( testparent, RESOURCES_FILE );
320 }
321
322 private void populateFile( File file, int type )
323 {
324 FileOutputStream outputStream;
325 String data = dataMap.get( file.getName() );
326
327 if ( ( data != null ) && file.exists() )
328 {
329 try
330 {
331 outputStream = new FileOutputStream( file );
332 outputStream.write( data.getBytes() );
333 outputStream.flush();
334 outputStream.close();
335 }
336 catch ( IOException ex )
337 {
338
339 }
340 }
341 }
342 }