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 private boolean isValidPath(String path) {
140 boolean bRetVal = true;
141
142 if (path.startsWith("c:") || path.startsWith("..") || path.startsWith("/") || path.startsWith("\\")) {
143 bRetVal = false;
144 }
145
146 return bRetVal;
147 }
148
149 private void setupBuild() {
150
151
152
153 srcDirectory = testRootDir + "/src";
154 buildDirectory = testRootDir + "/target";
155 outputDirectory = buildDirectory + "/classes";
156 testOutputDirectory = buildDirectory + "/test-classes";
157 resourcesDirectory = srcDirectory + "/main/resources/";
158 testResourcesDirectory = srcDirectory + "/test/resources/";
159
160 build.setDirectory(buildDirectory);
161 build.setOutputDirectory(outputDirectory);
162 build.setTestOutputDirectory(testOutputDirectory);
163 }
164
165 public void setupBuildEnvironment() throws Exception {
166
167
168
169 if (!FileUtils.fileExists(resourcesDirectory)) {
170 FileUtils.mkdir(resourcesDirectory);
171 }
172
173 if (!FileUtils.fileExists(testResourcesDirectory)) {
174 FileUtils.mkdir(testResourcesDirectory);
175 }
176
177 createDirectories(resourcesDirectory, testResourcesDirectory);
178 createFiles(resourcesDirectory, testResourcesDirectory);
179 setupRootFiles();
180
181
182 if (!FileUtils.fileExists(outputDirectory)) {
183 FileUtils.mkdir(outputDirectory);
184 }
185
186 if (!FileUtils.fileExists(testOutputDirectory)) {
187 FileUtils.mkdir(testOutputDirectory);
188 }
189
190 setupTargetFiles();
191 }
192
193 private void createDirectories(String parent, String testparent) {
194 for (String aDirectoryList : directoryList) {
195 File currentDirectory = new File(parent, "/" + aDirectoryList);
196
197 if (!currentDirectory.exists()) {
198 currentDirectory.mkdirs();
199 }
200
201
202 currentDirectory = new File(testparent, "/" + aDirectoryList);
203
204 if (!currentDirectory.exists()) {
205 currentDirectory.mkdirs();
206 }
207 }
208 }
209
210 private List<String> getList(int type) {
211 ArrayList<String> retVal = null;
212
213 switch (type) {
214 case SOURCE_FILE:
215 retVal = sourceFileList;
216 break;
217 case OUTPUT_FILE:
218 retVal = targetClassesList;
219 break;
220 case RESOURCES_FILE:
221 retVal = resourcesFileList;
222 break;
223 case ROOT_FILE:
224 retVal = rootFileList;
225 break;
226 }
227
228 return retVal;
229 }
230
231 private void createFiles(String parent, int type) throws IOException {
232 List<String> list = getList(type);
233
234 if (list == null) {
235 return;
236 }
237
238 for (String aList : list) {
239 File currentFile = new File(parent, aList);
240
241
242
243 if (!currentFile.getParentFile().exists()) {
244 currentFile.getParentFile().mkdirs();
245 }
246
247 if (!currentFile.exists()) {
248 currentFile.createNewFile();
249 populateFile(currentFile, RESOURCES_FILE);
250 }
251 }
252 }
253
254 private void setupRootFiles() throws IOException {
255 createFiles(testRootDir, ROOT_FILE);
256 }
257
258 private void setupTargetFiles() throws IOException {
259 createFiles(getOutputDirectory(), OUTPUT_FILE);
260 }
261
262 private void createFiles(String parent, String testparent) throws IOException {
263 createFiles(parent, RESOURCES_FILE);
264 createFiles(testparent, RESOURCES_FILE);
265 }
266
267 private void populateFile(File file, int type) throws IOException {
268 String data = dataMap.get(file.getName());
269
270 if ((data != null) && file.exists()) {
271 try (FileOutputStream outputStream = new FileOutputStream(file)) {
272 outputStream.write(data.getBytes());
273 }
274 }
275 }
276 }