jvanzyl 02/05/28 14:27:05
Added: src/java/org/apache/maven/dependency DependencyResolver.java
PackageProjectMap.java ProjectResolver.java
Log:
New dependency package. Still very rough and primitive but in a day or
two we'll be sailing.
Revision Changes Path
1.1
jakarta-turbine-maven/src/java/org/apache/maven/dependency/DependencyResolver.java
Index: DependencyResolver.java
===================================================================
package org.apache.maven.dependency;
/* ====================================================================
* 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 Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 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.io.File;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import org.apache.maven.MavenUtils;
import org.apache.maven.project.Dependency;
import org.apache.maven.project.Project;
import org.apache.commons.graph.domain.dependency.DependencyGraph;
public class DependencyResolver
{
/**
* Dependency graph
*/
private DependencyGraph dependencyGraph;
private String projectsDirectory;
private List projects;
public DependencyResolver()
{
dependencyGraph = new DependencyGraph();
projects = new ArrayList();
}
public void setProjectsDirectory(String projectsDirectory)
{
this.projectsDirectory = projectsDirectory;
}
public void compute()
throws Exception
{
}
public List getSortedDependencies(String projectId)
throws Exception
{
String[] projectFiles = MavenUtils.getFiles(projectsDirectory,
"**/project.xml");
for (int i = 0; i < projectFiles.length; i++)
{
Project project = MavenUtils.getProject(new File(projectFiles[i]));
projects.add(project);
List dependencies = project.getDependencies();
List deps = new ArrayList();
for (Iterator j = project.getDependencies().iterator(); j.hasNext();)
{
Dependency dep = (Dependency) j.next();
deps.add(dep.getId());
}
dependencyGraph.addDependencies(project.getId(), deps);
}
return dependencyGraph.getSortedDependencies(projectId);
}
}
1.1
jakarta-turbine-maven/src/java/org/apache/maven/dependency/PackageProjectMap.java
Index: PackageProjectMap.java
===================================================================
package org.apache.maven.dependency;
/* ====================================================================
* 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 Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 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.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import org.apache.maven.MavenUtils;
import org.apache.maven.executor.AbstractExecutor;
import org.apache.maven.project.Project;
/**
* Task for creating the package -> project map so that after
* determining which classes are used in a project we can map
* these classes to a set of projects which contain these
* references.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
* @version $Id: PackageProjectMap.java,v 1.1 2002/05/28 21:27:05 jvanzyl Exp $
*/
public class PackageProjectMap
extends AbstractExecutor
{
/**
* The directory where all the Maven descriptors are stored.
*/
private File descriptorDir;
/**
* Where to write the resultant package -> project map.
*/
private File map;
/**
* Set the directory where all the Maven descriptors are stored.
*
* @param descriptorDir The directory where all the Maven descriptors are
* stored.
*/
public void setDescriptorDir(File descriptorDir)
{
this.descriptorDir = descriptorDir;
}
/**
* Get the directory where all the Maven descriptors are stored.
*
* @return The directory where all the Maven descriptors are stored.
*/
public File getDescriptorDir()
{
return descriptorDir;
}
/**
* Where to write the resultant package -> project map
*
* @param map The file where the package -> project map is stored
*/
public void setMap(File map)
{
this.map = map;
}
/**
* Get where to write the resultant package -> project map
*
* @return The file to write the resultant package -> project map to
*/
public File getMap()
{
return map;
}
/**
* Execute the the overall maven opertion.
*
* @throws Exception when an error occurs
*/
public void execute()
throws Exception
{
if (descriptorDir == null)
{
throw new Exception("descriptorDir attribute must be specified!");
}
if (map == null)
{
throw new Exception("map attribute must be specified!");
}
String[] projects =
MavenUtils.getFiles(descriptorDir.getAbsolutePath(), "*.xml");
Properties m = new Properties();
for (int i = 0; i < projects.length; i++)
{
Project p = MavenUtils.getProject(projects[i]);
String projectPackage = p.getPackage();
String projectId = p.getId();
// Some of the projects may not have packages.
if (projectPackage != null &&
projectPackage.trim().length() > 0 &&
projectId != null)
{
m.put(projectPackage, projectId);
}
}
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(map);
m.store(fos, "Package -> Project Map");
}
finally
{
if (fos != null)
{
fos.close();
}
}
}
}
1.1
jakarta-turbine-maven/src/java/org/apache/maven/dependency/ProjectResolver.java
Index: ProjectResolver.java
===================================================================
package org.apache.maven.dependency;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 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 [EMAIL PROTECTED]
*
* 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.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.Properties;
import java.util.HashMap;
import org.apache.maven.MavenUtils;
import org.apache.maven.java.SourceTool;
import org.apache.maven.java.ImportStatementVisitor;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
* @version $Id: ProjectResolver.java,v 1.1 2002/05/28 21:27:05 jvanzyl Exp $
*/
public class ProjectResolver
{
/**
* Base directory in which JAR files and class files will be searched for or
* just a JAR file.
*/
private File base;
/**
* Package references that are collected by mapping the class references to
* package references.
*/
private List packageReferences;
/** Packages to exclude from the final list of packages */
private List packageExcludes;
/** Package -> Project map */
private File mapFile;
/** Project references */
private List projectReferences;
/** used to process source code with a visitor */
private SourceTool st;
/** visitor to process import statements */
private ImportStatementVisitor isv;
/**
* Default constructor.
*/
public ProjectResolver()
{
st = new SourceTool();
isv = new ImportStatementVisitor();
st.setVisitor(isv);
packageReferences = new LinkedList();
packageExcludes = new LinkedList();
projectReferences = new LinkedList();
}
/**
* Set the packages to exclude from the final list found by the resolution
* process.
*
* @param packageExcludesList comma separate list of the packages to
* exclude from the final list of packages.
*/
public void setPackageExcludes(String packageExcludesList)
{
StringTokenizer st = new StringTokenizer(packageExcludesList, ",");
while (st.hasMoreTokens())
{
String packageExclude = st.nextToken();
packageExcludes.add(packageExclude);
}
}
/**
* Set the directory in which JARs and class files are searched for.
*
* @param base directory in which JARs and class files are searched for.
*/
public void setBase(File base)
{
this.base = base;
}
/**
* Set the location of the package -> project map.
*
* @param mapFile Location of the package -> project map.
*/
public void setMapFile(File mapFile)
{
this.mapFile = mapFile;
}
/**
* Get the package referenced determined by the resolution process.
*
* @return List The list of packages references found.
*/
public List getPackageReferences()
{
return packageReferences;
}
/**
* Get the projects referenced determined by the resolution process.
*
* @return List The list of project references found.
*/
public List getProjectReferences()
{
return projectReferences;
}
/**
* Resolve the package dependencies from the set of JARs and classes used as
* the input.
* @throws Exception when any error occurs
*/
public void resolveDependencies()
throws Exception
{
// Collect all the JAR and class file that we can find
// in the base or just process the JAR.
if (base.isDirectory())
{
String[] files =
MavenUtils.getFiles(base.getAbsolutePath(), "**/*.java");
for (int i = 0; i < files.length; i++)
{
String file = files[i];
st.parse(new FileInputStream(file));
}
}
for (Iterator j = isv.getImportStatements().iterator(); j.hasNext();)
{
String classReference = (String) j.next();
processClassReference(classReference);
}
Properties map = new Properties();
try
{
map.load(new FileInputStream(mapFile));
}
catch (Exception e)
{
e.printStackTrace();
}
HashMap projects = new HashMap();
List packageReferences = getPackageReferences();
for (Iterator i = packageReferences.iterator(); i.hasNext();)
{
String classReference = (String) i.next();
for (Iterator j = map.keySet().iterator(); j.hasNext();)
{
String packageKey = (String) j.next();
if (classReference.indexOf(packageKey) == 0)
{
Object o = map.get(packageKey);
projects.put(o, o);
}
}
}
projectReferences.addAll(projects.values());
}
/**
* Process an individual class file.
*
* @param classReference An individual class reference in the form of a
* string.
*/
private void processClassReference(String classReference)
{
boolean includePackage = true;
for (Iterator j = packageExcludes.iterator(); j.hasNext();)
{
String packageExclude = (String) j.next();
if (classReference.startsWith(packageExclude))
{
includePackage = false;
break;
}
}
if (includePackage)
{
packageReferences.add(classReference);
}
}
/**
* Used for testing only.
* @param args command line args
* @throws Exception when any error occurs
*/
public static void main(String[] args)
throws Exception
{
ProjectResolver bdr = new ProjectResolver();
bdr.setBase(new File(args[0]));
bdr.setPackageExcludes(args[1]);
bdr.setMapFile(new File(args[2]));
bdr.resolveDependencies();
List projectReferences = bdr.getProjectReferences();
for (Iterator i = projectReferences.iterator(); i.hasNext();)
{
System.out.println(i.next());
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>