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