You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by br...@apache.org on 2009/02/16 14:38:53 UTC

svn commit: r744912 - in /maven/plugins/trunk/maven-dependency-plugin: ./ src/main/java/org/apache/maven/plugin/dependency/ src/main/java/org/apache/maven/plugin/dependency/treeSerializers/ src/test/java/org/apache/maven/plugin/dependency/

Author: brianf
Date: Mon Feb 16 13:38:32 2009
New Revision: 744912

URL: http://svn.apache.org/viewvc?rev=744912&view=rev
Log:
MDEP-145 merging branch from pisong@apache.org (includes patch from jerome creignou)

Added:
    maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/
      - copied from r744852, maven/sandbox/branches/MDEP-145/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/
    maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/AbstractSerializingVisitor.java
      - copied unchanged from r744852, maven/sandbox/branches/MDEP-145/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/AbstractSerializingVisitor.java
    maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/DOTDependencyNodeVisitor.java
      - copied unchanged from r744852, maven/sandbox/branches/MDEP-145/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/DOTDependencyNodeVisitor.java
    maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/GraphmlDependencyNodeVisitor.java
      - copied unchanged from r744852, maven/sandbox/branches/MDEP-145/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/GraphmlDependencyNodeVisitor.java
    maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/TGFDependencyNodeVisitor.java
      - copied unchanged from r744852, maven/sandbox/branches/MDEP-145/src/main/java/org/apache/maven/plugin/dependency/treeSerializers/TGFDependencyNodeVisitor.java
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
    maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestTreeMojo.java

Modified: maven/plugins/trunk/maven-dependency-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/pom.xml?rev=744912&r1=744911&r2=744912&view=diff
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-dependency-plugin/pom.xml Mon Feb 16 13:38:32 2009
@@ -25,7 +25,7 @@
   <parent>
     <artifactId>maven-plugins</artifactId>
     <groupId>org.apache.maven.plugins</groupId>
-    <version>12</version>
+    <version>13-SNAPSHOT</version>
   </parent>
   
   <artifactId>maven-dependency-plugin</artifactId>

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?rev=744912&r1=744911&r2=744912&view=diff
==============================================================================
--- 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 Feb 16 13:38:32 2009
@@ -22,6 +22,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.StringWriter;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -42,6 +43,9 @@
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
+import org.apache.maven.plugin.dependency.treeSerializers.GraphmlDependencyNodeVisitor;
+import org.apache.maven.plugin.dependency.treeSerializers.TGFDependencyNodeVisitor;
+import org.apache.maven.plugin.dependency.treeSerializers.DOTDependencyNodeVisitor;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
 import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
@@ -142,6 +146,18 @@
      * @since 2.0-alpha-5
      */
     private File outputFile;
+
+    /**
+     * If specified, this parameter will cause the dependency tree to be written using the specified format. Currently
+     * supported format are text, dot, graphml and tgf.
+     *
+     * These formats can be plotted to image files. An example of how to plot a dot file using
+     * pygraphviz can be found <a href="http://networkx.lanl.gov/pygraphviz/tutorial.html#layout-and-drawing">here</a>
+     *
+     * @parameter expression="${outputType}" default-value="text"
+     * @since 2.1
+     */
+    private String outputType;
     
     /**
      * The scope to filter by when resolving the dependency tree, or <code>null</code> to include dependencies from
@@ -333,7 +349,7 @@
         StringWriter writer = new StringWriter();
         TreeTokens treeTokens = toTreeTokens( tokens );
 
-        DependencyNodeVisitor visitor = new SerializingDependencyNodeVisitor( writer, treeTokens );
+        DependencyNodeVisitor visitor = getSerializingDependencyNodeVisitor( writer );
 
         // TODO: remove the need for this when the serializer can calculate last nodes from visitor calls only
         visitor = new BuildingDependencyNodeVisitor( visitor );
@@ -355,6 +371,26 @@
         return writer.toString();
     }
 
+    public DependencyNodeVisitor getSerializingDependencyNodeVisitor( Writer writer )
+    {
+        if ( "graphml".equals( outputType ) )
+        {
+            return new GraphmlDependencyNodeVisitor( writer );
+        }
+        else if ( "tgf".equals( outputType ) )
+        {
+            return new TGFDependencyNodeVisitor( writer );
+        }
+        else if ( "dot".equals( outputType ) )
+        {
+            return new DOTDependencyNodeVisitor( writer ) ;
+        }
+        else
+        {
+            return new SerializingDependencyNodeVisitor( writer, toTreeTokens( tokens ) );
+        }
+    }
+
     /**
      * Gets the tree tokens instance for the specified name.
      * 

Modified: maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestTreeMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestTreeMojo.java?rev=744912&r1=744911&r2=744912&view=diff
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestTreeMojo.java (original)
+++ maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestTreeMojo.java Mon Feb 16 13:38:32 2009
@@ -20,7 +20,12 @@
  */
 
 import java.io.File;
