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