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 return expression.replace( "$$", "$" );
196 }
197
198 MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
199
200 if ( "localRepository".equals( expression ) )
201 {
202 value = session.getLocalRepository();
203 }
204 else if ( "session".equals( expression ) )
205 {
206 value = session;
207 }
208 else if ( expression.startsWith( "session" ) )
209 {
210 try
211 {
212 int pathSeparator = expression.indexOf( '/' );
213
214 if ( pathSeparator > 0 )
215 {
216 String pathExpression = expression.substring( 1, pathSeparator );
217 value = ReflectionValueExtractor.evaluate( pathExpression, session );
218 value = value + expression.substring( pathSeparator );
219 }
220 else
221 {
222 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), session );
223 }
224 }
225 catch ( Exception e )
226 {
227
228 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
229 e );
230 }
231 }
232 else if ( "reactorProjects".equals( expression ) )
233 {
234 value = session.getProjects();
235 }
236 else if ( "mojoExecution".equals( expression ) )
237 {
238 value = mojoExecution;
239 }
240 else if ( "project".equals( expression ) )
241 {
242 value = project;
243 }
244 else if ( "executedProject".equals( expression ) )
245 {
246 value = project.getExecutionProject();
247 }
248 else if ( expression.startsWith( "project" ) || expression.startsWith( "pom" ) )
249 {
250 try
251 {
252 int pathSeparator = expression.indexOf( '/' );
253
254 if ( pathSeparator > 0 )
255 {
256 String pathExpression = expression.substring( 0, pathSeparator );
257 value = ReflectionValueExtractor.evaluate( pathExpression, project );
258 value = value + expression.substring( pathSeparator );
259 }
260 else
261 {
262 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), project );
263 }
264 }
265 catch ( Exception e )
266 {
267
268 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
269 e );
270 }
271 }
272 else if ( expression.equals( "repositorySystemSession" ) )
273 {
274 value = session.getRepositorySession();
275 }
276 else if ( expression.equals( "mojo" ) )
277 {
278 value = mojoExecution;
279 }
280 else if ( expression.startsWith( "mojo" ) )
281 {
282 try
283 {
284 int pathSeparator = expression.indexOf( '/' );
285
286 if ( pathSeparator > 0 )
287 {
288 String pathExpression = expression.substring( 1, pathSeparator );
289 value = ReflectionValueExtractor.evaluate( pathExpression, mojoExecution );
290 value = value + expression.substring( pathSeparator );
291 }
292 else
293 {
294 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), mojoExecution );
295 }
296 }
297 catch ( Exception e )
298 {
299
300 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
301 e );
302 }
303 }
304 else if ( expression.equals( "plugin" ) )
305 {
306 value = mojoDescriptor.getPluginDescriptor();
307 }
308 else if ( expression.startsWith( "plugin" ) )
309 {
310 try
311 {
312 int pathSeparator = expression.indexOf( '/' );
313
314 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
315
316 if ( pathSeparator > 0 )
317 {
318 String pathExpression = expression.substring( 1, pathSeparator );
319 value = ReflectionValueExtractor.evaluate( pathExpression, pluginDescriptor );
320 value = value + expression.substring( pathSeparator );
321 }
322 else
323 {
324 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), pluginDescriptor );
325 }
326 }
327 catch ( Exception e )
328 {
329 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
330 e );
331 }
332 }
333 else if ( "settings".equals( expression ) )
334 {
335 value = session.getSettings();
336 }
337 else if ( expression.startsWith( "settings" ) )
338 {
339 try
340 {
341 int pathSeparator = expression.indexOf( '/' );
342
343 if ( pathSeparator > 0 )
344 {
345 String pathExpression = expression.substring( 1, pathSeparator );
346 value = ReflectionValueExtractor.evaluate( pathExpression, session.getSettings() );
347 value = value + expression.substring( pathSeparator );
348 }
349 else
350 {
351 value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), session.getSettings() );
352 }
353 }
354 catch ( Exception e )
355 {
356
357 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
358 e );
359 }
360 }
361 else if ( "basedir".equals( expression ) )
362 {
363 value = basedir;
364 }
365 else if ( expression.startsWith( "basedir" ) )
366 {
367 int pathSeparator = expression.indexOf( '/' );
368
369 if ( pathSeparator > 0 )
370 {
371 value = basedir + expression.substring( pathSeparator );
372 }
373 }
374
375
376
377
378
379
380
381
382
383 if ( value != null && type != null && !( value instanceof String ) && !isTypeCompatible( type, value ) )
384 {
385 value = null;
386 }
387
388 if ( value == null )
389 {
390
391
392 if ( properties != null )
393 {
394
395
396
397
398
399 value = properties.getProperty( expression );
400 }
401
402 if ( ( value == null ) && ( ( project != null ) && ( project.getProperties() != null ) ) )
403 {
404 value = project.getProperties().getProperty( expression );
405 }
406
407 }
408
409 if ( value instanceof String )
410 {
411
412
413 String val = (String) value;
414
415 int exprStartDelimiter = val.indexOf( "${" );
416
417 if ( exprStartDelimiter >= 0 )
418 {
419 if ( exprStartDelimiter > 0 )
420 {
421 value = val.substring( 0, exprStartDelimiter ) + evaluate( val.substring( exprStartDelimiter ) );
422 }
423 else
424 {
425 value = evaluate( val.substring( exprStartDelimiter ) );
426 }
427 }
428 }
429
430 return value;
431 }
432
433 private static boolean isTypeCompatible( Class<?> type, Object value )
434 {
435 if ( type.isInstance( value ) )
436 {
437 return true;
438 }
439
440 return ( ( type.isPrimitive() || type.getName().startsWith( "java.lang." ) )
441 && value.getClass().getName().startsWith( "java.lang." ) );
442 }
443
444 private String stripTokens( String expr )
445 {
446 if ( expr.startsWith( "${" ) && ( expr.indexOf( '}' ) == expr.length() - 1 ) )
447 {
448 expr = expr.substring( 2, expr.length() - 1 );
449 }
450 return expr;
451 }
452
453 @Override
454 public File alignToBaseDirectory( File file )
455 {
456
457
458 if ( file != null )
459 {
460 if ( file.isAbsolute() )
461 {
462
463 }
464 else if ( file.getPath().startsWith( File.separator ) )
465 {
466
467 file = file.getAbsoluteFile();
468 }
469 else
470 {
471
472 file = new File( new File( basedir, file.getPath() ).toURI().normalize() ).getAbsoluteFile();
473 }
474 }
475 return file;
476 }
477
478 }