You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2006/01/11 08:57:37 UTC

svn commit: r367962 - in /maven/components/trunk/maven-core/src/main: java/org/apache/maven/lifecycle/ java/org/apache/maven/lifecycle/mapping/ resources/META-INF/plexus/

Author: jdcasey
Date: Tue Jan 10 23:57:32 2006
New Revision: 367962

URL: http://svn.apache.org/viewcvs?rev=367962&view=rev
Log:
[MNG-1903] Adding support for optional mojos within a lifecycle mapping.

Modified:
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/DefaultLifecycleMapping.java
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/Lifecycle.java
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecycleMapping.java
    maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml

Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java?rev=367962&r1=367961&r2=367962&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java (original)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java Tue Jan 10 23:57:32 2006
@@ -95,7 +95,7 @@
     private List defaultReports;
 
     private Map phaseToLifecycleMap;
-
+    
     // ----------------------------------------------------------------------
     //
     // ----------------------------------------------------------------------
@@ -375,7 +375,7 @@
                     try
                     {
                         // definitely a CLI goal, can use prefix
-                        mojo = getMojoDescriptor( task, session, project, task, true );
+                        mojo = getMojoDescriptor( task, session, project, task, true, false );
                     }
                     catch ( PluginNotFoundException e )
                     {
@@ -481,7 +481,7 @@
         throws LifecycleExecutionException, BuildFailureException, PluginNotFoundException
     {
         // guaranteed to come from the CLI and not be part of a phase
-        MojoDescriptor mojoDescriptor = getMojoDescriptor( task, session, project, task, true );
+        MojoDescriptor mojoDescriptor = getMojoDescriptor( task, session, project, task, true, false );
         executeGoals( Collections.singletonList( new MojoExecution( mojoDescriptor ) ), forkEntryPoints, session,
                       project );
     }
@@ -958,7 +958,9 @@
         throws LifecycleExecutionException, BuildFailureException, PluginNotFoundException
     {
         Map mappings = findMappingsForLifecycle( session, project, lifecycle );
-
+        
+        List optionalMojos = findOptionalMojosForLifecycle( session, project, lifecycle );
+        
         Map lifecycleMappings = new HashMap();
 
         for ( Iterator i = lifecycle.getPhases().iterator(); i.hasNext(); )
@@ -974,7 +976,12 @@
                     String goal = tok.nextToken().trim();
 
                     // Not from the CLI, don't use prefix
-                    MojoDescriptor mojoDescriptor = getMojoDescriptor( goal, session, project, selectedPhase, false );
+                    MojoDescriptor mojoDescriptor = getMojoDescriptor( goal, session, project, selectedPhase, false, optionalMojos.contains( goal ) );
+                    
+                    if ( mojoDescriptor == null )
+                    {
+                        continue;
+                    }
 
                     if ( mojoDescriptor.isDirectInvocationOnly() )
                     {
@@ -1045,6 +1052,41 @@
         return mappings;
     }
 
+    private List findOptionalMojosForLifecycle( MavenSession session, MavenProject project, Lifecycle lifecycle )
+        throws LifecycleExecutionException, PluginNotFoundException
+    {
+        String packaging = project.getPackaging();
+        List optionalMojos = null;
+
+        LifecycleMapping m = (LifecycleMapping) findExtension( project, LifecycleMapping.ROLE, packaging, session
+            .getSettings(), session.getLocalRepository() );
+        
+        if ( m != null )
+        {
+            optionalMojos = m.getOptionalMojos( lifecycle.getId() );
+        }
+
+        if ( optionalMojos == null )
+        {
+            try
+            {
+                m = (LifecycleMapping) session.lookup( LifecycleMapping.ROLE, packaging );
+                optionalMojos = m.getOptionalMojos( lifecycle.getId() );
+            }
+            catch ( ComponentLookupException e )
+            {
+                getLogger().debug( "Error looking up lifecycle mapping to retrieve optional mojos. Lifecycle ID: " + lifecycle.getId() + ". Error: " + e.getMessage(), e );
+            }
+        }
+        
+        if ( optionalMojos == null )
+        {
+            optionalMojos = Collections.EMPTY_LIST;
+        }
+
+        return optionalMojos;
+    }
+
     private Object findExtension( MavenProject project, String role, String roleHint, Settings settings,
                                   ArtifactRepository localRepository )
         throws LifecycleExecutionException, PluginNotFoundException
@@ -1334,7 +1376,7 @@
     }
 
     private MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project,
-                                              String invokedVia, boolean canUsePrefix )
+                                              String invokedVia, boolean canUsePrefix, boolean isOptionalMojo )
         throws BuildFailureException, LifecycleExecutionException, PluginNotFoundException
     {
         String goal;
@@ -1342,114 +1384,138 @@
 
         PluginDescriptor pluginDescriptor = null;
 
-        StringTokenizer tok = new StringTokenizer( task, ":" );
-        int numTokens = tok.countTokens();
-
-        if ( numTokens == 2 )
+        try
         {
-            if ( !canUsePrefix )
+            StringTokenizer tok = new StringTokenizer( task, ":" );
+            int numTokens = tok.countTokens();
+
+            if ( numTokens == 2 )
             {
-                String msg = "Mapped-prefix lookup of mojos are only supported from direct invocation. " +
-                    "Please use specification of the form groupId:artifactId[:version]:goal instead. " +
-                    "(Offending mojo: \'" + task + "\', invoked via: \'" + invokedVia + "\')";
-                throw new LifecycleExecutionException( msg );
-            }
+                if ( !canUsePrefix )
+                {
+                    String msg = "Mapped-prefix lookup of mojos are only supported from direct invocation. "
+                        + "Please use specification of the form groupId:artifactId[:version]:goal instead. "
+                        + "(Offending mojo: \'" + task + "\', invoked via: \'" + invokedVia + "\')";
+                    throw new LifecycleExecutionException( msg );
+                }
 
-            String prefix = tok.nextToken();
-            goal = tok.nextToken();
+                String prefix = tok.nextToken();
+                goal = tok.nextToken();
 
-            // Steps for retrieving the plugin model instance:
-            // 1. request directly from the plugin collector by prefix
-            pluginDescriptor = pluginManager.getPluginDescriptorForPrefix( prefix );
+                // Steps for retrieving the plugin model instance:
+                // 1. request directly from the plugin collector by prefix
+                pluginDescriptor = pluginManager.getPluginDescriptorForPrefix( prefix );
 
-            // 2. look in the repository via search groups
-            if ( pluginDescriptor == null )
-            {
-                plugin = pluginManager.getPluginDefinitionForPrefix( prefix, session, project );
-            }
-            else
-            {
-                plugin = new Plugin();
+                // 2. look in the repository via search groups
+                if ( pluginDescriptor == null )
+                {
+                    plugin = pluginManager.getPluginDefinitionForPrefix( prefix, session, project );
+                }
+                else
+                {
+                    plugin = new Plugin();
 
-                plugin.setGroupId( pluginDescriptor.getGroupId() );
-                plugin.setArtifactId( pluginDescriptor.getArtifactId() );
-                plugin.setVersion( pluginDescriptor.getVersion() );
-            }
+                    plugin.setGroupId( pluginDescriptor.getGroupId() );
+                    plugin.setArtifactId( pluginDescriptor.getArtifactId() );
+                    plugin.setVersion( pluginDescriptor.getVersion() );
+                }
+
+                // 3. search plugins in the current POM
+                if ( plugin == null )
+                {
+                    for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
+                    {
+                        Plugin buildPlugin = (Plugin) i.next();
+
+                        PluginDescriptor desc = verifyPlugin( buildPlugin, project, session.getSettings(), session
+                            .getLocalRepository() );
+                        if ( prefix.equals( desc.getGoalPrefix() ) )
+                        {
+                            plugin = buildPlugin;
+                        }
+                    }
+                }
+
+                // 4. default to o.a.m.plugins and maven-<prefix>-plugin
+                if ( plugin == null )
+                {
+                    plugin = new Plugin();
+                    plugin.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
+                    plugin.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( prefix ) );
+                }
 
-            // 3. search plugins in the current POM
-            if ( plugin == null )
-            {
                 for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
                 {
                     Plugin buildPlugin = (Plugin) i.next();
 
-                    PluginDescriptor desc =
-                        verifyPlugin( buildPlugin, project, session.getSettings(), session.getLocalRepository() );
-                    if ( prefix.equals( desc.getGoalPrefix() ) )
+                    if ( buildPlugin.getKey().equals( plugin.getKey() ) )
                     {
                         plugin = buildPlugin;
+                        break;
                     }
                 }
             }
-
-            // 4. default to o.a.m.plugins and maven-<prefix>-plugin
-            if ( plugin == null )
+            else if ( numTokens == 3 || numTokens == 4 )
             {
                 plugin = new Plugin();
-                plugin.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
-                plugin.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( prefix ) );
-            }
 
