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 2007/06/07 05:40:34 UTC

svn commit: r545048 - in /maven/plugins/trunk/maven-dependency-plugin: pom.xml src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java src/site/apt/index.apt src/site/apt/usage.apt

Author: brianf
Date: Wed Jun  6 20:40:34 2007
New Revision: 545048

URL: http://svn.apache.org/viewvc?view=rev&rev=545048
Log:
MDEP-94: adding tree mojo from help plugin

Added:
    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
    maven/plugins/trunk/maven-dependency-plugin/src/site/apt/index.apt
    maven/plugins/trunk/maven-dependency-plugin/src/site/apt/usage.apt

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=545048&r1=545047&r2=545048
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-dependency-plugin/pom.xml Wed Jun  6 20:40:34 2007
@@ -171,5 +171,10 @@
 		    <artifactId>maven-dependency-analyzer</artifactId>
 		    <version>1.0-alpha-3-SNAPSHOT</version>
 		</dependency>
+		<dependency>
+			<groupId>org.apache.maven.shared</groupId>
+			<artifactId>maven-dependency-tree</artifactId>
+			<version>1.0</version>
+		</dependency>
 	</dependencies>
 </project>

Added: 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=auto&rev=545048
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java (added)
+++ maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java Wed Jun  6 20:40:34 2007
@@ -0,0 +1,271 @@
+package org.apache.maven.plugin.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Iterator;
+
+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.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.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;
+
+/**
+ * Displays the dependency tree for this project.
+ * 
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id$
+ * @since 2.0-alpha-5
+ * @goal tree
+ * @requiresDependencyResolution test
+ */
+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 -----------------------------------------------------------------
+
+    /**
+     * The Maven project.
+     * 
+     * @parameter expression="${project}"
+     * @required
+     * @readonly
+     */
+    private MavenProject project;
+
+    /**
+     * The artifact respository to use.
+     * 
+     * @parameter expression="${localRepository}"
+     * @required
+     * @readonly
+     */
+    private ArtifactRepository localRepository;
+
+    /**
+     * The artifact factory to use.
+     * 
+     * @component
+     * @required
+     * @readonly
+     */
+    private ArtifactFactory artifactFactory;
+
+    /**
+     * The artifact metadata source to use.
+     * 
+     * @component
+     * @required
+     * @readonly
+     */
+    private ArtifactMetadataSource artifactMetadataSource;
+
+    /**
+     * The artifact collector to use.
+     * 
+     * @component
+     * @required
+     * @readonly
+     */
+    private ArtifactCollector artifactCollector;
+
+    /**
+     * The dependency tree builder to use.
+     * 
+     * @component
+     * @required
+     * @readonly
+     */
+    private DependencyTreeBuilder dependencyTreeBuilder;
+
+    /**
+     * If specified, this parameter will cause the dependency tree to be written to the path specified, instead of
+     * writing to the console.
+     * 
+     * @parameter expression="${output}"
+     */
+    private File output;
+
+    // Mojo methods -----------------------------------------------------------
+
+    /*
+     * @see org.apache.maven.plugin.Mojo#execute()
+     */
+    public void execute() throws MojoExecutionException, MojoFailureException
+    {
+        try
+        {
+            DependencyTree dependencyTree =
+                dependencyTreeBuilder.buildDependencyTree( project, localRepository, artifactFactory,
+                                                           artifactMetadataSource, artifactCollector );
+
+            String dependencyTreeString = serialiseDependencyTree( dependencyTree );
+
+            if ( output != null )
+            {
+                write( dependencyTreeString, output );
+
+                getLog().info( "Wrote dependency tree to: " + output );
+            }
+            else
+            {
+                log( dependencyTreeString );
+            }
+        }
+        catch ( DependencyTreeBuilderException exception )
+        {
+            throw new MojoExecutionException( "Cannot build project dependency tree", exception );
+        }
+        catch ( IOException exception )
+        {
+            throw new MojoExecutionException( "Cannot serialise project dependency tree", exception );
+        }
+    }
+
+    // private methods --------------------------------------------------------
+
+    /**
+     * Serialises the specified dependency tree to a string.
+     * 
+     * @param tree
+     *            the dependency tree to serialise
+     * @return the serialised dependency tree
+     */
+    private String serialiseDependencyTree( DependencyTree tree )
+    {
+        StringBuffer buffer = new StringBuffer();
+
+        serialiseDependencyNode( tree.getRootNode(), buffer );
+
+        return buffer.toString();
+    }
+
+    /**
+     * Serialises the specified dependency node and it's children to the specified string buffer.
+     * 
+     * @param node
+     *            the dependency node to log
+     * @param buffer
+     *            the string buffer to serialise to
+     */
+    private void serialiseDependencyNode( DependencyNode node, StringBuffer buffer )
+    {
+        // serialise node
+
+        for ( int i = 0; i < node.getDepth(); i++ )
+        {
+            buffer.append( INDENT );
+        }
+
+        buffer.append( node.getArtifact() ).append( NEWLINE );
+
+        // serialise children
+
+        for ( Iterator iterator = node.getChildren().iterator(); iterator.hasNext(); )
+        {
+            DependencyNode child = (DependencyNode) iterator.next();
+
+            serialiseDependencyNode( child, buffer );
+        }
+    }
+
+    /**
+     * Writes the specified string to the specified file.
+     * 
+     * @param string
+     *            the string to write
+     * @param file
+     *            the file to write to
+     * @throws IOException
+     *             if an I/O error occurs
+     */
+    private void write( String string, File file ) throws IOException
+    {
+        output.getParentFile().mkdirs();
+
+        FileWriter writer = null;
+
+        try
+        {
+            writer = new FileWriter( output );
+
+            writer.write( string );
+        }
+        finally
+        {
+            if ( writer != null )
+            {
+                try
+                {
+                    writer.close();
+                }
+                catch ( IOException exception )
+                {
+                    getLog().error( "Cannot close file", exception );
+                }
+            }
+        }
+    }
+
+    /**
+     * Writes the specified string to the log at info level.
+     * 
+     * @param string
+     *            the string to write
+     * @throws IOException
+     *             if an I/O error occurs
+     */
+    private void log( String string ) throws IOException
+    {
+        BufferedReader reader = new BufferedReader( new StringReader( string ) );
+
+        String line;
+
+        while ( ( line = reader.readLine() ) != null )
+        {
+            getLog().info( line );
+        }
+        
+        reader.close();
+    }
+}

