1 package org.apache.maven.plugin;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * Evaluator for plugin parameters expressions. Content surrounded by <code>${</code> and <code>}</code> is evaluated.
37 * Recognized values are:
38 * <table border="1">
39 * <caption>Expression matrix</caption>
40 * <tr><th>expression</th> <th></th> <th>evaluation result</th></tr>
41 * <tr><td><code>session</code></td> <td></td> <td>the actual {@link MavenSession}</td></tr>
42 * <tr><td><code>session.*</code></td> <td>(since Maven 3)</td><td></td></tr>
43 * <tr><td><code>localRepository</code></td> <td></td>
44 * <td>{@link MavenSession#getLocalRepository()}</td></tr>
45 * <tr><td><code>reactorProjects</code></td> <td></td> <td>{@link MavenSession#getProjects()}</td></tr>
46 * <tr><td><code>repositorySystemSession</code></td><td> (since Maven 3)</td>
47 * <td>{@link MavenSession#getRepositorySession()}</td></tr>
48 * <tr><td><code>project</code></td> <td></td>
49 * <td>{@link MavenSession#getCurrentProject()}</td></tr>
50 * <tr><td><code>project.*</code></td> <td></td> <td></td></tr>
51 * <tr><td><code>pom.*</code></td> <td>(since Maven 3)</td><td>same as <code>project.*</code></td></tr>
52 * <tr><td><code>executedProject</code></td> <td></td>
53 * <td>{@link MavenProject#getExecutionProject()}</td></tr>
54 * <tr><td><code>settings</code></td> <td></td> <td>{@link MavenSession#getSettings()}</td></tr>
55 * <tr><td><code>settings.*</code></td> <td></td> <td></td></tr>
56 * <tr><td><code>basedir</code></td> <td></td>
57 * <td>{@link MavenSession#getExecutionRootDirectory()} or
58 * <code>System.getProperty( "user.dir" )</code> if null</td></tr>
59 * <tr><td><code>mojoExecution</code></td> <td></td> <td>the actual {@link MojoExecution}</td></tr>
60 * <tr><td><code>mojo</code></td> <td>(since Maven 3)</td><td>same as <code>mojoExecution</code></td></tr>
61 * <tr><td><code>mojo.*</code></td> <td>(since Maven 3)</td><td></td></tr>
62 * <tr><td><code>plugin</code></td> <td>(since Maven 3)</td>
63 * <td>{@link MojoExecution#getMojoDescriptor()}.{@link MojoDescriptor#getPluginDescriptor()
64 * getPluginDescriptor()}</td></tr>
65 * <tr><td><code>plugin.*</code></td> <td></td> <td></td></tr>
66 * <tr><td><code>*</code></td> <td></td> <td>system properties</td></tr>
67 * <tr><td><code>*</code></td> <td></td> <td>project properties</td></tr>
68 * </table>
69 * <i>Notice:</i> <code>reports</code> was supported in Maven 2.x but was removed in Maven 3
70 *
71 * @author Jason van Zyl
72 * @see MavenSession
73 * @see MojoExecution
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 //TODO used by the Enforcer plugin
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 // Maven4: We may want to evaluate how this is used but we add these separate as the
110 // getExecutionProperties is deprecated in MavenSession.
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 // this should always be the case for non-super POM instances...
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 // Was not an expression
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 // TODO don't catch exception
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 // TODO don't catch exception
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 // TODO don't catch exception
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 // TODO don't catch exception
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 * MNG-4312: We neither have reserved all of the above magic expressions nor is their set fixed/well-known (it
377 * gets occasionally extended by newer Maven versions). This imposes the risk for existing plugins to
378 * unintentionally use such a magic expression for an ordinary system property. So here we check whether we
379 * ended up with a magic value that is not compatible with the type of the configured mojo parameter (a string
380 * could still be converted by the configurator so we leave those alone). If so, back off to evaluating the
381 * expression from properties only.
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 // The CLI should win for defining properties
391
392 if ( properties != null )
393 {
394 // We will attempt to get nab a system property as a way to specify a
395 // parameter to a plugins. My particular case here is allowing the surefire
396 // plugin to run a single test so I want to specify that class on the cli
397 // as a parameter.
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 // TODO without #, this could just be an evaluate call...
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 // likely Boolean -> boolean, Short -> int etc. conversions, it's not the problem case we try to avoid
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 // TODO Copied from the DefaultInterpolator. We likely want to resurrect the PathTranslator or at least a
457 // similar component for re-usage
458 if ( file != null )
459 {
460 if ( file.isAbsolute() )
461 {
462 // path was already absolute, just normalize file separator and we're done
463 }
464 else if ( file.getPath().startsWith( File.separator ) )
465 {
466 // drive-relative Windows path, don't align with project directory but with drive root
467 file = file.getAbsoluteFile();
468 }
469 else
470 {
471 // an ordinary relative path, align with project directory
472 file = new File( new File( basedir, file.getPath() ).toURI().normalize() ).getAbsoluteFile();
473 }
474 }
475 return file;
476 }
477
478 }