jvanzyl 02/04/16 11:33:28 Added: src/java/org/apache/maven ProjectResolverSource.java Log: Little tool for resolving project dependencies. Revision Changes Path 1.1 jakarta-turbine-maven/src/java/org/apache/maven/ProjectResolverSource.java Index: ProjectResolverSource.java =================================================================== package org.apache.maven; /* ==================================================================== * 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.io.InputStream; import java.util.Iterator; import java.util.List; import java.util.LinkedList; import java.util.jar.JarFile; import java.util.jar.JarEntry; import java.util.Map; import java.util.StringTokenizer; import java.util.Properties; import java.util.HashMap; import org.apache.maven.java.SourceTool; import org.apache.maven.java.ImportStatementVisitor; /** * <code>ProjectResolver</code> is an executor that will process a directory * looking for JAR and class files and turn the set class references it finds * into a set of project references which can be used to create an accurate set * of dependencies in a project descriptor. * * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a> * @version $Id: ProjectResolverSource.java,v 1.1 2002/04/16 18:33:27 jvanzyl Exp $ */ public class ProjectResolverSource { /** * 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; private SourceTool st; private ImportStatementVisitor isv; /** * Default constructor. */ public ProjectResolverSource() { 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 String 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 baseDirectory 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 map 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. */ 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]; System.out.println(file); 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. */ public static void main(String[] args) throws Exception { ProjectResolverSource bdr = new ProjectResolverSource(); 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()); } } }
