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