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