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