Author: vsiveton
Date: Fri Aug  1 16:37:20 2008
New Revision: 681903

URL: http://svn.apache.org/viewvc?rev=681903&view=rev
Log:
MPH-30: help:describe should accept "cmd" argument

o added new cmd argument 
o updated documentation

Modified:
    
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DescribeMojo.java
    maven/plugins/trunk/maven-help-plugin/src/site/apt/usage.apt

Modified: 
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DescribeMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DescribeMojo.java?rev=681903&r1=681902&r2=681903&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DescribeMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/DescribeMojo.java
 Fri Aug  1 16:37:20 2008
@@ -22,14 +22,22 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.Writer;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.Iterator;
 import java.util.List;
+import java.util.StringTokenizer;
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 import 
org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 import org.apache.maven.execution.MavenSession;
+import org.apache.maven.lifecycle.DefaultLifecycleExecutor;
+import org.apache.maven.lifecycle.Lifecycle;
+import org.apache.maven.lifecycle.LifecycleExecutionException;
+import org.apache.maven.lifecycle.LifecycleExecutor;
+import org.apache.maven.lifecycle.mapping.LifecycleMapping;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.InvalidPluginException;
@@ -48,6 +56,8 @@
 import org.apache.maven.project.ProjectBuildingException;
 import org.apache.maven.settings.Settings;
 import org.codehaus.plexus.component.repository.ComponentRequirement;
+import 
org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.WriterFactory;
 
 /**
@@ -188,6 +198,15 @@
      */
     private boolean medium;
 
+    /**
+     * A Maven command like a single goal or a single phase following the 
Maven command line:
+     * <code>mvn [options] [<goal(s)>] [<phase(s)>]</code>
+     *
+     * @parameter expression="${cmd}"
+     * @since 2.1
+     */
+    private String cmd;
+
     /** [EMAIL PROTECTED] */
     public void execute()
         throws MojoExecutionException, MojoFailureException
@@ -204,21 +223,30 @@
             }
         }
 
-        PluginInfo pi = new PluginInfo();
-
-        parsePluginLookupInfo( pi );
-
-        PluginDescriptor descriptor = lookupPluginDescriptor( pi );
-
         StringBuffer descriptionBuffer = new StringBuffer();
 
-        if ( mojo != null && mojo.length() > 0 )
+        boolean describePlugin = true;
+        if ( StringUtils.isNotEmpty( cmd ) )
         {
-            describeMojo( descriptor.getMojo( mojo ), descriptionBuffer );
+            describePlugin = describeCommand( descriptionBuffer );
         }
-        else
+
+        if ( describePlugin )
         {
-            describePlugin( descriptor, descriptionBuffer );
+            PluginInfo pi = new PluginInfo();
+
+            parsePluginLookupInfo( pi );
+
+            PluginDescriptor descriptor = lookupPluginDescriptor( pi );
+
+            if ( mojo != null && mojo.length() > 0 )
+            {
+                describeMojo( descriptor.getMojo( mojo ), descriptionBuffer );
+            }
+            else
+            {
+                describePlugin( descriptor, descriptionBuffer );
+            }
         }
 
         writeDescription( descriptionBuffer );
@@ -904,7 +932,178 @@
         this.version = version;
     }
 
