You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by bw...@apache.org on 2003/02/13 11:37:27 UTC
cvs commit: jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/maven DependencyResolverInterface.java WerkzDependencyResolver.java GraphDependencyResolver.java ReactorTag.java DependencyResolver.java
bwalding 2003/02/13 02:37:27
Modified: src/java/org/apache/maven/jelly/tags/maven ReactorTag.java
DependencyResolver.java
Added: src/java/org/apache/maven/jelly/tags/maven
DependencyResolverInterface.java
WerkzDependencyResolver.java
GraphDependencyResolver.java
Log:
o MAVEN-267: Made new dependency resolver use Werkz
Revision Changes Path
1.19 +3 -3 jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/maven/ReactorTag.java
Index: ReactorTag.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/maven/ReactorTag.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- ReactorTag.java 13 Feb 2003 03:28:03 -0000 1.18
+++ ReactorTag.java 13 Feb 2003 10:37:27 -0000 1.19
@@ -303,7 +303,7 @@
projectIncludes = getIncludes();
}
- System.out.println( "Starting the reactor (this may take a while)..." );
+ System.out.println( "Starting the reactor..." );
List projects = null;
try
1.3 +98 -163 jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/maven/DependencyResolver.java
Index: DependencyResolver.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/maven/DependencyResolver.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DependencyResolver.java 13 Jan 2003 17:21:42 -0000 1.2
+++ DependencyResolver.java 13 Feb 2003 10:37:27 -0000 1.3
@@ -3,7 +3,7 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2002 The Apache Software Foundation. All rights
+ * Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -26,12 +26,12 @@
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
- * "Apache MavenSession" must not be used to endorse or promote products
+ * "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
- * "Apache MavenSession", nor may "Apache" appear in their name, without
+ * "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
@@ -56,175 +56,110 @@
* ====================================================================
*/
+import java.util.Iterator;
+import java.util.List;
-import org.apache.commons.graph.domain.dependency.DependencyGraph;
-import org.apache.maven.project.Dependency;
import org.apache.maven.project.Project;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
/**
- * Takes a list of maven projects and determines the overall
- * dependency ordering among the project.
- *
- * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
- *
+ * @author <a href="bwalding@apache.org">Ben Walding</a>
* @version $Id$
*/
-public class DependencyResolver
+public class DependencyResolver implements DependencyResolverInterface
{
- /** The dependency graph. */
- private DependencyGraph dependencyGraph;
-
- /** List of maven projects to analyse. */
- private List projects;
- /** Flag to indicate whether the graph has been built. */
- private boolean graphBuilt;
+ private final DependencyResolverInterface impl;
- /**
- * Default constructor.
- */
- public DependencyResolver()
- {
- dependencyGraph = new DependencyGraph();
- projects = new ArrayList();
- graphBuilt = false;
+ /**
+ * Creates the dependency resolver. The implementation is chosen by the
+ * System property "maven.core.dependencyresolver". By default it is
+ * the WerkzDependencyResolver. Ultimately I see this proxy being removed.
+ * @see java.lang.Object#Object()
+ */
+ public DependencyResolver()
+ {
+ String prop = "maven.core.dependencyresolver";
+ String type = System.getProperty(prop);
+ try
+ {
+
+ if (type == null)
+ {
+ impl = new WerkzDependencyResolver();
+ }
+ else
+ {
+ impl = (DependencyResolverInterface) Class.forName(type).newInstance();
+ }
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Unable to create " + type, e);
+ }
+ }
+
+ /**
+ * Creates the dependency resolver with a specific implementation
+ * @param impl
+ */
+ public DependencyResolver(DependencyResolverInterface impl)
+ {
+ this.impl = impl;
+ }
+
+ /**
+ * @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#clear()
+ */
+ public void clear()
+ {
+ impl.clear();
+ }
+
+ /**
+ * @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#setProjects(java.util.List)
+ */
+ public void setProjects(List projects)
+ {
+ impl.setProjects(projects);
+ }
+
+ /**
+ * @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#getSortedDependencies(org.apache.maven.project.Project)
+ */
+ public List getSortedDependencies(Project project) throws Exception
+ {
+ return impl.getSortedDependencies(project);
+ }
+
+ /**
+ * @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#getSortedDependencies(org.apache.maven.project.Project, boolean)
+ */
+ public List getSortedDependencies(Project project, boolean sourceBuild) throws Exception
+ {
+ return impl.getSortedDependencies(project, sourceBuild);
+ }
+
+ /**
+ * @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#getSortedDependencies(boolean)
+ */
+ public List getSortedDependencies(boolean sourceBuild) throws Exception
+ {
+ return impl.getSortedDependencies(sourceBuild);
+ }
+
+ public static Project getProject(List projects, String id)
+ {
+ Iterator iter = projects.iterator();
+ while (iter.hasNext())
+ {
+ Project project = (Project) iter.next();
+ if (project.getId().equals(id))
+ {
+ return project;
+ }
}
+ return null;
- /**
- * Clear the project list and set the graph built flag to false.
- */
- public void clear()
- {
- graphBuilt = false;
- projects.clear();
- }
-
- /**
- * Set a list of projects to process.
- *
- * @param projects List of projects.
- */
- public void setProjects( List projects )
- {
- this.projects = projects;
- }
+ }
- /**
- * Get the list of projects in dependency sorted order.
- *
- * @param project The project to use as the head of the graph.
- * @return The list of projects.
- * @throws Exception If an error occurs while processing the graph.
- */
- public List getSortedDependencies( Project project )
- throws Exception
- {
- return getSortedDependencies( project, false );
- }
-
- /**
- * Get the list of projects in dependency sorted order.
- *
- * @param project The project to use as the head of the graph.
- * @param sourceBuild Indicate we are performing a source build.
- * @return The list of projects.
- * @throws Exception If an error occurs while processing the graph.
- */
- public List getSortedDependencies( Project project, boolean sourceBuild )
- throws Exception
- {
- buildGraph();
- List sortedDependencies = dependencyGraph.getSortedDependencies( project );
-
- if ( sourceBuild )
- {
- return sortedDependencies;
- }
-
- return getBinaryDependencies( sortedDependencies );
- }
-
- /**
- * Get the list of projects in dependency sorted order.
- *
- * @param sourceBuild Flag to indicate we are performing a source build.
- * @return The list of projects.
- * @throws Exception If an error occurs while processing the graph.
- */
- public List getSortedDependencies( boolean sourceBuild )
- throws Exception
- {
- buildGraph();
-
- List sortedDependencies = dependencyGraph.getSortedDependencies();
-
- if ( sourceBuild )
- {
- return sortedDependencies;
- }
-
- return getBinaryDependencies( sortedDependencies );
- }
-
- /**
- * Get dependency graph for a build based on binary inputs.
- *
- * @param sortedDependencies List of projects that have been dependency sorted.
- * @return The list of projects.
- */
- private List getBinaryDependencies( List sortedDependencies )
- {
- HashMap idMap = new HashMap();
- for ( Iterator i = projects.iterator(); i.hasNext(); )
- {
- Project p = (Project) i.next();
- idMap.put( p.getId() , p );
- }
-
- List binaryDeps = new ArrayList();
- for ( Iterator i = sortedDependencies.iterator(); i.hasNext(); )
- {
- Project p = (Project) i.next();
-
- Project dep = (Project) idMap.get( p.getId() );
- if ( dep != null )
- {
- binaryDeps.add( dep );
- }
- }
-
- return binaryDeps;
- }
-
- /**
- * Build the dependency graph.
- */
- private void buildGraph()
- {
- if ( graphBuilt )
- {
- return;
- }
-
- for ( Iterator i = projects.iterator(); i.hasNext(); )
- {
- Project p = (Project) i.next();
-
- ArrayList dependencies = new ArrayList();
- for ( Iterator j = p.getDependencies().iterator(); j.hasNext(); )
- {
- Dependency d = (Dependency) j.next();
- Project dependentProject = new Project();
- dependentProject.setId( d.getId() );
- dependencies.add( dependentProject );
- }
-
- dependencyGraph.addDependencies( p, dependencies );
- }
- graphBuilt = true;
- }
}
1.1 jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/maven/DependencyResolverInterface.java
Index: DependencyResolverInterface.java
===================================================================
package org.apache.maven.jelly.tags.maven;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
import org.apache.maven.project.Project;
import java.util.List;
/**
* @author <a href="bwalding@apache.org">Ben Walding</a>
* @version $Id: DependencyResolverInterface.java,v 1.1 2003/02/13 10:37:27 bwalding Exp $
*/
public interface DependencyResolverInterface
{
/**
* Clear the project list and set the graph built flag to false.
*/
void clear();
/**
* Set a list of projects to process.
*
* @param projects List of projects.
*/
void setProjects(List projects);
/**
* Get the list of projects in dependency sorted order.
*
* @param project The project to use as the head of the graph.
* @return The list of projects.
* @throws Exception If an error occurs while processing the graph.
*/
List getSortedDependencies(Project project) throws Exception;
/**
* Get the list of projects in dependency sorted order.
*
* @param project The project to use as the head of the graph.
* @param sourceBuild Indicate we are performing a source build.
* @return The list of projects.
* @throws Exception If an error occurs while processing the graph.
*/
List getSortedDependencies(Project project, boolean sourceBuild) throws Exception;
/**
* Get the list of projects in dependency sorted order.
*
* @param sourceBuild Flag to indicate we are performing a source build.
* @return The list of projects.
* @throws Exception If an error occurs while processing the graph.
*/
List getSortedDependencies(boolean sourceBuild) throws Exception;
}
1.1 jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/maven/WerkzDependencyResolver.java
Index: WerkzDependencyResolver.java
===================================================================
package org.apache.maven.jelly.tags.maven;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.project.Dependency;
import org.apache.maven.project.Project;
import com.werken.werkz.Action;
import com.werken.werkz.Goal;
import com.werken.werkz.Session;
import com.werken.werkz.WerkzProject;
/**
* THIS CLASS IS NOT THREADSAFE.
* @author <a href="bwalding@apache.org">Ben Walding</a>
* @version $Id: WerkzDependencyResolver.java,v 1.1 2003/02/13 10:37:27 bwalding Exp $
*/
public class WerkzDependencyResolver implements DependencyResolverInterface
{
private List projects = new ArrayList();
private final List sortedGoals = new ArrayList();
private Goal doItAll = null;
private WerkzProject wproject = null;
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#clear()
*/
public void clear()
{
projects.clear();
doItAll = null;
}
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#setProjects(java.util.List)
*/
public void setProjects(List projects)
{
this.projects = projects;
}
/**
* Retrieves the existing goal for a project (based on Id).
* If no goal exists, an exception is thrown.
* @param project
* @return ProjectGoal
*/
protected ProjectGoal getExistingGoal(Project project)
{
ProjectGoal goal = (ProjectGoal) wproject.getGoal(project.getId());
if (goal == null)
{
throw new NullPointerException("No goal exists : " + project.getId());
}
else
{
return goal;
}
}
/**
* Retrieves the existing goal for a project (based on Id).
* If no goal exists, one is created for this given project.
* @param project
* @param sourceBuild
* @return ProjectGoal
*/
protected ProjectGoal getOrCreateGoal(Project project, boolean sourceBuild)
{
ProjectGoal goal = (ProjectGoal) wproject.getGoal(project.getId());
if (goal == null)
{
goal = new ProjectGoal(project, sourceBuild);
goal.setAction(new ListAddAction(goal, sortedGoals));
wproject.addGoal(goal);
}
return goal;
}
/**
* Builds the projects / dependencies into an internal acyclic directed graph
* @throws Exception
*/
public void buildGraph() throws Exception
{
if (doItAll != null)
{
return;
}
try
{
wproject = new WerkzProject();
doItAll = new Goal("DO_IT_ALL");
doItAll.setAction(new DummyAction());
//Initialise all the true goals of the system
Iterator iter = projects.iterator();
while (iter.hasNext())
{
Project project = (Project) iter.next();
Goal projectGoal = getOrCreateGoal(project, true);
doItAll.addPrecursor(projectGoal);
}
//Now add the dependencies
iter = projects.iterator();
while (iter.hasNext())
{
Project project = (Project) iter.next();
Goal projectGoal = getExistingGoal(project);
Iterator depIter = project.getDependencies().iterator();
while (depIter.hasNext())
{
Dependency dep = (Dependency) depIter.next();
Project depProject = new Project();
depProject.setId(dep.getId());
Goal depGoal = getOrCreateGoal(depProject, false);
projectGoal.addPrecursor(depGoal);
}
}
}
catch (Exception e)
{
doItAll = null;
wproject = new WerkzProject();
}
}
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#getSortedDependencies(org.apache.maven.project.Project)
*/
public List getSortedDependencies(Project project) throws Exception
{
return getSortedDependencies(project, false);
}
/**
* Filters the given list of goals, and returns the associated projects
*
* If param:sourceBuild == true, include goal
* If param:sourceBuild == false, include goal if goal:sourceBuild == true
* @param goals
* @param sourceBuild
* @return List
*/
public List getProjects(List goals, boolean sourceBuild)
{
List result = new ArrayList();
Iterator iter = goals.iterator();
while (iter.hasNext())
{
ProjectGoal goal = (ProjectGoal) iter.next();
if ((sourceBuild) || (!sourceBuild && goal.getSourceBuild()))
{
result.add(((ProjectGoal) goal).getProject());
}
}
return result;
}
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#getSortedDependencies(org.apache.maven.project.Project, boolean)
*/
public List getSortedDependencies(Project project, boolean sourceBuild) throws Exception
{
buildGraph();
sortedGoals.clear();
Session session = new Session();
ProjectGoal g = getExistingGoal(project);
g.attain(session);
return getProjects(sortedGoals, sourceBuild);
}
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#getSortedDependencies(boolean)
*/
public List getSortedDependencies(boolean sourceBuild) throws Exception
{
buildGraph();
sortedGoals.clear();
Session session = new Session();
doItAll.attain(session);
return getProjects(sortedGoals, sourceBuild);
}
}
/**
* Simple action that adds the goal to a list when it is invoked
*/
class ListAddAction implements Action
{
private List list;
private Goal goal;
public ListAddAction(Goal goal, List list)
{
this.list = list;
this.goal = goal;
}
public boolean requiresAction()
{
return true;
}
public void performAction() throws Exception
{
list.add(goal);
}
}
/**
* "Do nothing" action
*/
class DummyAction implements Action
{
public boolean requiresAction()
{
return false;
}
public void performAction() throws Exception
{
}
}
/**
* Overrides Goal to add some extra project information
*/
class ProjectGoal extends Goal
{
private final Project project;
private final boolean sourceBuild;
public ProjectGoal(Project project, boolean sourceBuild)
{
super(project.getId());
this.project = project;
this.sourceBuild = sourceBuild;
}
public Project getProject()
{
return project;
}
public boolean getSourceBuild()
{
return sourceBuild;
}
}
1.1 jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/maven/GraphDependencyResolver.java
Index: GraphDependencyResolver.java
===================================================================
package org.apache.maven.jelly.tags.maven;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache MavenSession" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache MavenSession", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
import org.apache.commons.graph.domain.dependency.DependencyGraph;
import org.apache.maven.project.Dependency;
import org.apache.maven.project.Project;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/**
* Takes a list of maven projects and determines the overall
* dependency ordering among the project.
*
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
*
* @version $Id: GraphDependencyResolver.java,v 1.1 2003/02/13 10:37:27 bwalding Exp $
*/
public class GraphDependencyResolver implements DependencyResolverInterface
{
/** The dependency graph. */
private DependencyGraph dependencyGraph;
/** List of maven projects to analyse. */
private List projects;
/** Flag to indicate whether the graph has been built. */
private boolean graphBuilt;
/**
* Default constructor.
*/
public GraphDependencyResolver()
{
dependencyGraph = new DependencyGraph();
projects = new ArrayList();
graphBuilt = false;
}
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#clear()
*/
public void clear()
{
graphBuilt = false;
projects.clear();
}
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#setProjects(List)
*/
public void setProjects(List projects)
{
this.projects = projects;
}
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#getSortedDependencies(Project)
*/
public List getSortedDependencies(Project project) throws Exception
{
return getSortedDependencies(project, false);
}
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#getSortedDependencies(Project, boolean)
*/
public List getSortedDependencies(Project project, boolean sourceBuild) throws Exception
{
buildGraph();
List sortedDependencies = dependencyGraph.getSortedDependencies(project);
if (sourceBuild)
{
return sortedDependencies;
}
return getBinaryDependencies(sortedDependencies);
}
/**
* @see org.apache.maven.jelly.tags.maven.DependencyResolverInterface#getSortedDependencies(boolean)
*/
public List getSortedDependencies(boolean sourceBuild) throws Exception
{
buildGraph();
List sortedDependencies = dependencyGraph.getSortedDependencies();
if (sourceBuild)
{
return sortedDependencies;
}
return getBinaryDependencies(sortedDependencies);
}
/**
* Get dependency graph for a build based on binary inputs.
*
* @param sortedDependencies List of projects that have been dependency sorted.
* @return The list of projects.
*/
private List getBinaryDependencies(List sortedDependencies)
{
HashMap idMap = new HashMap();
for (Iterator i = projects.iterator(); i.hasNext();)
{
Project p = (Project) i.next();
idMap.put(p.getId(), p);
}
List binaryDeps = new ArrayList();
for (Iterator i = sortedDependencies.iterator(); i.hasNext();)
{
Project p = (Project) i.next();
Project dep = (Project) idMap.get(p.getId());
if (dep != null)
{
binaryDeps.add(dep);
}
}
return binaryDeps;
}
/**
* Build the dependency graph.
*/
private void buildGraph()
{
if (graphBuilt)
{
return;
}
for (Iterator i = projects.iterator(); i.hasNext();)
{
Project p = (Project) i.next();
ArrayList dependencies = new ArrayList();
for (Iterator j = p.getDependencies().iterator(); j.hasNext();)
{
Dependency d = (Dependency) j.next();
Project dependentProject = new Project();
dependentProject.setId(d.getId());
dependencies.add(dependentProject);
}
dependencyGraph.addDependencies(p, dependencies);
}
graphBuilt = true;
}
}