001package org.apache.maven.plugin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Iterator;
023import java.util.List;
024
025import org.apache.maven.plugin.descriptor.MojoDescriptor;
026import org.apache.maven.plugin.descriptor.PluginDescriptor;
027
028public class MojoNotFoundException
029    extends Exception
030{
031    private String goal;
032
033    private PluginDescriptor pluginDescriptor;
034
035    public MojoNotFoundException( String goal, PluginDescriptor pluginDescriptor )
036    {
037        super( toMessage( goal, pluginDescriptor ) );
038
039        this.goal = goal;
040        this.pluginDescriptor = pluginDescriptor;
041    }
042
043    public String getGoal()
044    {
045        return goal;
046    }
047
048    public PluginDescriptor getPluginDescriptor()
049    {
050        return pluginDescriptor;
051    }
052
053    private static String toMessage( String goal, PluginDescriptor pluginDescriptor )
054    {
055        StringBuilder buffer = new StringBuilder( 256 );
056
057        buffer.append( "Could not find goal '" ).append( goal ).append( "'" );
058
059        if ( pluginDescriptor != null )
060        {
061            buffer.append( " in plugin " ).append( pluginDescriptor.getId() );
062
063            buffer.append( " among available goals " );
064            List<MojoDescriptor> mojos = pluginDescriptor.getMojos();
065            if ( mojos != null )
066            {
067                for ( Iterator<MojoDescriptor> it = mojos.iterator(); it.hasNext(); )
068                {
069                    MojoDescriptor mojo = it.next();
070                    if ( mojo != null )
071                    {
072                        buffer.append( mojo.getGoal() );
073                    }
074                    if ( it.hasNext() )
075                    {
076                        buffer.append( ", " );
077                    }
078                }
079            }
080        }
081
082        return buffer.toString();
083    }
084
085}