maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r552517 - in /maven/plugins/trunk/maven-dependency-plugin: pom.xml src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java
Date Mon, 02 Jul 2007 15:51:51 GMT
Author: markh
Date: Mon Jul  2 08:51:50 2007
New Revision: 552517

URL: http://svn.apache.org/viewvc?view=rev&rev=552517
Log:
o Moved maven-dependency-tree to 1.1-SNAPSHOT for node states, visitors and filters
o Updated to use SerializingDependencyNodeVisitor for serialization
o Added various new parameters to dependency:tree to support:
	- scope filtering
	- node state filtering
	- artifact includes/excludes filtering

Modified:
    maven/plugins/trunk/maven-dependency-plugin/pom.xml
    maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java

Modified: maven/plugins/trunk/maven-dependency-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/pom.xml?view=diff&rev=552517&r1=552516&r2=552517
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-dependency-plugin/pom.xml Mon Jul  2 08:51:50 2007
@@ -179,7 +179,12 @@
 		<dependency>
 			<groupId>org.apache.maven.shared</groupId>
 			<artifactId>maven-dependency-tree</artifactId>
-			<version>1.0</version>
+			<version>1.1-SNAPSHOT</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.maven.shared</groupId>
+			<artifactId>maven-common-artifact-filters</artifactId>
+			<version>1.0-alpha-2-SNAPSHOT</version>
 		</dependency>
 	</dependencies>
 </project>

Modified: maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java?view=diff&rev=552517&r1=552516&r2=552517
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java
(original)
+++ maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java
Mon Jul  2 08:51:50 2007
@@ -24,20 +24,37 @@
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.StringReader;
-import java.util.Iterator;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.resolver.ArtifactCollector;
+import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
+import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
+import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
 import org.apache.maven.shared.dependency.tree.DependencyNode;
-import org.apache.maven.shared.dependency.tree.DependencyTree;
 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