-            for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
-            {
-                Plugin buildPlugin = (Plugin) i.next();
+                plugin.setGroupId( tok.nextToken() );
+                plugin.setArtifactId( tok.nextToken() );
 
-                if ( buildPlugin.getKey().equals( plugin.getKey() ) )
+                if ( numTokens == 4 )
                 {
-                    plugin = buildPlugin;
-                    break;
+                    plugin.setVersion( tok.nextToken() );
                 }
+
+                goal = tok.nextToken();
+            }
+            else
+            {
+                String message = "Invalid task '" + task + "': you must specify a valid lifecycle phase, or"
+                    + " a goal in the format plugin:goal or pluginGroupId:pluginArtifactId:pluginVersion:goal";
+                throw new BuildFailureException( message );
             }
-        }
-        else if ( numTokens == 3 || numTokens == 4 )
-        {
-            plugin = new Plugin();
 
-            plugin.setGroupId( tok.nextToken() );
-            plugin.setArtifactId( tok.nextToken() );
+            project.injectPluginManagementInfo( plugin );
 
-            if ( numTokens == 4 )
+            if ( pluginDescriptor == null )
             {
-                plugin.setVersion( tok.nextToken() );
+                pluginDescriptor = verifyPlugin( plugin, project, session.getSettings(), session.getLocalRepository() );
             }
 
-            goal = tok.nextToken();
-        }
-        else
-        {
-            String message = "Invalid task '" + task + "': you must specify a valid lifecycle phase, or" +
-                " a goal in the format plugin:goal or pluginGroupId:pluginArtifactId:pluginVersion:goal";
-            throw new BuildFailureException( message );
-        }
+            // this has been simplified from the old code that injected the plugin management stuff, since
+            // pluginManagement injection is now handled by the project method.
+            project.addPlugin( plugin );
 
