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