1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.util.eclipse;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.maven.artifact.repository.ArtifactRepository;
27 import org.apache.maven.plugin.logging.Log;
28 import org.apache.maven.profiles.ProfileManager;
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.project.MavenProjectBuilder;
31 import org.codehaus.plexus.util.DirectoryScanner;
32
33 import de.smartics.maven.util.project.MavenContext;
34
35
36
37
38
39
40
41
42 public final class WorkspaceUtils
43 {
44
45
46
47
48
49
50
51
52
53 private static final String DEFAULT_POM_FILENAME = "pom.xml";
54
55
56
57
58
59
60
61
62
63
64 private WorkspaceUtils()
65 {
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public static Set<MavenProject> fetchMavenProjects(
101 final MavenContext context,
102 final File workspaceDir,
103 final Set<String> includes,
104 final Set<String> excludes)
105 {
106 return fetchMavenProjects(context, workspaceDir, includes, excludes, false);
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public static Set<MavenProject> fetchMavenProjects(
134 final MavenContext context,
135 final File workspaceDir,
136 final Set<String> includes,
137 final Set<String> excludes,
138 final boolean filterSubprojects)
139 {
140 final Log log = context.getLog();
141 final Set<MavenProject> projects = new HashSet<MavenProject>();
142
143 final DirectoryScanner scanner =
144 createScanner(workspaceDir, includes, excludes);
145 final String[] dirs = scanner.getIncludedDirectories();
146 final MavenProjectBuilder projectBuilder = context.getProjectBuilder();
147 final ArtifactRepository artifactRepository =
148 context.getLocalArtifactRepository();
149 final ProfileManager profileManager = context.getProfileManager();
150 final Set<String> modules = new HashSet<String>();
151 for (String dirName : Arrays.asList(dirs))
152 {
153 final File dir = new File(workspaceDir, dirName);
154 if (dir.exists())
155 {
156 final File pomFile = new File(dir, DEFAULT_POM_FILENAME);
157 if (pomFile.exists())
158 {
159 try
160 {
161 final MavenProject project =
162 projectBuilder.build(pomFile, artifactRepository,
163 profileManager, false);
164 if (filterSubprojects)
165 {
166 addModules(log, modules, project);
167 }
168 final boolean isAdded = projects.add(project);
169 if (!isAdded)
170 {
171 if (log.isDebugEnabled())
172 {
173 final String projectId =
174 project.getGroupId() + ':' + project.getArtifactId();
175 final String message =
176 Messages.getString("project.skipped", projectId, pomFile);
177 log.debug(message);
178 }
179 }
180 else
181 {
182 if (log.isDebugEnabled())
183 {
184 final String projectId =
185 project.getGroupId() + ':' + project.getArtifactId();
186
187 final String message =
188 Messages.getString(filterSubprojects
189 ? "project.addedTemporary" : "project.added",
190 projectId, pomFile);
191 log.debug(message);
192 }
193 }
194 }
195 catch (final Exception e)
196 {
197 if (log.isInfoEnabled())
198 {
199 final String message =
200 Messages.getString("pomFile.unreadable", pomFile);
201 if (log.isDebugEnabled())
202 {
203 log.debug(message, e);
204 }
205 else
206 {
207 log.info(message);
208 }
209 }
210 }
211 }
212 }
213 }
214
215 if (filterSubprojects)
216 {
217 filterSubprojects(log, modules, projects);
218 }
219
220 return projects;
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 public static Set<String> fetchMavenProjectIds(
249 final MavenContext context,
250 final File workspaceDir,
251 final Set<String> includes,
252 final Set<String> excludes,
253 final boolean filterSubprojects)
254 {
255 final Set<MavenProject> projects =
256 fetchMavenProjects(context, workspaceDir, includes, excludes,
257 filterSubprojects);
258 final Set<String> ids = new HashSet<String>(projects.size());
259 for (MavenProject project : projects)
260 {
261 final String id = project.getGroupId() + ':' + project.getArtifactId();
262 ids.add(id);
263 }
264
265 return ids;
266 }
267
268
269
270
271
272
273
274
275
276
277
278 private static void filterSubprojects(
279 final Log log,
280 final Set<String> modules,
281 final Set<MavenProject> projects)
282 {
283 for (final Iterator<MavenProject> i = projects.iterator(); i.hasNext();)
284 {
285 final MavenProject project = i.next();
286 final File dir = project.getBasedir();
287 try
288 {
289 final String path = dir.getCanonicalPath();
290 if (modules.contains(path))
291 {
292 i.remove();
293 if (log.isDebugEnabled())
294 {
295 final String id =
296 project.getGroupId() + ':' + project.getArtifactId();
297 final String message =
298 Messages.getString("project.removed", id, path);
299 log.debug(message);
300 }
301 }
302 }
303 catch (final IOException e)
304 {
305
306 if (log.isWarnEnabled())
307 {
308 final String message =
309 Messages
310 .getString("project.filterFailure", dir.getAbsolutePath());
311 if (log.isDebugEnabled())
312 {
313 log.debug(message, e);
314 }
315 else
316 {
317 log.warn(message);
318 }
319 }
320 }
321 }
322 }
323
324
325
326
327
328
329
330
331
332 @SuppressWarnings("unchecked")
333 private static void addModules(
334 final Log log,
335 final Set<String> modules,
336 final MavenProject project)
337 {
338 final List<String> modulePathes = project.getModules();
339
340 final File baseDir = project.getBasedir();
341 for (String path : modulePathes)
342 {
343 File file = new File(baseDir, path);
344 try
345 {
346 if (file.exists())
347 {
348 modules.add(file.getCanonicalPath());
349 if (log.isDebugEnabled())
350 {
351 final String id =
352 project.getGroupId() + ':' + project.getArtifactId();
353 final String message =
354 Messages.getString("project.submoduleFound", id, file);
355 log.debug(message);
356 }
357 }
358 else
359 {
360 file = new File(path);
361 if (file.exists())
362 {
363 modules.add(file.getCanonicalPath());
364 if (log.isDebugEnabled())
365 {
366 final String id =
367 project.getGroupId() + ':' + project.getArtifactId();
368 final String message =
369 Messages.getString("project.submoduleFound", id, file);
370 log.debug(message);
371 }
372 }
373 }
374 }
375 catch (final IOException e)
376 {
377
378 if (log.isWarnEnabled())
379 {
380 final String message =
381 Messages.getString("project.moduleAddFailure", file);
382 if (log.isDebugEnabled())
383 {
384 log.debug(message, e);
385 }
386 else
387 {
388 log.warn(message);
389 }
390 }
391 }
392 }
393
394 }
395
396
397
398
399
400
401
402
403
404
405
406
407 private static DirectoryScanner createScanner(
408 final File rootDir,
409 final Set<String> includes,
410 final Set<String> excludes)
411 {
412 final DirectoryScanner scanner = new DirectoryScanner();
413 scanner.setBasedir(rootDir);
414 scanner.setCaseSensitive(true);
415 final String[] includesArray;
416 if (includes == null || includes.isEmpty())
417 {
418 includesArray = new String[]
419 { "*" };
420 }
421 else
422 {
423 includesArray = (String[]) includes.toArray(new String[includes.size()]);
424 }
425 scanner.setIncludes(includesArray);
426
427 if (excludes != null)
428 {
429 final String[] excludesArray =
430 (String[]) excludes.toArray(new String[excludes.size()]);
431 scanner.setExcludes(excludesArray);
432 }
433
434 scanner.scan();
435
436 return scanner;
437 }
438
439
440
441 }