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/08/14 16:19:43 UTC

svn commit: r565767 - in /maven/plugins/trunk/maven-enforcer-plugin/src: it/ main/java/org/apache/maven/plugin/enforcer/

Author: brianf
Date: Tue Aug 14 07:19:43 2007
New Revision: 565767

URL: http://svn.apache.org/viewvc?view=rev&rev=565767
Log:
new rule for checking plugin versions

Added:
    maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/PluginVersionsDefined.java
Modified:
    maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml
    maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java
    maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java
    maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java
    maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/NoSnapshots.java

Modified: maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml?view=diff&rev=565767&r1=565766&r2=565767
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml Tue Aug 14 07:19:43 2007
@@ -3,7 +3,7 @@
 	<groupId>org.apache.maven.plugins</groupId>
 	<artifactId>maven-enforcer-plugin-it1</artifactId>
 	<version>1</version>
-	<packaging>pom</packaging>
+	<packaging>jar</packaging>
 		  <dependencies>
 	    <dependency>
 		  <groupId>org.apache.maven.shared</groupId>
@@ -16,6 +16,18 @@
 		<plugins>
 			<plugin>
 				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-dependency-plugin</artifactId>
+				<executions>
+					<execution>
+						<id>copy</id>
+						<goals>
+						<goal>copy-dependencies</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
 				<artifactId>maven-enforcer-plugin</artifactId>
 				<dependencies>
 					<dependency>
@@ -29,7 +41,7 @@
 						<id>enforce</id>
 						<configuration>
 							<rules>
-								<requireJavaVersion>
+								<!--<requireJavaVersion>
 									<version>[1.3,1.6]</version>
 								</requireJavaVersion>
 								<requireMavenVersion>
@@ -42,20 +54,23 @@
 								<requireOS>
 									<family>!tandem</family>
 								</requireOS>
-								<!--<evaluateBeanshell>
+								<evaluateBeanshell>
 									<condition>rti.getApplicationVersion().getMajorVersion() == 2;</condition>
-								</evaluateBeanshell>-->
+								</evaluateBeanshell>
 								<noSnapshots/>
 								<bannedDependencies>
 								<excludes>
 								<exclude>org.apache.maven</exclude>
 								</excludes>
-								</bannedDependencies>
+								</bannedDependencies>-->
+								<pluginVersionsDefined>
+								</pluginVersionsDefined>
 							</rules>
 						</configuration>
 						<goals>
-							<goal>enforce-once</goal>
+							<goal>enforce</goal>
 						</goals>
+						<phase>validate</phase>
 
 					</execution>
 				</executions>

Modified: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java?view=diff&rev=565767&r1=565766&r2=565767
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java Tue Aug 14 07:19:43 2007
@@ -19,23 +19,15 @@
  * under the License.
  */
 
-import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
-import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
-import org.apache.maven.artifact.versioning.VersionRange;
-import org.apache.maven.plugin.enforcer.util.EnforcerUtils;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.shared.enforcer.rule.api.EnforcerRule;
 import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
-import org.codehaus.plexus.util.StringUtils;
 
 /**
  * Abstract Rule for banning dependencies

Modified: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java?view=diff&rev=565767&r1=565766&r2=565767
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java Tue Aug 14 07:19:43 2007
@@ -30,11 +30,7 @@
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 import org.apache.maven.artifact.versioning.VersionRange;
 import org.apache.maven.plugin.enforcer.util.EnforcerUtils;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.shared.enforcer.rule.api.EnforcerRule;
 import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
-import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
-import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
 import org.codehaus.plexus.util.StringUtils;
 
 /**

Modified: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java?view=diff&rev=565767&r1=565766&r2=565767
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java Tue Aug 14 07:19:43 2007
@@ -34,11 +34,10 @@
 
 /**
  * This goal executes the defined enforcer-rules once per module.
- * 
+ * @requiresDependencyResolution test
  * @goal enforce
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
  * @phase validate
- * @requiresDependencyResolution test
  * @version $Id$
  */
 public class EnforceMojo
