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