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.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   * Evaluator for plugin parameters expressions. Content surrounded by <code>${</code> and <code>}</code> is evaluated.
40   * Recognized values are:
41   * <table border="1">
42   * <caption>Expression matrix</caption>
43   * <tr><th>expression</th>                     <th></th>               <th>evaluation result</th></tr>
44   * <tr><td><code>session</code></td>           <td></td>               <td>the actual {@link Session}</td></tr>
45   * <tr><td><code>session.*</code></td>         <td>(since Maven 3)</td><td></td></tr>
46   * <tr><td><code>localRepository</code></td>   <td></td>
47   *                                             <td>{@link Session#getLocalRepository()}</td></tr>
48   * <tr><td><code>reactorProjects</code></td>   <td></td>               <td>{@link Session#getProjects()}</td></tr>
49   * <tr><td><code>project</code></td>           <td></td>
50   *                                 <td>{@link org.apache.maven.execution.MavenSession#getCurrentProject()}</td></tr>
51   * <tr><td><code>project.*</code></td>         <td></td>               <td></td></tr>
52   * <tr><td><code>pom.*</code></td>             <td>(since Maven 3)</td><td>same as <code>project.*</code></td></tr>
53   * <tr><td><code>executedProject</code></td>   <td></td>
54   *                                 <td>{@link org.apache.maven.project.MavenProject#getExecutionProject()}</td></tr>
55   * <tr><td><code>settings</code></td>          <td></td>               <td>{@link Session#getSettings()}</td></tr>
56   * <tr><td><code>settings.*</code></td>        <td></td>               <td></td></tr>
57   * <tr><td><code>basedir</code></td>           <td></td>
58   *                                 <td>{@link Session#getExecutionRootDirectory()} or
59   *                                                 <code>System.getProperty( "user.dir" )</code> if null</td></tr>
60   * <tr><td><code>mojoExecution</code></td>     <td></td>               <td>the actual {@link MojoExecution}</td></tr>
61   * <tr><td><code>mojo</code></td>              <td>(since Maven 3)</td><td>same as <code>mojoExecution</code></td></tr>
62   * <tr><td><code>mojo.*</code></td>            <td>(since Maven 3)</td><td></td></tr>
63   * <tr><td><code>plugin</code></td>            <td>(since Maven 3)</td>
64   *                             <td>{@link MojoExecution#getMojoDescriptor()}.{@link MojoDescriptor#getPluginDescriptor()
65   *                                 getPluginDescriptor()}</td></tr>
66   * <tr><td><code>plugin.*</code></td>          <td></td>               <td></td></tr>
67   * <tr><td><code>*</code></td>                 <td></td>               <td>system properties</td></tr>
68   * <tr><td><code>*</code></td>                 <td></td>               <td>project properties</td></tr>
69   * </table>
70   * <i>Notice:</i> <code>reports</code> was supported in Maven 2.x but was removed in Maven 3
71   *
72   * @author Jason van Zyl
73   * @see Session
74   * @see MojoExecution
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         // Maven4: We may want to evaluate how this is used but we add these separate as the
103         // getExecutionProperties is deprecated in MavenSession.
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             // this should always be the case for non-super POM instances...
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             // Was not an expression
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             // TODO: v4
201             value = session.getLocalRepository();
202         }
203         else if ( "session".equals( expression ) )
204         {
205             value = session;
206         }
207         else if ( expression.startsWith( "session" ) )
208         {
209             // TODO: v4
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 ( "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             // TODO: v4
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                 // TODO don't catch exception
266                 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
267                                                          e );
268             }
269         }
270         else if ( expression.equals( "repositorySystemSession" ) )
271         {
272             // TODO: v4
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             // TODO: v4
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                 // TODO don't catch exception
299                 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
300                                                          e );
301             }
302         }
303         else if ( expression.equals( "plugin" ) )
304         {
305             // TODO: v4
306             value = mojoExecution.getMojoDescriptor().getPluginDescriptor();
307         }
308         else if ( expression.startsWith( "plugin" ) )
309         {
310             // TODO: v4
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                 // TODO don't catch exception
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          * MNG-4312: We neither have reserved all of the above magic expressions nor is their set fixed/well-known (it
378          * gets occasionally extended by newer Maven versions). This imposes the risk for existing plugins to
379          * unintentionally use such a magic expression for an ordinary system property. So here we check whether we
380          * ended up with a magic value that is not compatible with the type of the configured mojo parameter (a string
381          * could still be converted by the configurator so we leave those alone). If so, back off to evaluating the
382          * expression from properties only.
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             // The CLI should win for defining properties
392 
393             if ( properties != null )
394             {
395                 // We will attempt to get nab a system property as a way to specify a
396                 // parameter to a plugins. My particular case here is allowing the surefire
397                 // plugin to run a single test so I want to specify that class on the cli
398                 // as a parameter.
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             // TODO without #, this could just be an evaluate call...
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         // likely Boolean -> boolean, Short -> int etc. conversions, it's not the problem case we try to avoid
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         // TODO Copied from the DefaultInterpolator. We likely want to resurrect the PathTranslator or at least a
458         // similar component for re-usage
459         if ( file != null )
460         {
461             if ( file.isAbsolute() )
462             {
463                 // path was already absolute, just normalize file separator and we're done
464             }
465             else if ( file.getPath().startsWith( File.separator ) )
466             {
467                 // drive-relative Windows path, don't align with project directory but with drive root
468                 file = file.getAbsoluteFile();
469             }
470             else
471             {
472                 // an ordinary relative path, align with project directory
473                 file = basedir.resolve( file.getPath() ).normalize().toAbsolutePath().toFile();
474             }
475         }
476         return file;
477     }
478 
479 }