Modified: maven/plugins/trunk/maven-dependency-plugin/src/site/apt/index.apt
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/site/apt/index.apt?view=diff&rev=545048&r1=545047&r2=545048
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/site/apt/index.apt (original)
+++ maven/plugins/trunk/maven-dependency-plugin/src/site/apt/index.apt Wed Jun  6 20:40:34 2007
@@ -70,6 +70,9 @@
   *{{{analyze-mojo.html}dependency:analyze}} analyzes your project's dependencies and lists dependencies that should be declared, but are not, and dependencies that are declared but unused.
   
   *{{{analyze-dep-mgt-mojo.html}dependency:analyze-dep-mgt}} analyzes your projects dependencies and lists mismatches between resolved dependencies and those listed in your dependencyManagement section.
+
+  *{{{tree-mojo.html}dependency:tree}} prints out the dependency tree for the project.
+  
   []
 
 * Usage
@@ -101,5 +104,6 @@
   * {{{http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html}Dependency Mechanism}}
 
   []
+
 
 

Modified: maven/plugins/trunk/maven-dependency-plugin/src/site/apt/usage.apt
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/site/apt/usage.apt?view=diff&rev=545048&r1=545047&r2=545048
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/site/apt/usage.apt (original)
+++ maven/plugins/trunk/maven-dependency-plugin/src/site/apt/usage.apt Wed Jun  6 20:40:34 2007
@@ -602,3 +602,19 @@
 [WARNING] Potential problems found in Dependency Management
 +---+
  
+* The <<<dependency:tree>>> Mojo
+
+	This mojo is used to view the dependency hierarchy of the project currently being built.
+	It will output the resolved tree of dependencies that the Maven build process actually uses.
+
+	This mojo can be executed from the command line:
+
++-----+
+mvn dependency:tree
++-----+
+
+	Optionally, the <<<output>>> parameter can be specified to divert the output to a file:
+
++-----+
+mvn dependency:tree -Doutput=/path/to/file
++-----+