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