@@ -96,6 +95,11 @@
      * @required
      */
     private EnforcerRule[] rules;
+    
+    /**
+     * True if dependencies are resolved
+     */
+    private boolean dependenciesAreResolved = false;
 
     /**
      * Entry point to the mojo
@@ -135,7 +139,7 @@
                     EnforcerRule rule = rules[i];
                     if ( rule != null )
                     {
-                        // store the current rule for loggin purposes
+                        // store the current rule for logging purposes
                         currentRule = rule.getClass().getName();
                         log.debug( "Executing rule: " + currentRule );
                         try
@@ -304,5 +308,21 @@
     public void setTranslator( PathTranslator theTranslator )
     {
         this.translator = theTranslator;
+    }
+
+    /**
+     * @return the dependenciesAreResolved
+     */
+    public boolean isDependenciesAreResolved ()
+    {
+        return this.dependenciesAreResolved;
+    }
+
+    /**
+     * @param theDependenciesAreResolved the dependenciesAreResolved to set
+     */
+    public void setDependenciesAreResolved ( boolean theDependenciesAreResolved )
+    {
+        this.dependenciesAreResolved = theDependenciesAreResolved;
     }
 }

Modified: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/NoSnapshots.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/NoSnapshots.java?view=diff&rev=565767&r1=565766&r2=565767
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/NoSnapshots.java (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/NoSnapshots.java Tue Aug 14 07:19:43 2007
@@ -19,23 +19,12 @@
  * under the License.
  */
 
-import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
-import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
-import org.apache.maven.artifact.versioning.VersionRange;
-import org.apache.maven.plugin.enforcer.util.EnforcerUtils;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.shared.enforcer.rule.api.EnforcerRule;
 import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
-import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
-import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
-import org.codehaus.plexus.util.StringUtils;
 
 /**
  * This rule checks that no snapshots are included.

Added: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/PluginVersionsDefined.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/PluginVersionsDefined.java?view=auto&rev=565767
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/PluginVersionsDefined.java (added)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/PluginVersionsDefined.java Tue Aug 14 07:19:43 2007
@@ -0,0 +1,509 @@
+package org.apache.maven.plugin.enforcer;
+
+/*
+ * 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.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import org.apache.maven.BuildFailureException;
+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.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.InvalidPluginException;
+import org.apache.maven.plugin.PluginManager;
+import org.apache.maven.plugin.PluginManagerException;
+import org.apache.maven.plugin.PluginNotFoundException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugin.version.PluginVersionNotFoundException;
+import org.apache.maven.plugin.version.PluginVersionResolutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRule;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+import org.codehaus.plexus.util.ReflectionUtils;
+import org.codehaus.plexus.util.StringUtils;
+
+/**
+ * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
+ * @version $Id$
+ * 
+ * This rule will enforce that all plugins specified in the
+ * poms have a version declared.
+ */
+public class PluginVersionsDefined
+    implements EnforcerRule
+{
+
+    /**
+     * The message to be printed in case the condition
+     * returns <b>true</b>
+     * 
+     * @required
+     * @parameter
+     */
+    public String message;
+
+    /**
+     * The message to be printed in case the condition
+     * returns <b>true</b>
+     * 
+     * @required
+     * @parameter
+     */
+    public boolean banLatest = true;
+
+    /**
+     * The message to be printed in case the condition
+     * returns <b>true</b>
+     * 
+     * @required
+     * @parameter
+     */
+    public boolean banRelease = true;
+
+    private PluginManager pluginManager;
+
+    private Map phaseToLifecycleMap;
+
+    private List lifecycles;
+
+    Log log;
+
+    public void execute ( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        log = helper.getLog();
+
+
+        MavenProject project;
+        try
+        {
+            // get the various expressions out of the helper.
+            project = (MavenProject) helper.evaluate( "${project}" );
+            LifecycleExecutor life;
+            life = (LifecycleExecutor) helper.getComponent( LifecycleExecutor.class );
+            MavenSession session = (MavenSession) helper.evaluate( "${session}" );
+            pluginManager = (PluginManager) helper.getComponent( PluginManager.class );
+
+            //I couldn't find a direct way to get at the lifecycles list.
+            lifecycles = (List) ReflectionUtils.getValueIncludingSuperclasses( "lifecycles", life );
+
+            // hardcoded for now
+            Lifecycle lifecycle = getLifecycleForPhase( "deploy" );
+
+            Set allPlugins = getAllPlugins( session, project, lifecycle );
+
+            log.debug( "All Plugins: " + allPlugins );
+
+            
+            //NOTE: This code here is not getting the raw model. Substitute with a method to get the 
+            //raw pom values for plugins and pluginManagement and the rest should be good.
+            List plugins = new ArrayList();
+            List pluginMgt = new ArrayList();
+            try
+            {
+                plugins = project.getModel().getBuild().getPlugins();
+            }
+            catch ( NullPointerException e )
+            {
+                // nothing set, just skip it
+            }
+            try
+            {
+                pluginMgt = project.getModel().getBuild().getPluginManagement().getPlugins();
+            }
+            catch ( NullPointerException e )
+            {
+                // nothing set, just skip it
+            }
+            
+            //end code attempting to get raw model.
+            
+            //now look for the versions that aren't valid and add to a list.
+            ArrayList failures = new ArrayList();
+            Iterator iter = allPlugins.iterator();
+            while ( iter.hasNext() )
+            {
+                Plugin plugin = (Plugin) iter.next();
+                if ( !hasVersionSpecified( plugin, plugins, banRelease, banLatest )
+                    && !hasVersionSpecified( plugin, pluginMgt, banRelease, banLatest ) )
+                {
+                    failures.add( plugin );
+                }
+            }
+
+            //if anything was found, log it then append the optional message.
+            if ( !failures.isEmpty() )
+            {
+                StringBuffer newMsg = new StringBuffer();
+                newMsg.append( "Some plugins are missing valid versions:\n" );
+                iter = failures.iterator();
+                while ( iter.hasNext() )
+                {
+                    Plugin plugin = (Plugin) iter.next();
+                    newMsg.append( plugin.getGroupId() + ":" + plugin.getArtifactId() + "\n" );
+                }
+                newMsg.append( message );
+
+                throw new EnforcerRuleException( newMsg.toString() );
+            }
+        }
+        catch ( ExpressionEvaluationException e )
+        {
+            throw new EnforcerRuleException( "Unable to Evaluate an Expression:" + e.getLocalizedMessage() );
+        }
+        catch ( ComponentLookupException e )
+        {
+            throw new EnforcerRuleException( "Unable to lookup a component:" + e.getLocalizedMessage() );
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new EnforcerRuleException( e.getLocalizedMessage() );
+        }
+        catch ( BuildFailureException e )
+        {
+            throw new EnforcerRuleException( e.getLocalizedMessage() );
+        }
+        catch ( LifecycleExecutionException e )
+        {
+            throw new EnforcerRuleException( e.getLocalizedMessage() );
+        }
+        catch ( PluginNotFoundException e )
+        {
+            throw new EnforcerRuleException( e.getLocalizedMessage() );
+        }
+
+    }
+
+    /*
+     * Checks to see if the version is specified for the plugin. Can optionally ban "RELEASE" or "LATEST" even
+     * if specified.
+     */
+    public boolean hasVersionSpecified ( Plugin source, List plugins, boolean banRelease, boolean banLatest )
+    {
+        Iterator iter = plugins.iterator();
+        while ( iter.hasNext() )
+        {
+            // find the matching plugin entry
+            Plugin plugin = (Plugin) iter.next();
+            if ( source.getArtifactId().equals( plugin.getArtifactId() )
+                && source.getGroupId().equals( plugin.getGroupId() ) )
+            {
+                // found the entry. now see if the version
+                // is specified
+                if ( StringUtils.isNotEmpty( plugin.getVersion() ) )
+                {
+                    if ( banRelease && plugin.getVersion().equals( "RELEASE" ) )
+                    {
+                        return false;
+                    }
+
+                    if ( banLatest && plugin.getVersion().equals( "LATEST" ) )
+                    {
+                        return false;
+                    }
+                    // the version was specified and not
+                    // banned. It's ok.
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    /*
+     * Uses borrowed lifecycle code to get a list of all plugins bound to the lifecycle.
+     */
+    private Set getAllPlugins ( MavenSession session, MavenProject project, Lifecycle lifecycle )
+        throws PluginNotFoundException, LifecycleExecutionException
+
+    {
+        HashSet plugins = new HashSet();
+        // first, bind those associated with the packaging
+        Map mappings = findMappingsForLifecycle( session, project, lifecycle );
+
+        Iterator iter = mappings.entrySet().iterator();
+        while ( iter.hasNext() )
+        {
+            Entry entry = (Entry) iter.next();
+            String value = (String) entry.getValue();
+            String tokens[] = value.split( ":" );
+
+            Plugin plugin = new Plugin();
+            plugin.setGroupId( tokens[0] );
+            plugin.setArtifactId( tokens[1] );
+            plugins.add( plugin );
+        }
+
+        //NOTE: I'm not positive of the format returned here. Needs more investigation.
+        //For now, I assume it's the same as the method above.
+        List mojos = findOptionalMojosForLifecycle( session, project, lifecycle );
+        iter = mojos.iterator();
+        while ( iter.hasNext() )
+        {
+            Entry entry = (Entry) iter.next();
+            String value = (String) entry.getValue();
+            String tokens[] = value.split( ":" );
+
+            Plugin plugin = new Plugin();
+            plugin.setGroupId( tokens[0] );
+            plugin.setArtifactId( tokens[1] );
+            plugins.add( plugin );
+        }
+
+        for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
+        {
+            plugins.add( i.next() );
+        }
+
+        return plugins;
+    }
+
+    
+    /*
+     * NOTE:
+     * All the code following this point was scooped from the DefaultLifecycleExecutor. There must be a 
+     * better way but for now it should work.
+     * 
+     */
+    public Map getPhaseToLifecycleMap ()
+        throws LifecycleExecutionException
+    {
+        if ( phaseToLifecycleMap == null )
+        {
+            phaseToLifecycleMap = new HashMap();
+
+            for ( Iterator i = lifecycles.iterator(); i.hasNext(); )
+            {
+                Lifecycle lifecycle = (Lifecycle) i.next();
+
+                for ( Iterator p = lifecycle.getPhases().iterator(); p.hasNext(); )
+                {
+                    String phase = (String) p.next();
+
+                    if ( phaseToLifecycleMap.containsKey( phase ) )
+                    {
+                        Lifecycle prevLifecycle = (Lifecycle) phaseToLifecycleMap.get( phase );
+                        throw new LifecycleExecutionException( "Phase '" + phase
+                            + "' is defined in more than one lifecycle: '" + lifecycle.getId() + "' and '"
+                            + prevLifecycle.getId() + "'" );
+                    }
+                    else
+                    {
+                        phaseToLifecycleMap.put( phase, lifecycle );
+                    }
+                }
+            }
+        }
+        return phaseToLifecycleMap;
+    }
+
+    private Lifecycle getLifecycleForPhase ( String phase )
+        throws BuildFailureException, LifecycleExecutionException
+    {
+        Lifecycle lifecycle = (Lifecycle) getPhaseToLifecycleMap().get( phase );
+
+        if ( lifecycle == null )
+        {
+            throw new BuildFailureException( "Unable to find lifecycle for phase '" + phase + "'" );
+        }
+        return lifecycle;
+    }
+
+    private Map findMappingsForLifecycle ( MavenSession session, MavenProject project, Lifecycle lifecycle )
+        throws LifecycleExecutionException, PluginNotFoundException
+    {
+        String packaging = project.getPackaging();
+        Map mappings = null;
+
+        LifecycleMapping m = (LifecycleMapping) findExtension( project, LifecycleMapping.ROLE, packaging, session
+            .getSettings(), session.getLocalRepository() );
+        if ( m != null )
+        {
+            mappings = m.getPhases( lifecycle.getId() );
+        }
+
+        Map defaultMappings = lifecycle.getDefaultPhases();
+
+        if ( mappings == null )
+        {
+            try
+            {
+                m = (LifecycleMapping) session.lookup( LifecycleMapping.ROLE, packaging );
+                mappings = m.getPhases( lifecycle.getId() );
+            }
+            catch ( ComponentLookupException e )
+            {
+                if ( defaultMappings == null )
+                {
+                    throw new LifecycleExecutionException( "Cannot find lifecycle mapping for packaging: \'"
+                        + packaging + "\'.", e );
+                }
+            }
+        }
+
+        if ( mappings == null )
+        {
+            if ( defaultMappings == null )
+            {
+                throw new LifecycleExecutionException( "Cannot find lifecycle mapping for packaging: \'" + packaging
+                    + "\', and there is no default" );
+            }
+            else
+            {
+                mappings = defaultMappings;
+            }
+        }
+
+        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 )
+            {
+                log.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
+    {
+        Object pluginComponent = null;
+
+        for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext() && pluginComponent == null; )
+        {
+            Plugin plugin = (Plugin) i.next();
+
+            if ( plugin.isExtensions() )
+            {
+                verifyPlugin( plugin, project, settings, localRepository );
+
+                // TODO: if moved to the plugin manager we
+                // already have the descriptor from above
+                // and so do can lookup the container
+                // directly
+                try
+                {
+                    pluginComponent = pluginManager.getPluginComponent( plugin, role, roleHint );
+                }
+                catch ( ComponentLookupException e )
+                {
+                    log.debug( "Unable to find the lifecycle component in the extension", e );
+                }
+                catch ( PluginManagerException e )
+                {
+                    throw new LifecycleExecutionException( "Error getting extensions from the plugin '"
+                        + plugin.getKey() + "': " + e.getMessage(), e );
+                }
+            }
+        }
+        return pluginComponent;
+    }
+
+    private PluginDescriptor verifyPlugin ( Plugin plugin, MavenProject project, Settings settings,
+                                            ArtifactRepository localRepository )
+        throws LifecycleExecutionException, PluginNotFoundException
+    {
+        PluginDescriptor pluginDescriptor;
+        try
+        {
+            pluginDescriptor = pluginManager.verifyPlugin( plugin, project, settings, localRepository );
+        }
+        catch ( PluginManagerException e )
+        {
+            throw new LifecycleExecutionException( "Internal error in the plugin manager getting plugin '"
+                + plugin.getKey() + "': " + e.getMessage(), e );
+        }
+        catch ( PluginVersionResolutionException e )
+        {
+            throw new LifecycleExecutionException( e.getMessage(), e );
+        }
+        catch ( InvalidVersionSpecificationException e )
+        {
+            throw new LifecycleExecutionException( e.getMessage(), e );
+        }
+        catch ( InvalidPluginException e )
+        {
+            throw new LifecycleExecutionException( e.getMessage(), e );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            throw new LifecycleExecutionException( e.getMessage(), e );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            throw new LifecycleExecutionException( e.getMessage(), e );
+        }
+        catch ( PluginVersionNotFoundException e )
+        {
+            throw new LifecycleExecutionException( e.getMessage(), e );
+        }
+        return pluginDescriptor;
+    }
+
+}
\ No newline at end of file