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