View Javadoc
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   * Contains utilities for dealing with Maven projects.
15   *
16   * @author Chad Brandon
17   */
18  public class ProjectUtils
19  {
20      /**
21       * Stores previously discovered projects.
22       */
23      private static final Map<File, MavenProject> projectCache = new HashMap<File, MavenProject>();
24  
25      /**
26       * Gets a project for the given <code>pom</code>.
27       * @param projectBuilder
28       * @param session
29       * @param pom the pom from which to build the project.
30       * @param logger
31       * @return the built project.
32       * @throws ProjectBuildingException
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          // - first attempt to get a project from the cache
42          MavenProject project = projectCache.get(pom);
43          if (project == null)
44          {
45              // - next attempt to get the existing project from the session
46              project = getProjectFromSession(session, pom);
47              if (project == null)
48              {
49                  // - if we didn't find it in the session, create it
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       * The POM file name.
70       */
71      private static final String POM_FILE = "pom.xml";
72  
73      /**
74       * Attempts to retrieve the Maven project for the given <code>pom</code>.
75       *
76       * @param pom the POM to find.
77       * @return the maven project with the matching POM.
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  }