View Javadoc
1   package org.apache.maven.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import org.apache.maven.plugin.descriptor.MojoDescriptor;
29  import org.apache.maven.plugin.descriptor.Parameter;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  public class PluginParameterException
33      extends PluginConfigurationException
34  {
35  
36      private final List<Parameter> parameters;
37  
38      private final MojoDescriptor mojo;
39  
40      public PluginParameterException( MojoDescriptor mojo, List<Parameter> parameters )
41      {
42          super( mojo.getPluginDescriptor(), "The parameters " + format( parameters ) + " for goal "
43              + mojo.getRoleHint() + " are missing or invalid" );
44  
45          this.mojo = mojo;
46  
47          this.parameters = parameters;
48      }
49  
50      private static String format( List<Parameter> parameters )
51      {
52          StringBuilder buffer = new StringBuilder( 128 );
53          if ( parameters != null )
54          {
55              for ( Parameter parameter : parameters )
56              {
57                  if ( buffer.length() > 0 )
58                  {
59                      buffer.append( ", " );
60                  }
61                  buffer.append( '\'' ).append( parameter.getName() ).append( '\'' );
62              }
63          }
64          return buffer.toString();
65      }
66  
67      public MojoDescriptor getMojoDescriptor()
68      {
69          return mojo;
70      }
71  
72      public List<Parameter> getParameters()
73      {
74          return parameters;
75      }
76  
77      private static void decomposeParameterIntoUserInstructions( MojoDescriptor mojo, Parameter param,
78                                                                  StringBuilder messageBuffer )
79      {
80          String expression = param.getExpression();
81  
82          if ( param.isEditable() )
83          {
84              boolean isArray = param.getType().endsWith( "[]" );
85              boolean isCollection = false;
86              boolean isMap = false;
87              boolean isProperties = false;
88              if ( !isArray )
89              {
90                  try
91                  {
92                      //assuming Type is available in current ClassLoader
93                      isCollection = Collection.class.isAssignableFrom( Class.forName( param.getType() ) );
94                      isMap = Map.class.isAssignableFrom( Class.forName( param.getType() ) );
95                      isProperties = Properties.class.isAssignableFrom( Class.forName( param.getType() ) );
96                  }
97                  catch ( ClassNotFoundException e )
98                  {
99                      // assume it is not assignable from Collection or Map
100                 }
101             }
102 
103             messageBuffer.append( "Inside the definition for plugin \'" );
104             messageBuffer.append( mojo.getPluginDescriptor().getArtifactId() );
105             messageBuffer.append( "\', specify the following:\n\n<configuration>\n  ...\n" );
106             messageBuffer.append( "  <" ).append( param.getName() ).append( '>' );
107             if ( isArray || isCollection )
108             {
109                 messageBuffer.append( '\n' );
110                 messageBuffer.append( "    <item>" );
111             }
112             else if ( isProperties )
113             {
114                 messageBuffer.append( '\n' );
115                 messageBuffer.append( "    <property>\n" );
116                 messageBuffer.append( "      <name>KEY</name>\n" );
117                 messageBuffer.append( "      <value>" );
118             }
119             else if ( isMap )
120             {
121                 messageBuffer.append( '\n' );
122                 messageBuffer.append( "    <KEY>" );
123             }
124             messageBuffer.append( "VALUE" );
125             if ( isArray || isCollection )
126             {
127                 messageBuffer.append( "</item>\n" );
128                 messageBuffer.append( "  " );
129             }
130             else if ( isProperties )
131             {
132                 messageBuffer.append( "</value>\n" );
133                 messageBuffer.append( "    </property>\n" );
134                 messageBuffer.append( "  " );
135             }
136             else if ( isMap )
137             {
138                 messageBuffer.append( "</KEY>\n" );
139                 messageBuffer.append( "  " );
140             }
141             messageBuffer.append( "</" ).append( param.getName() ).append( ">\n" );
142             messageBuffer.append( "</configuration>" );
143 
144             String alias = param.getAlias();
145             if ( StringUtils.isNotEmpty( alias ) && !alias.equals( param.getName() ) )
146             {
147                 messageBuffer.append( "\n\n-OR-\n\n<configuration>\n  ...\n  <" ).append( alias ).append(
148                     ">VALUE</" ).append( alias ).append( ">\n</configuration>\n" );
149             }
150         }
151 
152         if ( StringUtils.isEmpty( expression ) )
153         {
154             messageBuffer.append( "." );
155         }
156         else
157         {
158             if ( param.isEditable() )
159             {
160                 messageBuffer.append( "\n\n-OR-\n\n" );
161             }
162 
163             //addParameterUsageInfo( expression, messageBuffer );
164         }
165     }
166 
167     public String buildDiagnosticMessage()
168     {
169         StringBuilder messageBuffer = new StringBuilder( 256 );
170 
171         List<Parameter> params = getParameters();
172         MojoDescriptor mojo = getMojoDescriptor();
173 
174         messageBuffer.append( "One or more required plugin parameters are invalid/missing for \'" )
175             .append( mojo.getPluginDescriptor().getGoalPrefix() ).append( ":" ).append( mojo.getGoal() )
176             .append( "\'\n" );
177 
178         int idx = 0;
179         for ( Iterator<Parameter> it = params.iterator(); it.hasNext(); idx++ )
180         {
181             Parameter param = it.next();
182 
183             messageBuffer.append( "\n[" ).append( idx ).append( "] " );
184 
185             decomposeParameterIntoUserInstructions( mojo, param, messageBuffer );
186 
187             messageBuffer.append( "\n" );
188         }
189 
190         return messageBuffer.toString();
191     }
192 }