1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.resources.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
27 import org.apache.maven.model.Build;
28 import org.codehaus.plexus.util.FileUtils;
29
30 import static org.apache.commons.io.FileUtils.deleteDirectory;
31
32 public class MavenProjectBuildStub extends MavenProjectBasicStub {
33 protected Build build;
34
35 protected String srcDirectory;
36
37 protected String targetDirectory;
38
39 protected String buildDirectory;
40
41 protected String outputDirectory;
42
43 protected String testOutputDirectory;
44
45 protected String resourcesDirectory;
46
47 protected String testResourcesDirectory;
48
49 protected String targetResourceDirectory;
50
51 protected String targetTestResourcesDirectory;
52
53 protected ArrayList<String> fileList;
54
55 protected ArrayList<String> directoryList;
56
57 protected HashMap<String, String> dataMap;
58
59 public MavenProjectBuildStub(String key) throws Exception {
60 super(key);
61
62 build = new Build();
63 fileList = new ArrayList<>();
64 directoryList = new ArrayList<>();
65 dataMap = new HashMap<>();
66 setupBuild();
67 }
68
69 public void addDirectory(String name) {
70 if (isValidPath(name)) {
71 directoryList.add(name);
72 }
73 }
74
75 public void setOutputDirectory(String dir) {
76 outputDirectory = buildDirectory + "/" + dir;
77 build.setOutputDirectory(outputDirectory);
78 }
79
80 public void addFile(String name) {
81 if (isValidPath(name)) {
82 fileList.add(name);
83 }
84 }
85
86 public void addFile(String name, String data) {
87 File fileName = new File(name);
88
89 addFile(name);
90 dataMap.put(fileName.getName(), data);
91 }
92
93 public String getOutputDirectory() {
94 return outputDirectory;
95 }
96
97 public String getTestOutputDirectory() {
98 return testOutputDirectory;
99 }
100
101 public String getResourcesDirectory() {
102 return resourcesDirectory;
103 }
104
105 public String getTestResourcesDirectory() {
106 return testResourcesDirectory;
107 }
108
109 public Build getBuild() {
110 return build;
111 }
112
113
114
115
116
117
118
119
120
121
122 private boolean isValidPath(String path) {
123 return !path.startsWith("c:") && !path.startsWith("..") && !path.startsWith("/") && !path.startsWith("\\");
124 }
125
126 private void setupBuild() {
127
128
129
130 srcDirectory = testRootDir + "/src";
131 buildDirectory = testRootDir + "/target";
132 outputDirectory = buildDirectory + "/classes";
133 testOutputDirectory = buildDirectory + "/test-classes";
134 resourcesDirectory = srcDirectory + "/main/resources/";
135 testResourcesDirectory = srcDirectory + "/test/resources/";
136
137 build.setDirectory(buildDirectory);
138 build.setOutputDirectory(outputDirectory);
139 build.setTestOutputDirectory(testOutputDirectory);
140 }
141
142 public void cleanBuildEnvironment() throws Exception {
143 if (FileUtils.fileExists(resourcesDirectory)) {
144 deleteDirectory(new File(resourcesDirectory));
145 }
146
147 if (FileUtils.fileExists(testResourcesDirectory)) {
148 deleteDirectory(new File(testResourcesDirectory));
149 }
150
151 if (FileUtils.fileExists(outputDirectory)) {
152 deleteDirectory(new File(outputDirectory));
153 }
154
155 if (FileUtils.fileExists(testOutputDirectory)) {
156 deleteDirectory(new File(testOutputDirectory));
157 }
158 }
159
160 public void setupBuildEnvironment() throws Exception {
161
162
163
164 if (!FileUtils.fileExists(resourcesDirectory)) {
165 FileUtils.mkdir(resourcesDirectory);
166 }
167
168 if (!FileUtils.fileExists(testResourcesDirectory)) {
169 FileUtils.mkdir(testResourcesDirectory);
170 }
171
172 createDirectories(resourcesDirectory, testResourcesDirectory);
173 createFiles(resourcesDirectory, testResourcesDirectory);
174
175
176 if (!FileUtils.fileExists(outputDirectory)) {
177 FileUtils.mkdir(outputDirectory);
178 }
179
180 if (!FileUtils.fileExists(testOutputDirectory)) {
181 FileUtils.mkdir(testOutputDirectory);
182 }
183 }
184
185 private void createDirectories(String parent, String testparent) {
186 File currentDirectory;
187
188 for (String directory : directoryList) {
189 currentDirectory = new File(parent, "/" + directory);
190
191 if (!currentDirectory.exists()) {
192 currentDirectory.mkdirs();
193 }
194
195
196 currentDirectory = new File(testparent, "/" + directory);
197
198 if (!currentDirectory.exists()) {
199 currentDirectory.mkdirs();
200 }
201 }
202 }
203
204 private void createFiles(String parent, String testparent) throws IOException {
205 File currentFile;
206
207 for (String file : fileList) {
208 currentFile = new File(parent, file);
209
210
211
212 if (!currentFile.getParentFile().exists()) {
213 currentFile.getParentFile().mkdirs();
214 }
215
216 if (!currentFile.exists()) {
217 try {
218 currentFile.createNewFile();
219 populateFile(currentFile);
220 } catch (IOException io) {
221
222 }
223 }
224
225
226 currentFile = new File(testparent, file);
227
228 if (!currentFile.getParentFile().exists()) {
229 currentFile.getParentFile().mkdirs();
230 }
231
232 if (!currentFile.exists()) {
233 currentFile.createNewFile();
234 populateFile(currentFile);
235 }
236 }
237 }
238
239 private void populateFile(File file) throws IOException {
240 String data = dataMap.get(file.getName());
241
242 if ((data != null) && file.exists()) {
243 try (FileOutputStream outputStream = new FileOutputStream(file)) {
244 outputStream.write(data.getBytes());
245 }
246 }
247 }
248 }