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