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