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 org.apache.maven.plugin.descriptor.MojoDescriptor;
23  import org.apache.maven.plugin.descriptor.Parameter;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  import java.util.Iterator;
27  import java.util.List;
28  
29  public class PluginParameterException
30      extends PluginConfigurationException
31  {
32  
33      private final List parameters;
34  
35      private final MojoDescriptor mojo;
36  
37      public PluginParameterException( MojoDescriptor mojo, List parameters )
38      {
39          super( mojo.getPluginDescriptor(),
40                 "Invalid or missing parameters: " + parameters + " for mojo: " + mojo.getRoleHint() );
41  
42          this.mojo = mojo;
43  
44          this.parameters = parameters;
45      }
46  
47      public PluginParameterException( MojoDescriptor mojo, List parameters, Throwable cause )
48      {
49          super( mojo.getPluginDescriptor(),
50                 "Invalid or missing parameters: " + parameters + " for mojo: " + mojo.getRoleHint(), cause );
51  
52          this.mojo = mojo;
53  
54          this.parameters = parameters;
55      }
56  
57      public MojoDescriptor getMojoDescriptor()
58      {
59          return mojo;
60      }
61  
62      public List getParameters()
63      {
64          return parameters;
65      }
66  
67      private static void decomposeParameterIntoUserInstructions( MojoDescriptor mojo, Parameter param,
68                                                                  StringBuffer messageBuffer )
69      {
70          String expression = param.getExpression();
71  
72          if ( param.isEditable() )
73          {
74              messageBuffer.append( "Inside the definition for plugin \'" + mojo.getPluginDescriptor().getArtifactId() +
75                  "\' specify the following:\n\n<configuration>\n  ...\n  <" + param.getName() + ">VALUE</" +
76                  param.getName() + ">\n</configuration>" );
77  
78              String alias = param.getAlias();
79              if ( StringUtils.isNotEmpty( alias ) && !alias.equals( param.getName() ) )
80              {
81                  messageBuffer.append(
82                      "\n\n-OR-\n\n<configuration>\n  ...\n  <" + alias + ">VALUE</" + alias + ">\n</configuration>\n" );
83              }
84          }
85  
86          if ( StringUtils.isEmpty( expression ) )
87          {
88              messageBuffer.append( "." );
89          }
90          else
91          {
92              if ( param.isEditable() )
93              {
94                  messageBuffer.append( "\n\n-OR-\n\n" );
95              }
96  
97              addParameterUsageInfo( expression, messageBuffer );
98          }
99      }
100 
101     public String buildDiagnosticMessage()
102     {
103         StringBuffer messageBuffer = new StringBuffer();
104 
105         List params = getParameters();
106         MojoDescriptor mojo = getMojoDescriptor();
107 
108         messageBuffer.append( "One or more required plugin parameters are invalid/missing for \'" )
109             .append( mojo.getPluginDescriptor().getGoalPrefix() ).append( ":" ).append( mojo.getGoal() )
110             .append( "\'\n" );
111 
112         int idx = 0;
113         for ( Iterator it = params.iterator(); it.hasNext(); idx++ )
114         {
115             Parameter param = (Parameter) it.next();
116 
117             messageBuffer.append( "\n[" ).append( idx ).append( "] " );
118 
119             decomposeParameterIntoUserInstructions( mojo, param, messageBuffer );
120 
121             messageBuffer.append( "\n" );
122         }
123 
124         return messageBuffer.toString();
125     }
126 }