View Javadoc
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:<table border="1" summary="Expression matrix">
38   * <tr><th>expression</th>                     <th></th>               <th>evaluation result</th></tr>
39   * <tr><td><code>session</code></td>           <td></td>               <td>the actual {@link MavenSession}</td></tr>
40   * <tr><td><code>session.*</code></td>         <td>(since Maven 3)</td><td></td></tr>
41   * <tr><td><code>localRepository</code></td>   <td></td>
42   *                                             <td>{@link MavenSession#getLocalRepository()}</td></tr>
43   * <tr><td><code>reactorProjects</code></td>   <td></td>               <td>{@link MavenSession#getProjects()}</td></tr>
44   * <tr><td><code>repositorySystemSession</code></td><td> (since Maven 3)</td>
45   *                                             <td>{@link MavenSession#getRepositorySession()}</td></tr>
46   * <tr><td><code>project</code></td>           <td></td>
47   *                                             <td>{@link MavenSession#getCurrentProject()}</td></tr>
48   * <tr><td><code>project.*</code></td>         <td></td>               <td></td></tr>
49   * <tr><td><code>pom.*</code></td>             <td>(since Maven 3)</td><td>same as <code>project.*</code></td></tr>
50   * <tr><td><code>executedProject</code></td>   <td></td>
51   *                                             <td>{@link MavenProject#getExecutionProject()}</td></tr>
52   * <tr><td><code>settings</code></td>          <td></td>               <td>{@link MavenSession#getSettings()}</td></tr>
53   * <tr><td><code>settings.*</code></td>        <td></td>               <td></td></tr>
54   * <tr><td><code>basedir</code></td>           <td></td>
55   *                                             <td>{@link MavenSession#getExecutionRootDirectory()} or 
56   *                                                 <code>System.getProperty( "user.dir" )</code> if null</td></tr>
57   * <tr><td><code>mojoExecution</code></td>     <td></td>               <td>the actual {@link MojoExecution}</td></tr>
58   * <tr><td><code>mojo</code></td>              <td>(since Maven 3)</td><td>same as <code>mojoExecution</code></td></tr>
59   * <tr><td><code>mojo.*</code></td>            <td>(since Maven 3)</td><td></td></tr>
60   * <tr><td><code>plugin</code></td>            <td>(since Maven 3)</td>
61   *                             <td>{@link MojoExecution#getMojoDescriptor()}.{@link MojoDescriptor#getPluginDescriptor()
62   *                                 getPluginDescriptor()}</td></tr>
63   * <tr><td><code>plugin.*</code></td>          <td></td>               <td></td></tr>
64   * <tr><td><code>*</code></td>                 <td></td>               <td>system properties</td></tr>
65   * <tr><td><code>*</code></td>                 <td></td>               <td>project properties</td></tr>
66   * </table>
67   * <i>Notice:</i> <code>reports</code> was supported in Maven 2.x but was removed in Maven 3
68   *
69   * @author Jason van Zyl
70   * @see MavenSession
71   * @see MojoExecution
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 //TODO used by the Enforcer plugin
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         // Maven4: We may want to evaluate how this is used but we add these separate as the 
108         // getExecutionProperties is deprecated in MavenSession.
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             // this should always be the case for non-super POM instances...
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             // Was not an expression
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                 // TODO don't catch exception
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                 // TODO don't catch exception
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                 // TODO don't catch exception
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                 // TODO don't catch exception
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          * MNG-4312: We neither have reserved all of the above magic expressions nor is their set fixed/well-known (it
382          * gets occasionally extended by newer Maven versions). This imposes the risk for existing plugins to
383          * unintentionally use such a magic expression for an ordinary system property. So here we check whether we
384          * ended up with a magic value that is not compatible with the type of the configured mojo parameter (a string
385          * could still be converted by the configurator so we leave those alone). If so, back off to evaluating the
386          * expression from properties only.
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             // The CLI should win for defining properties
396 
397             if ( properties != null )
398             {
399                 // We will attempt to get nab a system property as a way to specify a
400                 // parameter to a plugins. My particular case here is allowing the surefire
401                 // plugin to run a single test so I want to specify that class on the cli
402                 // as a parameter.
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             // TODO without #, this could just be an evaluate call...
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         // likely Boolean -> boolean, Short -> int etc. conversions, it's not the problem case we try to avoid
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         // TODO Copied from the DefaultInterpolator. We likely want to resurrect the PathTranslator or at least a
462         // similar component for re-usage
463         if ( file != null )
464         {
465             if ( file.isAbsolute() )
466             {
467                 // path was already absolute, just normalize file separator and we're done
468             }
469             else if ( file.getPath().startsWith( File.separator ) )
470             {
471                 // drive-relative Windows path, don't align with project directory but with drive root
472                 file = file.getAbsoluteFile();
473             }
474             else
475             {
476                 // an ordinary relative path, align with project directory
477                 file = new File( new File( basedir, file.getPath() ).toURI().normalize() ).getAbsoluteFile();
478             }
479         }
480         return file;
481     }
482 
483 }