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.Iterator;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.descriptor.MojoDescriptor;
26  import org.apache.maven.plugin.descriptor.Parameter;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  public class PluginParameterException
30      extends PluginConfigurationException
31  {
32  
33      private final List<Parameter> parameters;
34  
35      private final MojoDescriptor mojo;
36  
37      public PluginParameterException( MojoDescriptor mojo, List<Parameter> parameters )
38      {
39          super( mojo.getPluginDescriptor(), "The parameters " + format( parameters ) + " for goal "
40              + mojo.getRoleHint() + " are missing or invalid" );
41  
42          this.mojo = mojo;
43  
44          this.parameters = parameters;
45      }
46  
47      private static String format( List<Parameter> parameters )
48      {
49          StringBuilder buffer = new StringBuilder( 128 );
50          if ( parameters != null )
51          {
52              for ( Parameter parameter : parameters )
53              {
54                  if ( buffer.length() > 0 )
55                  {
56                      buffer.append( ", " );
57                  }
58                  buffer.append( '\'' ).append( parameter.getName() ).append( '\'' );
59              }
60          }
61          return buffer.toString();
62      }
63  
64      public MojoDescriptor getMojoDescriptor()
65      {
66          return mojo;
67      }
68  
69      public List<Parameter> getParameters()
70      {
71          return parameters;
72      }
73  
74      private static void decomposeParameterIntoUserInstructions( MojoDescriptor mojo, Parameter param,
75                                                                  StringBuilder messageBuffer )
76      {
77          String expression = param.getExpression();
78  
79          if ( param.isEditable() )
80          {
81              messageBuffer.append( "Inside the definition for plugin \'" + mojo.getPluginDescriptor().getArtifactId()
82                  + "\' specify the following:\n\n<configuration>\n  ...\n  <" + param.getName() + ">VALUE</"
83                  + param.getName() + ">\n</configuration>" );
84  
85              String alias = param.getAlias();
86              if ( StringUtils.isNotEmpty( alias ) && !alias.equals( param.getName() ) )
87              {
88                  messageBuffer.append(
89                      "\n\n-OR-\n\n<configuration>\n  ...\n  <" + alias + ">VALUE</" + alias + ">\n</configuration>\n" );
90              }
91          }
92  
93          if ( StringUtils.isEmpty( expression ) )
94          {
95              messageBuffer.append( "." );
96          }
97          else
98          {
99              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 }