001package org.andromda.maven.plugin.andromdapp.utils;
002
003import java.io.File;
004import java.util.HashMap;
005import java.util.Map;
006import org.apache.maven.execution.MavenSession;
007import org.apache.maven.plugin.logging.Log;
008import org.apache.maven.profiles.DefaultProfileManager;
009import org.apache.maven.project.MavenProject;
010import org.apache.maven.project.MavenProjectBuilder;
011import org.apache.maven.project.ProjectBuildingException;
012
013/**
014 * Contains utilities for dealing with Maven projects.
015 *
016 * @author Chad Brandon
017 */
018public class ProjectUtils
019{
020    /**
021     * Stores previously discovered projects.
022     */
023    private static final Map<File, MavenProject> projectCache = new HashMap<File, MavenProject>();
024
025    /**
026     * Gets a project for the given <code>pom</code>.
027     * @param projectBuilder
028     * @param session
029     * @param pom the pom from which to build the project.
030     * @param logger
031     * @return the built project.
032     * @throws ProjectBuildingException
033     */
034    public static synchronized MavenProject getProject(
035        final MavenProjectBuilder projectBuilder,
036        final MavenSession session,
037        final File pom,
038        final Log logger)
039        throws ProjectBuildingException
040    {
041        // - first attempt to get a project from the cache
042        MavenProject project = projectCache.get(pom);
043        if (project == null)
044        {
045            // - next attempt to get the existing project from the session
046            project = getProjectFromSession(session, pom);
047            if (project == null)
048            {
049                // - if we didn't find it in the session, create it
050                try
051                {
052                    project = projectBuilder.build(pom, session.getLocalRepository(),
053                            new DefaultProfileManager(session.getContainer()));
054                    projectCache.put(pom, project);
055                }
056                catch (Exception ex)
057                {
058                    if (logger.isDebugEnabled())
059                    {
060                        logger.debug("Failed to build project from pom: " + pom, ex);
061                    }
062                }
063            }
064        }
065        return project;
066    }
067
068    /**
069     * The POM file name.
070     */
071    private static final String POM_FILE = "pom.xml";
072
073    /**
074     * Attempts to retrieve the Maven project for the given <code>pom</code>.
075     *
076     * @param pom the POM to find.
077     * @return the maven project with the matching POM.
078     */
079    private static MavenProject getProjectFromSession(
080        final MavenSession session,
081        final File pom)
082    {
083        MavenProject foundProject = null;
084        for (final MavenProject project : session.getSortedProjects())
085        {
086            final File projectPom = new File(
087                    project.getBasedir(),
088                    POM_FILE);
089            if (projectPom.equals(pom))
090            {
091                foundProject = project;
092            }
093        }
094        return foundProject;
095    }
096}