001    package 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    
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import org.apache.maven.plugin.descriptor.MojoDescriptor;
026    import org.apache.maven.plugin.descriptor.Parameter;
027    import org.codehaus.plexus.util.StringUtils;
028    
029    public class PluginParameterException
030        extends PluginConfigurationException
031    {
032    
033        private final List<Parameter> parameters;
034    
035        private final MojoDescriptor mojo;
036    
037        public PluginParameterException( MojoDescriptor mojo, List<Parameter> parameters )
038        {
039            super( mojo.getPluginDescriptor(), "The parameters " + format( parameters ) + " for goal "
040                + mojo.getRoleHint() + " are missing or invalid" );
041    
042            this.mojo = mojo;
043    
044            this.parameters = parameters;
045        }
046    
047        private static String format( List<Parameter> parameters )
048        {
049            StringBuilder buffer = new StringBuilder( 128 );
050            if ( parameters != null )
051            {
052                for ( Parameter parameter : parameters )
053                {
054                    if ( buffer.length() > 0 )
055                    {
056                        buffer.append( ", " );
057                    }
058                    buffer.append( '\'' ).append( parameter.getName() ).append( '\'' );
059                }
060            }
061            return buffer.toString();
062        }
063    
064        public MojoDescriptor getMojoDescriptor()
065        {
066            return mojo;
067        }
068    
069        public List<Parameter> getParameters()
070        {
071            return parameters;
072        }
073    
074        private static void decomposeParameterIntoUserInstructions( MojoDescriptor mojo, Parameter param,
075                                                                    StringBuilder messageBuffer )
076        {
077            String expression = param.getExpression();
078    
079            if ( param.isEditable() )
080            {
081                messageBuffer.append( "Inside the definition for plugin \'" + mojo.getPluginDescriptor().getArtifactId()
082                    + "\' specify the following:\n\n<configuration>\n  ...\n  <" + param.getName() + ">VALUE</"
083                    + param.getName() + ">\n</configuration>" );
084    
085                String alias = param.getAlias();
086                if ( StringUtils.isNotEmpty( alias ) && !alias.equals( param.getName() ) )
087                {
088                    messageBuffer.append(
089                        "\n\n-OR-\n\n<configuration>\n  ...\n  <" + alias + ">VALUE</" + alias + ">\n</configuration>\n" );
090                }
091            }
092    
093            if ( StringUtils.isEmpty( expression ) )
094            {
095                messageBuffer.append( "." );
096            }
097            else
098            {
099                if ( param.isEditable() )
100                {
101                    messageBuffer.append( "\n\n-OR-\n\n" );
102                }
103    
104                //addParameterUsageInfo( expression, messageBuffer );
105            }
106        }
107    
108        public String buildDiagnosticMessage()
109        {
110            StringBuilder messageBuffer = new StringBuilder( 256 );
111    
112            List<Parameter> params = getParameters();
113            MojoDescriptor mojo = getMojoDescriptor();
114    
115            messageBuffer.append( "One or more required plugin parameters are invalid/missing for \'" )
116                .append( mojo.getPluginDescriptor().getGoalPrefix() ).append( ":" ).append( mojo.getGoal() )
117                .append( "\'\n" );
118    
119            int idx = 0;
120            for ( Iterator<Parameter> it = params.iterator(); it.hasNext(); idx++ )
121            {
122                Parameter param = it.next();
123    
124                messageBuffer.append( "\n[" ).append( idx ).append( "] " );
125    
126                decomposeParameterIntoUserInstructions( mojo, param, messageBuffer );
127    
128                messageBuffer.append( "\n" );
129            }
130    
131            return messageBuffer.toString();
132        }
133    }