Author: fgiust
Date: Fri Dec  8 03:24:08 2006
New Revision: 483937

URL: http://svn.apache.org/viewvc?view=rev&rev=483937
Log:
MPH-14 Add a mojo to print the dependency tree
Submitted by Mark Hobson

Added:
    
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DependenciesMojo.java
   (with props)
Modified:
    maven/plugins/trunk/maven-help-plugin/pom.xml
    maven/plugins/trunk/maven-help-plugin/src/site/apt/index.apt
    maven/plugins/trunk/maven-help-plugin/src/site/apt/usage.apt

Modified: maven/plugins/trunk/maven-help-plugin/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/pom.xml?view=diff&rev=483937&r1=483936&r2=483937
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-help-plugin/pom.xml Fri Dec  8 03:24:08 2006
@@ -49,6 +49,11 @@
       <artifactId>maven-plugin-descriptor</artifactId>
       <version>2.0</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-dependency-tree</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
   </dependencies>
   <reporting>
     <plugins>

Added: 
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DependenciesMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DependenciesMojo.java?view=auto&rev=483937
==============================================================================
--- 
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DependenciesMojo.java
 (added)
+++ 
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DependenciesMojo.java
 Fri Dec  8 03:24:08 2006
@@ -0,0 +1,258 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+package org.apache.maven.plugins.help;
+
+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:[EMAIL PROTECTED]">Mark Hobson</a>
+ * @version $Id$
+ * @goal dependencies
+ * @requiresDependencyResolution test
+ */
+public class DependenciesMojo 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
+     */
+    private ArtifactFactory artifactFactory;
+
+    /**
+     * The artifact metadata source to use.
+     * 
+     * @component
+     */
+    private ArtifactMetadataSource artifactMetadataSource;
+
+    /**
+     * The artifact collector to use.
+     * 
+     * @component
+     */
+    private ArtifactCollector artifactCollector;
+
+    /**
+     * The dependency tree builder to use.
+     * 
+     * @component
+     */
+    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();
+    }
+}

Propchange: 
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DependenciesMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DependenciesMojo.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: 
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DependenciesMojo.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: maven/plugins/trunk/maven-help-plugin/src/site/apt/index.apt
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/site/apt/index.apt?view=diff&rev=483937&r1=483936&r2=483937
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/site/apt/index.apt (original)
+++ maven/plugins/trunk/maven-help-plugin/src/site/apt/index.apt Fri Dec  8 
03:24:08 2006
@@ -1,42 +1,44 @@
- ------
- Introduction
- ------
- Maria Odea Ching
- ------
- 7 July 2006
- ------
-
-
-Maven 2 Help Plugin
-
- The Maven 2 Help Plugin is used to get relative information about a project. 
It can be used to get a description
- of a particular plugin, including the plugin's mojos with their parameters 
and component requirements, the effective pom
- and effective settings of the current build, and the profiles applied to the 
current project being built.
-
-* Goals Overview
-
-  The Help plugin has 4 goals:
-
-  * {{{active-profiles-mojo.html}help:active-profiles}} lists the profiles 
which are currently active for the build.
-
-  * {{{describe-mojo.html}help:describe}} describes the attirbutes of a plugin 
and/or plugin mojo. For its execution, it requires
-    the groupId and artifactId or the plugin prefix of the plugin to be 
specified.
-
-  * {{{effective-pom-mojo.html}help:effective-pom}} displays the effective POM 
for the current build, with the active
-    profiles factored in.
-
-  * {{{effective-settings-mojo.html}help:effective-settings}} prints out the 
calculated settings for the project, given any
-    profile enhancement and the inheritance of the global settings into the 
user-level settings.
-
-* Usage
-
-   Instructions on how to use the Help Plugin can be found 
{{{usage.html}here}}.
-
-* Examples
-
-   To provide you with better understanding on some usages of the Help plugin, 
you can take a look into the
-   following example(s):
-
-   * {{{examples/describe-configuration.html}Configuring Describe Mojo}}
-
-
+ ------
+ Introduction
+ ------
+ Maria Odea Ching
+ ------
+ 7 July 2006
+ ------
+
+
+Maven 2 Help Plugin
+
+ The Maven 2 Help Plugin is used to get relative information about a project. 
It can be used to get a description
+ of a particular plugin, including the plugin's mojos with their parameters 
and component requirements, the effective pom
+ and effective settings of the current build, and the profiles applied to the 
current project being built.
+
+* Goals Overview
+
+  The Help plugin has the following goals:
+
+  * {{{active-profiles-mojo.html}help:active-profiles}} lists the profiles 
which are currently active for the build.
+
+  * {{{dependencies-mojo.html}help:dependencies}} prints out the dependency 
tree for the project.
+
+  * {{{describe-mojo.html}help:describe}} describes the attirbutes of a plugin 
and/or plugin mojo. For its execution, it requires
+    the groupId and artifactId or the plugin prefix of the plugin to be 
specified.
+
+  * {{{effective-pom-mojo.html}help:effective-pom}} displays the effective POM 
for the current build, with the active
+    profiles factored in.
+
+  * {{{effective-settings-mojo.html}help:effective-settings}} prints out the 
calculated settings for the project, given any
+    profile enhancement and the inheritance of the global settings into the 
user-level settings.
+
+* Usage
+
+   Instructions on how to use the Help Plugin can be found 
{{{usage.html}here}}.
+
+* Examples
+
+   To provide you with better understanding on some usages of the Help plugin, 
you can take a look into the
+   following example(s):
+
+   * {{{examples/describe-configuration.html}Configuring Describe Mojo}}
+
+