-    private static class PluginInfo
+    /**
+     * Describe the <code>cmd</code> parameter
+     *
+     * @param descriptionBuffer not null
+     * @return <code>true</code> if it implies to describe a plugin, 
<code>false</code> otherwise.
+     * @throws MojoFailureException if any
+     */
+    private boolean describeCommand( StringBuffer descriptionBuffer )
+        throws MojoFailureException
+    {
+        if ( cmd.indexOf( ":" ) == -1 )
+        {
+            // phase
+            try
+            {
+                DefaultLifecycleExecutor lifecycleExecutor =
+                    (DefaultLifecycleExecutor) session.lookup( 
LifecycleExecutor.ROLE );
+
+                Lifecycle lifecycle = (Lifecycle) 
lifecycleExecutor.getPhaseToLifecycleMap().get( cmd );
+
+                LifecycleMapping lifecycleMapping =
+                    (LifecycleMapping) session.lookup( LifecycleMapping.ROLE, 
project.getPackaging() );
+                if ( lifecycle.getDefaultPhases() == null )
+                {
+                    descriptionBuffer.append( "'" + cmd + "' is a phase 
corresponding to this plugin:\n" );
+                    for ( Iterator it = lifecycle.getPhases().iterator(); 
it.hasNext(); )
+                    {
+                        String key = (String) it.next();
+
+                        if ( !key.equals( cmd ) )
+                        {
+                            continue;
+                        }
+
+                        if ( lifecycleMapping.getPhases( "default" ).get( key 
) != null )
+                        {
+                            descriptionBuffer.append( 
lifecycleMapping.getPhases( "default" ).get( key ) ).append( "\n" );
+                        }
+                    }
+
+                    descriptionBuffer.append( "\n" );
+                    descriptionBuffer.append(
+                                              "It is a part of the lifecycle 
for the POM packaging '"
+                                                  + project.getPackaging() + 
"'. This lifecycle includes the following phases:" ).append( "\n" );
+                    for ( Iterator it = lifecycle.getPhases().iterator(); 
it.hasNext(); )
+                    {
+                        String key = (String) it.next();
+
+                        descriptionBuffer.append( "* " + key + ": ");
+                        String value = (String) lifecycleMapping.getPhases( 
"default" ).get( key );
+                        if ( value != null )
+                        {
+                            for ( StringTokenizer tok = new StringTokenizer( 
value, "," ); tok.hasMoreTokens(); )
+                            {
+                                descriptionBuffer.append( 
tok.nextToken().trim() );
+
+                                if (!tok.hasMoreTokens())
+                                {
+                                    descriptionBuffer.append( "\n" );
+                                }
+                                else
+                                {
+                                    descriptionBuffer.append( ", " );
+                                }
+                            }
+                        }
+                        else
+                        {
+                            descriptionBuffer.append( "NOT DEFINED" ).append( 
"\n" );
+                        }
+                    }
+                }
+                else
+                {
+                    descriptionBuffer.append( "'" + cmd + "' is a lifecycle 
with the following phases: " ).append( "\n" );
+                    for ( Iterator it = lifecycle.getPhases().iterator(); 
it.hasNext(); )
+                    {
+                        String key = (String) it.next();
+
+                        descriptionBuffer.append( "* " + key + ": ");
+                        if ( lifecycle.getDefaultPhases().get( key ) != null )
+                        {
+                            descriptionBuffer.append( 
lifecycle.getDefaultPhases().get( key ) ).append( "\n" );
+                        }
+                        else
+                        {
+                            descriptionBuffer.append( "NOT DEFINED" ).append( 
"\n" );
+                        }
+                    }
+                }
+            }
+            catch ( ComponentLookupException e )
+            {
+                throw new MojoFailureException( "ComponentLookupException: " + 
e.getMessage() );
+            }
+            catch ( LifecycleExecutionException e )
+            {
+                throw new MojoFailureException( "LifecycleExecutionException: 
" + e.getMessage() );
+            }
+
+            return false;
+        }
+
+        // goals
+        MojoDescriptor mojoDescriptor = getMojoDescriptor( cmd, session, 
project, cmd, true, false );
+
+        descriptionBuffer.append(  "'" + cmd + "' is a plugin" ).append( ".\n" 
);
+        plugin = mojoDescriptor.getPluginDescriptor().getId();
+
+        return true;
+    }
+
+    /**
+     * Invoke the following private method
+     * <code>DefaultLifecycleExecutor#getMojoDescriptor(String, MavenSession, 
MavenProject, String, boolean, boolean)</code>
+     *
+     * @param task not null
+     * @param session not null
+     * @param project not null
+     * @param invokedVia not null
+     * @param canUsePrefix not null
+     * @param isOptionalMojo not null
+     * @return MojoDescriptor for the task
+     * @throws MojoFailureException if any
+     * @see DefaultLifecycleExecutor#getMojoDescriptor(String, MavenSession, 
MavenProject, String, boolean, boolean)
+     */
+    private MojoDescriptor getMojoDescriptor( String task, MavenSession 
session, MavenProject project,
+                                              String invokedVia, boolean 
canUsePrefix, boolean isOptionalMojo )
+        throws MojoFailureException
+    {
+        try
+        {
+            DefaultLifecycleExecutor lifecycleExecutor =
+                (DefaultLifecycleExecutor) session.lookup( 
LifecycleExecutor.ROLE );
+
+            Method m =
+                lifecycleExecutor.getClass().getDeclaredMethod(
+                                                                
"getMojoDescriptor",
+                                                                new Class[] { 
String.class, MavenSession.class,
+                                                                    
MavenProject.class, String.class,
+                                                                    
Boolean.TYPE, Boolean.TYPE } );
+            m.setAccessible( true );
+            return (MojoDescriptor) m.invoke( lifecycleExecutor, new Object[] 
{ task, session, project,
+                invokedVia, Boolean.valueOf( canUsePrefix ), Boolean.valueOf( 
isOptionalMojo ) } );
+        }
+        catch ( SecurityException e )
+        {
+            throw new MojoFailureException( "SecurityException: " + 
e.getMessage() );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            throw new MojoFailureException( "IllegalArgumentException: " + 
e.getMessage() );
+        }
+        catch ( ComponentLookupException e )
+        {
+            throw new MojoFailureException( "ComponentLookupException: " + 
e.getMessage() );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            throw new MojoFailureException( "NoSuchMethodException: " + 
e.getMessage() );
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new MojoFailureException( "IllegalAccessException: " + 
e.getMessage() );
+        }
+        catch ( InvocationTargetException e )
+        {
+            throw new MojoFailureException( "InvocationTargetException: " + 
e.getMessage() );
+        }
+    }
+
+    protected static class PluginInfo
     {
         String prefix;
 
@@ -920,5 +1119,4 @@
 
         PluginDescriptor pluginDescriptor;
     }
-
 }

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?rev=681903&r1=681902&r2=681903&view=diff
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/site/apt/usage.apt (original)
+++ maven/plugins/trunk/maven-help-plugin/src/site/apt/usage.apt Fri Aug  1 
16:37:20 2008
@@ -4,7 +4,7 @@
  John Casey
  Maria Odea Ching
  ------
- January 2008
+ 2008-08-01
  ------
 
  ~~ Licensed to the Apache Software Foundation (ASF) under one
@@ -38,7 +38,7 @@
   You can execute this mojo using the following command:
 
 +-----+
-mvn help:active-profiles
+# mvn help:active-profiles
 +-----+
 
 
@@ -51,23 +51,89 @@
   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
+# mvn help:describe -DgroupId=org.somewhere -DartifactId=some-plugin 
-Dversion=0.0.0
 +-----+
 
  or
 
 +-----+
-mvn help:describe -Dplugin=org.somewhere:some-plugin:0.0.0
+# mvn help:describe -Dplugin=org.somewhere:some-plugin:0.0.0
 +-----+
 
  Here is an example with the <<<mojo>>> parameter specified:
 
 +-----+
-mvn help:describe -Dplugin=org.apache.maven.plugins:maven-help-plugin 
-Dmojo=describe
+# mvn help:describe -Dplugin=org.apache.maven.plugins:maven-help-plugin 
-Dmojo=describe
 +-----+
 
  <<Note:>> <<<version>>> is always optional here.
 
+ You could also asking for a single Maven command, i.e. a goal, a phase or a 
lifecycle:
+
++-----+
+# mvn help:describe -Dcmd=clean
+...
+[INFO] [help:describe]
+[INFO] 'clean' is a lifecycle with the following phases:
+* pre-clean: NOT DEFINED
+* clean: org.apache.maven.plugins:maven-clean-plugin:clean
+* post-clean: NOT DEFINED
+...
++-----+
+
+or
+
++-----+
+# mvn help:describe -Dcmd=compile
+...
+[INFO] [help:describe]
+[INFO] 'compile' is a phase corresponding to this plugin:
+org.apache.maven.plugins:maven-compiler-plugin:compile
+
+It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle 
includes the following phases:
+* validate: NOT DEFINED
+* initialize: NOT DEFINED
+* generate-sources: NOT DEFINED
+* process-sources: NOT DEFINED
+* generate-resources: NOT DEFINED
+* process-resources: org.apache.maven.plugins:maven-resources-plugin:resources
+* compile: org.apache.maven.plugins:maven-compiler-plugin:compile
+* process-classes: NOT DEFINED
+* generate-test-sources: NOT DEFINED
+* process-test-sources: NOT DEFINED
+* generate-test-resources: NOT DEFINED
+* process-test-resources: 
org.apache.maven.plugins:maven-resources-plugin:testResources
+* test-compile: org.apache.maven.plugins:maven-compiler-plugin:testCompile
+* process-test-classes: NOT DEFINED
+* test: org.apache.maven.plugins:maven-surefire-plugin:test
+* package: org.apache.maven.plugins:maven-jar-plugin:jar
+* pre-integration-test: NOT DEFINED
+* integration-test: NOT DEFINED
+* post-integration-test: NOT DEFINED
+* verify: NOT DEFINED
+* install: org.apache.maven.plugins:maven-install-plugin:install
+* deploy: org.apache.maven.plugins:maven-deploy-plugin:deploy
+...
++-----+
+
+or
+
++-----+
+# mvn help:describe -Dcmd=compiler:compile
+...
+[INFO] [help:describe]
+[INFO] 'compiler:compile' is a plugin.
+Plugin: 'org.apache.maven.plugins:maven-compiler-plugin:2.0.2'
+-----------------------------------------------
+Group Id:  org.apache.maven.plugins
+Artifact Id: maven-compiler-plugin
+Version:     2.0.2
+Goal Prefix: compiler
+Description:
+
+Maven Plugins
+...
++-----+
 
 * The <<<help:effective-pom>>> Mojo
 
@@ -78,7 +144,7 @@
   The mojo can be executed using the following command:
 
 +-----+
-mvn help:effective-pom
+# mvn help:effective-pom
 +-----+
 
 
@@ -90,7 +156,7 @@
   The mojo can be executed using the following command:
 
 +-----+
-mvn help:effective-settings
+# mvn help:effective-settings
 +-----+
 
 * The <<<help:system>>> Mojo
@@ -100,7 +166,7 @@
   The mojo can be executed using the following command:
 
 +-----+
-mvn help:system
+# mvn help:system
 +-----+
 
 * Redirecting output to a file
@@ -109,5 +175,5 @@
   divert the output to a file. Here is an example of that:
 
 +-----+
-mvn help:active-profiles -Doutput=/path/to/file
+# mvn help:active-profiles -Doutput=/path/to/file
 +-----+


Reply via email to