01: package org.andromda.maven.plugin.andromdapp.utils;
02:
03: import java.io.File;
04:
05: import java.util.HashMap;
06: import java.util.Iterator;
07: import java.util.Map;
08:
09: import org.apache.maven.execution.MavenSession;
10: import org.apache.maven.profiles.DefaultProfileManager;
11: import org.apache.maven.project.MavenProject;
12: import org.apache.maven.project.MavenProjectBuilder;
13: import org.apache.maven.project.ProjectBuildingException;
14:
15: /**
16: * Contains ulitities for dealing with Maven projects.
17: *
18: * @author Chad Brandon
19: */
20: public class ProjectUtils {
21: /**
22: * Stores previously discovered projects.
23: */
24: private static final Map projectCache = new HashMap();
25:
26: /**
27: * Gets a project for the given <code>pom</code>.
28: *
29: * @param pom the pom from which to build the project.
30: * @return the built project.
31: * @throws ProjectBuildingException
32: */
33: public static synchronized MavenProject getProject(
34: final MavenProjectBuilder projectBuilder,
35: final MavenSession session, final File pom)
36: throws ProjectBuildingException {
37: // - first attempt to get a project from the cache
38: MavenProject project = (MavenProject) projectCache.get(pom);
39: if (project == null) {
40: // - next attempt to get the existing project from the session
41: project = getProjectFromSession(session, pom);
42: if (project == null) {
43: // - if we didn't find it in the session, create it
44: project = projectBuilder.build(pom, session
45: .getLocalRepository(),
46: new DefaultProfileManager(session
47: .getContainer()));
48: }
49: projectCache.put(pom, project);
50: }
51: return project;
52: }
53:
54: /**
55: * The POM file name.
56: */
57: private static final String POM_FILE = "pom.xml";
58:
59: /**
60: * Attempts to retrieve the Maven project for the given <code>pom</code>.
61: *
62: * @param pom the POM to find.
63: * @return the maven project with the matching POM.
64: */
65: private static MavenProject getProjectFromSession(
66: final MavenSession session, final File pom) {
67: MavenProject foundProject = null;
68: for (final Iterator projectIterator = session
69: .getSortedProjects().iterator(); projectIterator
70: .hasNext();) {
71: final MavenProject project = (MavenProject) projectIterator
72: .next();
73: final File projectPom = new File(project.getBasedir(),
74: POM_FILE);
75: if (projectPom.equals(pom)) {
76: foundProject = project;
77: }
78: }
79: return foundProject;
80: }
81: }
|