Modified: maven/plugins/trunk/maven-help-plugin/src/site/apt/usage.apt
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/site/apt/usage.apt?view=diff&rev=483937&r1=483936&r2=483937
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/site/apt/usage.apt (original)
+++ maven/plugins/trunk/maven-help-plugin/src/site/apt/usage.apt Fri Dec  8 
03:24:08 2006
@@ -1,78 +1,91 @@
- ------
- Usage
- ------
- John Casey
- Maria Odea Ching
- ------
- 10 July 2006
- ------
-
-Usage
-
- Below are the different goals and configuration of the Help plugin.
-
-* The <<<help:active-profiles>>> Mojo
-
-  The <<<active-profiles>>> mojo is used to discover which profiles have been 
applied to the projects currently being built.
-  For each project in the build session, it will output a list of profiles 
which have been applied to that project, along
-  with the source of the profile (POM, settings.xml, or profiles.xml).
-
-  Optionally, the output parameter can be specified to divert this output to a 
file.
-
-  You can execute this mojo using the following command:
-
-+-----+
-mvn help:active-profiles -Doutput=/path/to/file
-+-----+
-
-* The <<<help:describe>>> Mojo
-
-  The <<<describe>>> mojo is used to discover information about Maven plugins. 
Given a plugin prefix or groupId, artifactId,
-  and optionally version, the mojo will lookup that plugin and output details 
about it. If the user also specifies
-  which mojo to describe, the <<describe>> mojo will limit output to the 
details of that mojo, including parameters.
-
-  Optionally, the output parameter can be specified to divert this output to a 
file.
-
-  This mojo requires either the groupId and artifactId parameters or the 
plugin parameter to be specified:
-
-+-----+
-mvn help:describe -DgroupId=org.somewhere
-    -DartifactId=some-plugin -Dversion=0.0.0
-+-----+
-
- or
-
-+-----+
-mvn help:active-profiles -Dplugin=org.somewhere:some-plugin:0.0.0
-+-----+
-
- (NOTE: version is always optional here.)
-
-* The <<<help:effective-pom>>> Mojo
-
-  The <<<effective-pom>>> mojo is used to make visible the POM that results 
from the application of interpolation, inheritance,
-  and active profiles. It provides a useful way of removing the guesswork 
about just what ends up in the POM that Maven uses
-  to build your project. It will iterate over all projects in the current 
build session, printing the effective POM for each.
-
-  Optionally, the output parameter can be specified to divert this output to a 
file.
-
-  The mojo can be executedd using the following command:
-
-+-----+
-mvn help:effective-profiles -Doutput=/path/to/file
-+-----+
-
-* The <<<help:effective-settings>>> Mojo
-
-  The <<<effective-settings>>> mojo is used to view the Settings that Maven 
actually uses to run the build. This Settings
-  instance is a result of merging the global file with the user's file, with 
the user's file taking precedence.
-
-  Optionally, the output parameter can be specified to divert this output to a 
file.
-
-  The mojo can be executedd using the following command:
-
-+-----+
-mvn help:effective-settings -Doutput=/path/to/file
-+-----+
-
-
+ ------
+ Usage
+ ------
+ John Casey
+ Maria Odea Ching
+ ------
+ 10 July 2006
+ ------
+
+Usage
+
+ Below are the different goals and configuration of the Help plugin.
+
+* The <<<help:active-profiles>>> Mojo
+
+  The <<<active-profiles>>> mojo is used to discover which profiles have been 
applied to the projects currently being built.
+  For each project in the build session, it will output a list of profiles 
which have been applied to that project, along
+  with the source of the profile (POM, settings.xml, or profiles.xml).
+
+  Optionally, the output parameter can be specified to divert this output to a 
file.
+
+  You can execute this mojo using the following command:
+
++-----+
+mvn help:active-profiles -Doutput=/path/to/file
++-----+
+
+* The <<<help:dependencies>>> Mojo
+
+  The <<<dependencies>>> 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.
+
+  Optionally, the output parameter can be specified to divert this output to a 
file.
+
+  You can execute this mojo using the following command:
+
++-----+
+mvn help:dependencies -Doutput=/path/to/file
++-----+
+
+* The <<<help:describe>>> Mojo
+
+  The <<<describe>>> mojo is used to discover information about Maven plugins. 
Given a plugin prefix or groupId, artifactId,
+  and optionally version, the mojo will lookup that plugin and output details 
about it. If the user also specifies
+  which mojo to describe, the <<describe>> mojo will limit output to the 
details of that mojo, including parameters.
+
+  Optionally, the output parameter can be specified to divert this output to a 
file.
+
+  This mojo requires either the groupId and artifactId parameters or the 
plugin parameter to be specified:
+
++-----+
+mvn help:describe -DgroupId=org.somewhere
+    -DartifactId=some-plugin -Dversion=0.0.0
++-----+
+
+ or
+
++-----+
+mvn help:active-profiles -Dplugin=org.somewhere:some-plugin:0.0.0
++-----+
+
+ (NOTE: version is always optional here.)
+
+* The <<<help:effective-pom>>> Mojo
+
+  The <<<effective-pom>>> mojo is used to make visible the POM that results 
from the application of interpolation, inheritance,
+  and active profiles. It provides a useful way of removing the guesswork 
about just what ends up in the POM that Maven uses
+  to build your project. It will iterate over all projects in the current 
build session, printing the effective POM for each.
+
+  Optionally, the output parameter can be specified to divert this output to a 
file.
+
+  The mojo can be executedd using the following command:
+
++-----+
+mvn help:effective-profiles -Doutput=/path/to/file
++-----+
+
+* The <<<help:effective-settings>>> Mojo
+
+  The <<<effective-settings>>> mojo is used to view the Settings that Maven 
actually uses to run the build. This Settings
+  instance is a result of merging the global file with the user's file, with 
the user's file taking precedence.
+
+  Optionally, the output parameter can be specified to divert this output to a 
file.
+
+  The mojo can be executedd using the following command:
+
++-----+
+mvn help:effective-settings -Doutput=/path/to/file
++-----+
+
+


Reply via email to