-        project.injectPluginManagementInfo( plugin );
+            MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
+            if ( mojoDescriptor == null )
+            {
+                if ( isOptionalMojo )
+                {
+                    getLogger().info( "Skipping missing optional mojo: " + task );
+                }
+                else
+                {
+                    throw new BuildFailureException( "Required goal not found: " + task );
+                }
+            }
 
-        if ( pluginDescriptor == null )
-        {
-            pluginDescriptor = verifyPlugin( plugin, project, session.getSettings(), session.getLocalRepository() );
+            return mojoDescriptor;
         }
-
-        // this has been simplified from the old code that injected the plugin management stuff, since
-        // pluginManagement injection is now handled by the project method.
-        project.addPlugin( plugin );
-
-        MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
-        if ( mojoDescriptor == null )
+        catch ( PluginNotFoundException e )
         {
-            throw new BuildFailureException( "Required goal not found: " + task );
+            if ( isOptionalMojo )
+            {
+                getLogger().info( "Skipping missing optional mojo: " + task );
+                getLogger().debug( "Mojo: " + task + " could not be found. Reason: " + e.getMessage(), e );
+            }
+            else
+            {
+                throw e;
+            }
         }
-
-        return mojoDescriptor;
+        
+        return null;
     }
 
     protected void line()

Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/DefaultLifecycleMapping.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/DefaultLifecycleMapping.java?rev=367962&r1=367961&r2=367962&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/DefaultLifecycleMapping.java (original)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/DefaultLifecycleMapping.java Tue Jan 10 23:57:32 2006
@@ -36,6 +36,33 @@
 
     /** @deprecated use lifecycles instead */
     private Map phases;
+    
+    public List getOptionalMojos( String lifecycle )
+    {
+        if ( lifecycleMap == null )
+        {
+            lifecycleMap = new HashMap();
+
+            if ( lifecycles != null )
+            {
+                for ( Iterator i = lifecycles.iterator(); i.hasNext(); )
+                {
+                    Lifecycle l = (Lifecycle) i.next();
+                    lifecycleMap.put( l.getId(), l );
+                }
+            }
+        }
+        Lifecycle l = (Lifecycle) lifecycleMap.get( lifecycle );
+
+        if ( l != null )
+        {
+            return l.getOptionalMojos();
+        }
+        else
+        {
+            return null;
+        }
+    }
 
     public Map getPhases( String lifecycle )
     {

Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/Lifecycle.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/Lifecycle.java?rev=367962&r1=367961&r2=367962&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/Lifecycle.java (original)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/Lifecycle.java Tue Jan 10 23:57:32 2006
@@ -16,6 +16,8 @@
  * limitations under the License.
  */
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -32,6 +34,8 @@
      * Field phases
      */
     private Map phases;
+    
+    private List optionalMojos = new ArrayList();
 
     /**
      * Method getId
@@ -58,4 +62,19 @@
     {
         this.id = id;
     } //-- void setId(String) 
+    
+    public void addOptionalMojo( String optionalMojo )
+    {
+        this.optionalMojos.add( optionalMojo );
+    }
+    
+    public void setOptionalMojos( List optionalMojos )
+    {
+        this.optionalMojos = optionalMojos;
+    }
+    
+    public List getOptionalMojos()
+    {
+        return this.optionalMojos;
+    }
 }

Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecycleMapping.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecycleMapping.java?rev=367962&r1=367961&r2=367962&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecycleMapping.java (original)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecycleMapping.java Tue Jan 10 23:57:32 2006
@@ -16,6 +16,7 @@
  * limitations under the License.
  */
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -26,5 +27,7 @@
 {
     String ROLE = LifecycleMapping.class.getName();
 
+    List getOptionalMojos( String lifecycle );
+    
     Map getPhases( String lifecycle );
 }

Modified: maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml?rev=367962&r1=367961&r2=367962&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml (original)
+++ maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml Tue Jan 10 23:57:32 2006
@@ -306,6 +306,9 @@
               <install>org.apache.maven.plugins:maven-install-plugin:install</install>
               <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
             </phases>
+            <optional-mojos>
+              <optional-mojo>org.apache.maven.plugins:maven-site-plugin:attach-descriptor</optional-mojo>
+            </optional-mojos>
             <!-- END SNIPPET: pom-lifecycle -->
           </lifecycle>
         </lifecycles>