+import java.io.BufferedReader;
+import java.io.FileReader;
 import java.util.Set;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
@@ -83,6 +88,116 @@
         assertChildNodeEquals( "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile", rootNode, 0 );
         assertChildNodeEquals( "testGroupId:release:jar:1.0:compile", rootNode, 1 );
     }
+
+    /**
+     * Test the DOT format serialization
+     *
+     * @throws Exception
+     */
+    public void testTreeDotSerializing()
+        throws Exception
+    {
+        List contents = runTreeMojo("tree1.dot", "dot") ;
+        assertTrue(findString(contents, "digraph \"testGroupId:project:jar:1.0:compile\" {"));
+        assertTrue(findString(contents, "\"testGroupId:project:jar:1.0:compile\" -> \"testGroupId:snapshot:jar:2.0-SNAPSHOT:compile\""));
+        assertTrue(findString(contents, "\"testGroupId:project:jar:1.0:compile\" -> \"testGroupId:release:jar:1.0:compile\""));
+    }
+
+    /**
+     * Test the GraphML format serialization
+     *
+     * @throws Exception
+     */
+    public void testTreeGraphMLSerializing()
+        throws Exception
+    {
+        List contents = runTreeMojo("tree1.graphml", "graphml") ;
+
+        assertTrue(findString(contents, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
+        assertTrue(findString(contents, "<y:NodeLabel>testGroupId:project:jar:1.0:compile</y:NodeLabel>"));
+        assertTrue(findString(contents, "<y:NodeLabel>testGroupId:snapshot:jar:2.0-SNAPSHOT:compile</y:NodeLabel>"));
+        assertTrue(findString(contents, "<y:NodeLabel>testGroupId:release:jar:1.0:compile</y:NodeLabel>"));
+        assertTrue(findString(contents, "<key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/>"));
+        assertTrue(findString(contents, "<key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/>"));
+    }
+
+    /**
+     * Test the TGF format serialization
+     *
+     * @throws Exception
+     */
+    public void testTreeTGFSerializing()
+        throws Exception
+    {
+        List contents = runTreeMojo("tree1.tgf", "tgf") ;
+        assertTrue(findString(contents, "testGroupId:project:jar:1.0:compile"));
+        assertTrue(findString(contents, "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile"));
+        assertTrue(findString(contents, "testGroupId:release:jar:1.0:compile"));
+    }
+
+    /**
+     * Help finding content in the given list of string
+     * @param outputFile
+     * @param format
+     * @return list of strings in the output file
+     */
+    private List runTreeMojo(String outputFile, String format)
+             throws Exception
+    {
+        File testPom = new File( getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml" );
+        String outputFileName =  testDir.getAbsolutePath() + outputFile  ;
+        TreeMojo mojo = (TreeMojo) lookupMojo( "tree", testPom );
+        setVariableValueToObject( mojo, "localRepository", new StubArtifactRepository( testDir.getAbsolutePath() ) );
+        setVariableValueToObject( mojo, "outputType", format );
+        setVariableValueToObject( mojo, "outputFile", new File( outputFileName) );
+
+        assertNotNull( mojo );
+        assertNotNull( mojo.getProject() );
+        MavenProject project = mojo.getProject();
+        project.setArtifact( this.stubFactory.createArtifact( "testGroupId", "project", "1.0" ) );
+
+        Set artifacts = this.stubFactory.getScopedArtifacts();
+        Set directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
+        artifacts.addAll( directArtifacts );
+
+        project.setArtifacts( artifacts );
+        project.setDependencyArtifacts( directArtifacts );
+
+        mojo.execute();
+
+        BufferedReader fp1 =  new BufferedReader(new FileReader(outputFileName));
+        List contents = new ArrayList() ;
+
+        String line = null;
+        while ((line = fp1.readLine()) != null)
+        {
+            contents.add(line);
+        }
+        fp1.close();
+
+        return contents ;
+    }
+
+    /**
+     * Help finding content in the given list of string
+     * @param contents
+     * @param str
+     */
+    private boolean findString(List contents, String str)
+    {
+        for ( Iterator it = contents.iterator () ; it.hasNext (); )
+        {
+            String line = (String) it.next() ;
+            if ( line.indexOf(str) != -1 )
+            {
+                // if match then return here
+                return true ;
+            }
+        }
+
+        // in case no match for the whole list
+        return false ;
+    }
     
     // private methods --------------------------------------------------------