private MojoDescriptor getMojoDescriptor()

in maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java [1742:1888]


    private MojoDescriptor getMojoDescriptor( final String task, final MavenSession session, final MavenProject project,
                                              final String invokedVia, final boolean canUsePrefix, final boolean isOptionalMojo )
        throws BuildFailureException, LifecycleExecutionException, PluginNotFoundException
    {
        String goal;
        Plugin plugin = null;

        PluginDescriptor pluginDescriptor = null;

        try
        {
            StringTokenizer tok = new StringTokenizer( task, ":" );
            int numTokens = tok.countTokens();

            if ( numTokens == 2 )
            {
                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();

                // Steps for retrieving the plugin model instance:
                // 1. request directly from the plugin collector by prefix
                pluginDescriptor = pluginManager.getPluginDescriptorForPrefix( prefix );
                if ( pluginDescriptor != null )
                {
                    plugin = new Plugin();
                    plugin.setGroupId( pluginDescriptor.getGroupId() );
                    plugin.setArtifactId( pluginDescriptor.getArtifactId() );
                    plugin.setVersion( pluginDescriptor.getVersion() );
                }

                // 2. 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 );
                        if ( prefix.equals( desc.getGoalPrefix() ) )
                        {
                            plugin = buildPlugin;
                            pluginDescriptor = desc;
                            break;
                        }
                    }
                }

                // 3. look in the repository via search groups
                if ( plugin == null )
                {
                    plugin = pluginManager.getPluginDefinitionForPrefix( prefix, session, project );
                }

                // 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 ) );
                }
            }
            else if ( numTokens == 3 || numTokens == 4 )
            {
                plugin = new Plugin();

                plugin.setGroupId( tok.nextToken() );
                plugin.setArtifactId( tok.nextToken() );

                if ( numTokens == 4 )
                {
                    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 );
            }

            if ( plugin.getVersion() == null )
            {
                for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
                {
                    Plugin buildPlugin = (Plugin) i.next();

                    if ( buildPlugin.getKey().equals( plugin.getKey() ) )
                    {
                        plugin = buildPlugin;
                        break;
                    }
                }

                project.injectPluginManagementInfo( plugin );
            }

            if ( pluginDescriptor == null )
            {
                pluginDescriptor = verifyPlugin( plugin, project, session );
            }

            // 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 )
            {
                if ( isOptionalMojo )
                {
                    getLogger().info( "Skipping missing optional mojo: " + task );
                }
                else
                {
                    throw new BuildFailureException( "Required goal not found: " + task + " in "
                        + pluginDescriptor.getId() );
                }
            }

            return mojoDescriptor;
        }
        catch ( PluginNotFoundException e )
        {
            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 null;
    }