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.io.File;
23 import java.util.Properties;
24
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.plugin.descriptor.MojoDescriptor;
27 import org.apache.maven.plugin.descriptor.PluginDescriptor;
28 import org.apache.maven.project.MavenProject;
29 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
30 import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
31 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public class PluginParameterExpressionEvaluator
74 implements TypeAwareExpressionEvaluator
75 {
76 private MavenSession session;
77
78 private MojoExecution mojoExecution;
79
80 private MavenProject project;
81
82 private String basedir;
83
84 private Properties properties;
85
86 public PluginParameterExpressionEvaluator( MavenSession session )
87 {
88 this( session, null );
89 }
90
91 public PluginParameterExpressionEvaluator( MavenSession session, MojoExecution mojoExecution )
92 {
93 this.session = session;
94 this.mojoExecution = mojoExecution;
95 this.properties = new Properties();
96 this.project = session.getCurrentProject();
97
98
99
100
101
102 this.properties.putAll( session.getUserProperties() );
103 this.properties.putAll( session.getSystemProperties() );
104
105 String basedir = null;
106
107 if ( project != null )
108 {
109 File projectFile = project.getBasedir();
110
111
112 if ( projectFile != null )
113 {
114 basedir = projectFile.getAbsolutePath();
115 }
116 }
117
118 if ( basedir == null )
119 {
120 basedir = session.getExecutionRootDirectory();
121 }
122
123 if ( basedir == null )
124 {
125 basedir = System.getProperty( "user.dir" );
126 }
127
128 this.basedir = basedir;
129 }
130
131 @Override
132 public Object evaluate( String expr )
133 throws ExpressionEvaluationException
134 {
135 return evaluate( expr, null );
136 }
137
138 @Override
139 @SuppressWarnings( "checkstyle:methodlength" )
140 public Object evaluate( String expr, Class<?> type )
141 throws ExpressionEvaluationException
142 {
143 Object value = null;
144
145 if ( expr == null )
146 {
147 return null;
148 }
149
150 String expression = stripTokens( expr );
151 if ( expression.equals( expr ) )
152 {
153 int index = expr.indexOf( "${" );
154 if ( index >= 0 )
155 {
156 int lastIndex = expr.indexOf( '}', index );
157 if ( lastIndex >= 0 )
158 {
159 String retVal = expr.substring( 0, index );
160
161 if ( ( index > 0 ) && ( expr.charAt( index - 1 ) == '$' ) )
162 {
163 retVal += expr.substring( index + 1, lastIndex + 1 );
164 }
165 else
166 {
167 Object subResult = evaluate( expr.substring( index, lastIndex + 1 ) );
168
169 if ( subResult != null )
170 {
171 retVal += subResult;
172 }
173 else
174 {
175 retVal += "$" + expr.substring( index + 1, lastIndex + 1 );
176 }
177 }
178
179 retVal += evaluate( expr.substring( lastIndex + 1 ) );
180 return retVal;
181 }
182 }
183
184
185 if ( expression.contains( "$$" ) )
186 {
187 return expression.replaceAll( "\\$\\$", "\\$" );
188 }
189 else
190 {
191 return expression;
192 }
193 }
194
195 MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
196
197 if ( "localRepository".equals( expression ) )
198 {
199 value = session.getLocalRepository();
200 }
201 else if ( "session".equals( expression ) )
202 {
203 value = session;
204 }
205 else if ( expression.startsWith( "session" ) )
206 {
207 try
208 {
209 int pathSeparator = expression.indexOf( '/' );
210
211 if ( pathSeparator > 0 )
212 {
213 String pathExpression = expression.substring( 1, pathSeparator );
214 value = ReflectionValueExtractor.evaluate( pathExpression, session );
215 value = value + expression.substring( pathSeparator );
216 }
217 else
218 {
219 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), session );
220 }
221 }
222 catch ( Exception e )
223 {
224
225 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
226 e );
227 }
228 }
229 else if ( "reactorProjects".equals( expression ) )
230 {
231 value = session.getProjects();
232 }
233 else if ( "project".equals( expression ) )
234 {
235 value = project;
236 }
237 else if ( "executedProject".equals( expression ) )
238 {
239 value = project.getExecutionProject();
240 }
241 else if ( expression.startsWith( "project" ) || expression.startsWith( "pom" ) )
242 {
243 try
244 {
245 int pathSeparator = expression.indexOf( '/' );
246
247 if ( pathSeparator > 0 )
248 {
249 String pathExpression = expression.substring( 0, pathSeparator );
250 value = ReflectionValueExtractor.evaluate( pathExpression, project );
251 value = value + expression.substring( pathSeparator );
252 }
253 else
254 {
255 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), project );
256 }
257 }
258 catch ( Exception e )
259 {
260
261 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
262 e );
263 }
264 }
265 else if ( expression.equals( "repositorySystemSession" ) )
266 {
267 value = session.getRepositorySession();
268 }
269 else if ( expression.equals( "mojo" ) || expression.equals( "mojoExecution" ) )
270 {
271 value = mojoExecution;
272 }
273 else if ( expression.startsWith( "mojo" ) )
274 {
275 try
276 {
277 int pathSeparator = expression.indexOf( '/' );
278
279 if ( pathSeparator > 0 )
280 {
281 String pathExpression = expression.substring( 1, pathSeparator );
282 value = ReflectionValueExtractor.evaluate( pathExpression, mojoExecution );
283 value = value + expression.substring( pathSeparator );
284 }
285 else
286 {
287 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), mojoExecution );
288 }
289 }
290 catch ( Exception e )
291 {
292
293 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
294 e );
295 }
296 }
297 else if ( expression.equals( "plugin" ) )
298 {
299 value = mojoDescriptor.getPluginDescriptor();
300 }
301 else if ( expression.startsWith( "plugin" ) )
302 {
303 try
304 {
305 int pathSeparator = expression.indexOf( '/' );
306
307 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
308
309 if ( pathSeparator > 0 )
310 {
311 String pathExpression = expression.substring( 1, pathSeparator );
312 value = ReflectionValueExtractor.evaluate( pathExpression, pluginDescriptor );
313 value = value + expression.substring( pathSeparator );
314 }
315 else
316 {
317 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), pluginDescriptor );
318 }
319 }
320 catch ( Exception e )
321 {
322 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
323 e );
324 }
325 }
326 else if ( "settings".equals( expression ) )
327 {
328 value = session.getSettings();
329 }
330 else if ( expression.startsWith( "settings" ) )
331 {
332 try
333 {
334 int pathSeparator = expression.indexOf( '/' );
335
336 if ( pathSeparator > 0 )
337 {
338 String pathExpression = expression.substring( 1, pathSeparator );
339 value = ReflectionValueExtractor.evaluate( pathExpression, session.getSettings() );
340 value = value + expression.substring( pathSeparator );
341 }
342 else
343 {
344 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), session.getSettings() );
345 }
346 }
347 catch ( Exception e )
348 {
349
350 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
351 e );
352 }
353 }
354 else if ( "basedir".equals( expression ) )
355 {
356 value = basedir;
357 }
358 else if ( expression.startsWith( "basedir" ) )
359 {
360 int pathSeparator = expression.indexOf( '/' );
361
362 if ( pathSeparator > 0 )
363 {
364 value = basedir + expression.substring( pathSeparator );
365 }
366 }
367
368
369
370
371
372
373
374
375
376 if ( value != null && type != null && !( value instanceof String ) && !isTypeCompatible( type, value ) )
377 {
378 value = null;
379 }
380
381 if ( value == null )
382 {
383
384
385 if ( properties != null )
386 {
387
388
389
390
391
392 value = properties.getProperty( expression );
393 }
394
395 if ( ( value == null ) && ( ( project != null ) && ( project.getProperties() != null ) ) )
396 {
397 value = project.getProperties().getProperty( expression );
398 }
399
400 }
401
402 if ( value instanceof String )
403 {
404
405
406 String val = (String) value;
407
408 int exprStartDelimiter = val.indexOf( "${" );
409
410 if ( exprStartDelimiter >= 0 )
411 {
412 if ( exprStartDelimiter > 0 )
413 {
414 value = val.substring( 0, exprStartDelimiter ) + evaluate( val.substring( exprStartDelimiter ) );
415 }
416 else
417 {
418 value = evaluate( val.substring( exprStartDelimiter ) );
419 }
420 }
421 }
422
423 return value;
424 }
425
426 private static boolean isTypeCompatible( Class<?> type, Object value )
427 {
428 if ( type.isInstance( value ) )
429 {
430 return true;
431 }
432
433 return ( ( type.isPrimitive() || type.getName().startsWith( "java.lang." ) )
434 && value.getClass().getName().startsWith( "java.lang." ) );
435 }
436
437 private String stripTokens( String expr )
438 {
439 if ( expr.startsWith( "${" ) && ( expr.indexOf( '}' ) == expr.length() - 1 ) )
440 {
441 expr = expr.substring( 2, expr.length() - 1 );
442 }
443 return expr;
444 }
445
446 @Override
447 public File alignToBaseDirectory( File file )
448 {
449
450
451 if ( file != null )
452 {
453 if ( file.isAbsolute() )
454 {
455
456 }
457 else if ( file.getPath().startsWith( File.separator ) )
458 {
459
460 file = file.getAbsoluteFile();
461 }
462 else
463 {
464
465 file = new File( new File( basedir, file.getPath() ).toURI().normalize() ).getAbsoluteFile();
466 }
467 }
468 return file;
469 }
470
471 }