+import org.apache.maven.shared.dependency.tree.filter.AncestorOrSelfDependencyNodeFilter;
+import org.apache.maven.shared.dependency.tree.filter.AndDependencyNodeFilter;
+import org.apache.maven.shared.dependency.tree.filter.ArtifactDependencyNodeFilter;
+import org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter;
+import org.apache.maven.shared.dependency.tree.filter.StateDependencyNodeFilter;
+import org.apache.maven.shared.dependency.tree.traversal.BuildingDependencyNodeVisitor;
+import org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor;
+import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
+import org.apache.maven.shared.dependency.tree.traversal.FilteringDependencyNodeVisitor;
+import org.apache.maven.shared.dependency.tree.traversal.SerializingDependencyNodeVisitor;
+import org.apache.maven.shared.dependency.tree.traversal.SerializingDependencyNodeVisitor.TreeTokens;
 
 /**
  * Displays the dependency tree for this project.
@@ -50,18 +67,6 @@
  */
 public class TreeMojo extends AbstractMojo
 {
-    // constants --------------------------------------------------------------
-
-    /**
-     * The indentation string to use when serialising the dependency tree.
-     */
-    private static final String INDENT = "   ";
-
-    /**
-     * The newline string to use when serialising the dependency tree.
-     */
-    private static final String NEWLINE = System.getProperty( "line.separator" );
-
     // fields -----------------------------------------------------------------
 
     /**
@@ -126,6 +131,54 @@
      */
     private File output;
 
+    /**
+     * The computed dependency tree root node of the Maven project.
+     */
+    private DependencyNode rootNode;
+
+    /**
+     * The scope to filter by when resolving the dependency tree, or <code>null</code>
to include dependencies from
+     * all scopes.
+     * 
+     * @parameter expression="${scope}"
+     */
+    private String scope;
+
+    /**
+     * Whether to include omitted nodes in the serialized dependency tree.
+     * 
+     * @parameter expression="${verbose}" default-value="false"
+     */
+    private boolean verbose;
+
+    /**
+     * The token set name to use when outputting the dependency tree. Possible values are
<code>whitespace</code>,
+     * <code>standard</code> or <code>extended</code>, which use
whitespace, standard or extended ASCII sets
+     * respectively.
+     * 
+     * @parameter expression="${tokens}" default-value="standard"
+     */
+    private String tokens;
+
+    /**
+     * A comma-separated list of artifacts to filter the serialized dependency tree by, or
<code>null</code> not to
+     * filter the dependency tree. The artifact syntax is defined by <code>StrictPatternIncludesArtifactFilter</code>.
+     * 
+     * @see StrictPatternIncludesArtifactFilter
+     * @parameter expression="${includes}"
+     */
+    private String includes;
+
+    /**
+     * A comma-separated list of artifacts to filter from the serialized dependency tree,
or <code>null</code> not to
+     * filter any artifacts from the dependency tree. The artifact syntax is defined by
+     * <code>StrictPatternExcludesArtifactFilter</code>.
+     * 
+     * @see StrictPatternExcludesArtifactFilter
+     * @parameter expression="${excludes}"
+     */
+    private String excludes;
+
     // Mojo methods -----------------------------------------------------------
 
     /*
@@ -133,13 +186,16 @@
      */
     public void execute() throws MojoExecutionException, MojoFailureException
     {
+        ArtifactFilter artifactFilter = createResolvingArtifactFilter();
+
         try
         {
-            DependencyTree dependencyTree =
+            // TODO: fix DependencyTreeBuilder to apply filter
+            rootNode =
                 dependencyTreeBuilder.buildDependencyTree( project, localRepository, artifactFactory,
-                                                           artifactMetadataSource, artifactCollector
);
+                                                           artifactMetadataSource, artifactFilter,
artifactCollector );
 
-            String dependencyTreeString = serialiseDependencyTree( dependencyTree );
+            String dependencyTreeString = serialiseDependencyTree( rootNode );
 
             if ( output != null )
             {
@@ -162,51 +218,159 @@
         }
     }
 
+    // public methods ---------------------------------------------------------
+
+    /**
+     * Gets the Maven project used by this mojo.
+     * 
+     * @return the Maven project
+     */
+    public MavenProject getProject()
+    {
+        return project;
+    }
+
+    /**
+     * Gets the computed dependency tree root node for the Maven project.
+     * 
+     * @return the dependency tree root node
+     */
+    public DependencyNode getDependencyTree()
+    {
+        return rootNode;
+    }
+
     // private methods --------------------------------------------------------
 
     /**
+     * Gets the artifact filter to use when resolving the dependency tree.
+     * 
+     * @return the artifact filter
+     */
+    private ArtifactFilter createResolvingArtifactFilter()
+    {
+        ArtifactFilter filter;
+
+        // filter scope
+        if ( scope != null )
+        {
+            getLog().debug( "+ Resolving dependency tree for scope '" + scope + "'" );
+
+            filter = new ScopeArtifactFilter( scope );
+        }
+        else
+        {
+            filter = null;
+        }
+
+        return filter;
+    }
+
+    /**
      * Serialises the specified dependency tree to a string.
      * 
-     * @param tree
-     *            the dependency tree to serialise
+     * @param rootNode
+     *            the dependency tree root node to serialise
      * @return the serialised dependency tree
      */
-    private String serialiseDependencyTree( DependencyTree tree )
+    private String serialiseDependencyTree( DependencyNode rootNode )
     {
-        StringBuffer buffer = new StringBuffer();
+        StringWriter writer = new StringWriter();
+        TreeTokens treeTokens = toTreeTokens( tokens );
+
+        DependencyNodeVisitor visitor = new SerializingDependencyNodeVisitor( writer, treeTokens
);
 
-        serialiseDependencyNode( tree.getRootNode(), buffer );
+        // TODO: remove the need for this when the serializer can calculate last nodes from
visitor calls only
+        visitor = new BuildingDependencyNodeVisitor( visitor );
+
+        DependencyNodeFilter filter = createDependencyNodeFilter();
+
+        if ( filter != null )
+        {
+            CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
+            DependencyNodeVisitor firstPassVisitor = new FilteringDependencyNodeVisitor(
collectingVisitor, filter );
+            rootNode.accept( firstPassVisitor );
 
-        return buffer.toString();
+            DependencyNodeFilter secondPassFilter = new AncestorOrSelfDependencyNodeFilter(
collectingVisitor.getNodes() );
+            visitor = new FilteringDependencyNodeVisitor( visitor, secondPassFilter );
+        }
+
+        rootNode.accept( visitor );
+
+        return writer.toString();
     }
 
     /**
-     * Serialises the specified dependency node and it's children to the specified string
buffer.
+     * Gets the tree tokens instance for the specified name.
      * 
-     * @param node
-     *            the dependency node to log
-     * @param buffer
-     *            the string buffer to serialise to
+     * @param tokens
+     *            the tree tokens name
+     * @return the <code>TreeTokens</code> instance
      */
-    private void serialiseDependencyNode( DependencyNode node, StringBuffer buffer )
+    private TreeTokens toTreeTokens( String tokens )
     {
-        // serialise node
+        TreeTokens treeTokens;
 
-        for ( int i = 0; i < node.getDepth(); i++ )
+        if ( "whitespace".equals( tokens ) )
         {
-            buffer.append( INDENT );
+            getLog().debug( "+ Using whitespace tree tokens" );
+
+            treeTokens = SerializingDependencyNodeVisitor.WHITESPACE_TOKENS;
         }
+        else if ( "extended".equals( tokens ) )
+        {
+            getLog().debug( "+ Using extended tree tokens" );
 
-        buffer.append( node.getArtifact() ).append( NEWLINE );
+            treeTokens = SerializingDependencyNodeVisitor.EXTENDED_TOKENS;
+        }
+        else
+        {
+            treeTokens = SerializingDependencyNodeVisitor.STANDARD_TOKENS;
+        }
 
-        // serialise children
+        return treeTokens;
+    }
 
-        for ( Iterator iterator = node.getChildren().iterator(); iterator.hasNext(); )
+    /**
+     * Gets the dependency node filter to use when serializing the dependency tree.
+     * 
+     * @return the dependency node filter, or <code>null</code> if none required
+     */
+    private DependencyNodeFilter createDependencyNodeFilter()
+    {
+        List filters = new ArrayList();
+
+        // filter node states
+        if ( !verbose )
         {
-            DependencyNode child = (DependencyNode) iterator.next();
+            getLog().debug( "+ Filtering omitted nodes from dependency tree" );
 
-            serialiseDependencyNode( child, buffer );
+            filters.add( StateDependencyNodeFilter.INCLUDED );
         }
+
+        // filter includes
+        if ( includes != null )
+        {
+            List patterns = Arrays.asList( includes.split( "," ) );
+
+            getLog().debug( "+ Filtering dependency tree by artifact include patterns: "
+ patterns );
+
+            ArtifactFilter artifactFilter = new StrictPatternIncludesArtifactFilter( patterns
);
+            filters.add( new ArtifactDependencyNodeFilter( artifactFilter ) );
+        }
+
+        // filter excludes
+        if ( excludes != null )
+        {
+            List patterns = Arrays.asList( excludes.split( "," ) );
+
+            getLog().debug( "+ Filtering dependency tree by artifact exclude patterns: "
+ patterns );
+
+            ArtifactFilter artifactFilter = new StrictPatternExcludesArtifactFilter( patterns
);
+            filters.add( new ArtifactDependencyNodeFilter( artifactFilter ) );
+        }
+
+        return filters.isEmpty() ? null : new AndDependencyNodeFilter( filters );
     }
 
     /**
@@ -265,7 +429,7 @@
         {
             getLog().info( line );
         }
-        
+
         reader.close();
     }
 }



Mime
View raw message