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