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