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  /**
33   * PluginParameterException
34   */
35  public class PluginParameterException
36      extends PluginConfigurationException
37  {
38  
39      private final List<Parameter> parameters;
40  
41      private final MojoDescriptor mojo;
42  
43      public PluginParameterException( MojoDescriptor mojo, List<Parameter> parameters )
44      {
45          super( mojo.getPluginDescriptor(), "The parameters " + format( parameters ) + " for goal "
46              + mojo.getRoleHint() + " are missing or invalid" );
47  
48          this.mojo = mojo;
49  
50          this.parameters = parameters;
51      }
52  
53      private static String format( List<Parameter> parameters )
54      {
55          StringBuilder buffer = new StringBuilder( 128 );
56          if ( parameters != null )
57          {
58              for ( Parameter parameter : parameters )
59              {
60                  if ( buffer.length() > 0 )
61                  {
62                      buffer.append( ", " );
63                  }
64                  buffer.append( '\'' ).append( parameter.getName() ).append( '\'' );
65              }
66          }
67          return buffer.toString();
68      }
69  
70      public MojoDescriptor getMojoDescriptor()
71      {
72          return mojo;
73      }
74  
75      public List<Parameter> getParameters()
76      {
77          return parameters;
78      }
79  
80      private static void decomposeParameterIntoUserInstructions( MojoDescriptor mojo, Parameter param,
81                                                                  StringBuilder messageBuffer )
82      {
83          String expression = param.getExpression();
84  
85          if ( param.isEditable() )
86          {
87              boolean isArray = param.getType().endsWith( "[]" );
88              boolean isCollection = false;
89              boolean isMap = false;
90              boolean isProperties = false;
91              if ( !isArray )
92              {
93                  try
94                  {
95                      //assuming Type is available in current ClassLoader
96                      isCollection = Collection.class.isAssignableFrom( Class.forName( param.getType() ) );
97                      isMap = Map.class.isAssignableFrom( Class.forName( param.getType() ) );
98                      isProperties = Properties.class.isAssignableFrom( Class.forName( param.getType() ) );
99                  }
100                 catch ( ClassNotFoundException e )
101                 {
102                     // assume it is not assignable from Collection or Map
103                 }
104             }
105 
106             messageBuffer.append( "Inside the definition for plugin \'" );
107             messageBuffer.append( mojo.getPluginDescriptor().getArtifactId() );
108             messageBuffer.append( "\', specify the following:\n\n<configuration>\n  ...\n" );
109             messageBuffer.append( "  <" ).append( param.getName() ).append( '>' );
110             if ( isArray || isCollection )
111             {
112                 messageBuffer.append( '\n' );
113                 messageBuffer.append( "    <item>" );
114             }
115             else if ( isProperties )
116             {
117                 messageBuffer.append( '\n' );
118                 messageBuffer.append( "    <property>\n" );
119                 messageBuffer.append( "      <name>KEY</name>\n" );
120                 messageBuffer.append( "      <value>" );
121             }
122             else if ( isMap )
123             {
124                 messageBuffer.append( '\n' );
125                 messageBuffer.append( "    <KEY>" );
126             }
127             messageBuffer.append( "VALUE" );
128             if ( isArray || isCollection )
129             {
130                 messageBuffer.append( "</item>\n" );
131                 messageBuffer.append( "  " );
132             }
133             else if ( isProperties )
134             {
135                 messageBuffer.append( "</value>\n" );
136                 messageBuffer.append( "    </property>\n" );
137                 messageBuffer.append( "  " );
138             }
139             else if ( isMap )
140             {
141                 messageBuffer.append( "</KEY>\n" );
142                 messageBuffer.append( "  " );
143             }
144             messageBuffer.append( "</" ).append( param.getName() ).append( ">\n" );
145             messageBuffer.append( "</configuration>" );
146 
147             String alias = param.getAlias();
148             if ( StringUtils.isNotEmpty( alias ) && !alias.equals( param.getName() ) )
149             {
150                 messageBuffer.append( "\n\n-OR-\n\n<configuration>\n  ...\n  <" ).append( alias ).append(
151                     ">VALUE</" ).append( alias ).append( ">\n</configuration>\n" );
152             }
153         }
154 
155         if ( StringUtils.isEmpty( expression ) )
156         {
157             messageBuffer.append( '.' );
158         }
159         else
160         {
161             if ( param.isEditable() )
162             {
163                 messageBuffer.append( "\n\n-OR-\n\n" );
164             }
165 
166             //addParameterUsageInfo( expression, messageBuffer );
167         }
168     }
169 
170     public String buildDiagnosticMessage()
171     {
172         StringBuilder messageBuffer = new StringBuilder( 256 );
173 
174         List<Parameter> params = getParameters();
175         MojoDescriptor mojo = getMojoDescriptor();
176 
177         messageBuffer.append( "One or more required plugin parameters are invalid/missing for \'" )
178             .append( mojo.getPluginDescriptor().getGoalPrefix() ).append( ':' ).append( mojo.getGoal() )
179             .append( "\'\n" );
180 
181         int idx = 0;
182         for ( Iterator<Parameter> it = params.iterator(); it.hasNext(); idx++ )
183         {
184             Parameter param = it.next();
185 
186             messageBuffer.append( "\n[" ).append( idx ).append( "] " );
187 
188             decomposeParameterIntoUserInstructions( mojo, param, messageBuffer );
189 
190             messageBuffer.append( '\n' );
191         }
192 
193         return messageBuffer.toString();
194     }
195 }