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