1 package org.andromda.maven.plugin.andromdapp.utils;
2
3 import java.io.File;
4 import java.util.HashMap;
5 import java.util.Map;
6 import org.apache.maven.execution.MavenSession;
7 import org.apache.maven.plugin.logging.Log;
8 import org.apache.maven.profiles.DefaultProfileManager;
9 import org.apache.maven.project.MavenProject;
10 import org.apache.maven.project.MavenProjectBuilder;
11 import org.apache.maven.project.ProjectBuildingException;
12
13
14
15
16
17
18 public class ProjectUtils
19 {
20
21
22
23 private static final Map<File, MavenProject> projectCache = new HashMap<File, MavenProject>();
24
25
26
27
28
29
30
31
32
33
34 public static synchronized MavenProject getProject(
35 final MavenProjectBuilder projectBuilder,
36 final MavenSession session,
37 final File pom,
38 final Log logger)
39 throws ProjectBuildingException
40 {
41
42 MavenProject project = projectCache.get(pom);
43 if (project == null)
44 {
45
46 project = getProjectFromSession(session, pom);
47 if (project == null)
48 {
49
50 try
51 {
52 project = projectBuilder.build(pom, session.getLocalRepository(),
53 new DefaultProfileManager(session.getContainer()));
54 projectCache.put(pom, project);
55 }
56 catch (Exception ex)
57 {
58 if (logger.isDebugEnabled())
59 {
60 logger.debug("Failed to build project from pom: " + pom, ex);
61 }
62 }
63 }
64 }
65 return project;
66 }
67
68
69
70
71 private static final String POM_FILE = "pom.xml";
72
73
74
75
76
77
78
79 private static MavenProject getProjectFromSession(
80 final MavenSession session,
81 final File pom)
82 {
83 MavenProject foundProject = null;
84 for (final MavenProject project : session.getSortedProjects())
85 {
86 final File projectPom = new File(
87 project.getBasedir(),
88 POM_FILE);
89 if (projectPom.equals(pom))
90 {
91 foundProject = project;
92 }
93 }
94 return foundProject;
95 }
96 }