View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin;
20  
21  import java.io.File;
22  import java.util.Properties;
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.plugin.descriptor.MojoDescriptor;
25  import org.apache.maven.plugin.descriptor.PluginDescriptor;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
28  import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
29  import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
30  
31  /**
32   * Evaluator for plugin parameters expressions. Content surrounded by <code>${</code> and <code>}</code> is evaluated.
33   * Recognized values are:
34   * <table border="1">
35   * <caption>Expression matrix</caption>
36   * <tr><th>expression</th>                     <th></th>               <th>evaluation result</th></tr>
37   * <tr><td><code>session</code></td>           <td></td>               <td>the actual {@link MavenSession}</td></tr>
38   * <tr><td><code>session.*</code></td>         <td>(since Maven 3)</td><td></td></tr>
39   * <tr><td><code>localRepository</code></td>   <td></td>
40   *                                             <td>{@link MavenSession#getLocalRepository()}</td></tr>
41   * <tr><td><code>reactorProjects</code></td>   <td></td>               <td>{@link MavenSession#getProjects()}</td></tr>
42   * <tr><td><code>repositorySystemSession</code></td><td> (since Maven 3)</td>
43   *                                             <td>{@link MavenSession#getRepositorySession()}</td></tr>
44   * <tr><td><code>project</code></td>           <td></td>
45   *                                             <td>{@link MavenSession#getCurrentProject()}</td></tr>
46   * <tr><td><code>project.*</code></td>         <td></td>               <td></td></tr>
47   * <tr><td><code>pom.*</code></td>             <td>(since Maven 3)</td><td>same as <code>project.*</code></td></tr>
48   * <tr><td><code>executedProject</code></td>   <td></td>
49   *                                             <td>{@link MavenProject#getExecutionProject()}</td></tr>
50   * <tr><td><code>settings</code></td>          <td></td>               <td>{@link MavenSession#getSettings()}</td></tr>
51   * <tr><td><code>settings.*</code></td>        <td></td>               <td></td></tr>
52   * <tr><td><code>basedir</code></td>           <td></td>
53   *                                             <td>{@link MavenSession#getExecutionRootDirectory()} or
54   *                                                 <code>System.getProperty( "user.dir" )</code> if null</td></tr>
55   * <tr><td><code>mojoExecution</code></td>     <td></td>               <td>the actual {@link MojoExecution}</td></tr>
56   * <tr><td><code>mojo</code></td>              <td>(since Maven 3)</td><td>same as <code>mojoExecution</code></td></tr>
57   * <tr><td><code>mojo.*</code></td>            <td>(since Maven 3)</td><td></td></tr>
58   * <tr><td><code>plugin</code></td>            <td>(since Maven 3)</td>
59   *                             <td>{@link MojoExecution#getMojoDescriptor()}.{@link MojoDescriptor#getPluginDescriptor()
60   *                                 getPluginDescriptor()}</td></tr>
61   * <tr><td><code>plugin.*</code></td>          <td></td>               <td></td></tr>
62   * <tr><td><code>*</code></td>                 <td></td>               <td>user properties</td></tr>
63   * <tr><td><code>*</code></td>                 <td></td>               <td>project properties</td></tr>
64   * <tr><td><code>*</code></td>                 <td></td>               <td>system properties</td></tr>
65   * </table>
66   * <i>Notice:</i> <code>reports</code> was supported in Maven 2.x but was removed in Maven 3
67   *
68   * @author Jason van Zyl
69   * @see MavenSession
70   * @see MojoExecution
71   */
72  public class PluginParameterExpressionEvaluator implements TypeAwareExpressionEvaluator {
73      private MavenSession session;
74  
75      private MojoExecution mojoExecution;
76  
77      private MavenProject project;
78  
79      private String basedir;
80  
81      private Properties properties;
82  
83      public PluginParameterExpressionEvaluator(MavenSession session) {
84          this(session, null);
85      }
86  
87      public PluginParameterExpressionEvaluator(MavenSession session, MojoExecution mojoExecution) {
88          this.session = session;
89          this.mojoExecution = mojoExecution;
90          this.properties = new Properties();
91          this.project = session.getCurrentProject();
92  
93          //
94          // Maven4: We may want to evaluate how this is used but we add these separate as the
95          // getExecutionProperties is deprecated in MavenSession.
96          //
97          this.properties.putAll(session.getUserProperties());
98          this.properties.putAll(session.getSystemProperties());
99  
100         String basedir = null;
101 
102         if (project != null) {
103             File projectFile = project.getBasedir();
104 
105             // this should always be the case for non-super POM instances...
106             if (projectFile != null) {
107                 basedir = projectFile.getAbsolutePath();
108             }
109         }
110 
111         if (basedir == null) {
112             basedir = session.getExecutionRootDirectory();
113         }
114 
115         if (basedir == null) {
116             basedir = System.getProperty("user.dir");
117         }
118 
119         this.basedir = basedir;
120     }
121 
122     @Override
123     public Object evaluate(String expr) throws ExpressionEvaluationException {
124         return evaluate(expr, null);
125     }
126 
127     @Override
128     @SuppressWarnings("checkstyle:methodlength")
129     public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
130         Object value = null;
131 
132         if (expr == null) {
133             return null;
134         }
135 
136         String expression = stripTokens(expr);
137         if (expression.equals(expr)) {
138             int index = expr.indexOf("${");
139             if (index >= 0) {
140                 int lastIndex = expr.indexOf('}', index);
141                 if (lastIndex >= 0) {
142                     String retVal = expr.substring(0, index);
143 
144                     if ((index > 0) && (expr.charAt(index - 1) == '$')) {
145                         retVal += expr.substring(index + 1, lastIndex + 1);
146                     } else {
147                         Object subResult = evaluate(expr.substring(index, lastIndex + 1));
148 
149                         if (subResult != null) {
150                             retVal += subResult;
151                         } else {
152                             retVal += "$" + expr.substring(index + 1, lastIndex + 1);
153                         }
154                     }
155 
156                     retVal += evaluate(expr.substring(lastIndex + 1));
157                     return retVal;
158                 }
159             }
160 
161             // Was not an expression
162             if (expression.contains("$$")) {
163                 return expression.replaceAll("\\$\\$", "\\$");
164             } else {
165                 return expression;
166             }
167         }
168 
169         MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
170 
171         if ("localRepository".equals(expression)) {
172             value = session.getLocalRepository();
173         } else if ("session".equals(expression)) {
174             value = session;
175         } else if (expression.startsWith("session")) {
176             try {
177                 int pathSeparator = expression.indexOf('/');
178 
179                 if (pathSeparator > 0) {
180                     String pathExpression = expression.substring(1, pathSeparator);
181                     value = ReflectionValueExtractor.evaluate(pathExpression, session);
182                     value = value + expression.substring(pathSeparator);
183                 } else {
184                     value = ReflectionValueExtractor.evaluate(expression.substring(1), session);
185                 }
186             } catch (Exception e) {
187                 // TODO don't catch exception
188                 throw new ExpressionEvaluationException(
189                         "Error evaluating plugin parameter expression: " + expression, e);
190             }
191         } else if ("reactorProjects".equals(expression)) {
192             value = session.getProjects();
193         } else if ("project".equals(expression)) {
194             value = project;
195         } else if ("executedProject".equals(expression)) {
196             value = project.getExecutionProject();
197         } else if (expression.startsWith("project") || expression.startsWith("pom")) {
198             try {
199                 int pathSeparator = expression.indexOf('/');
200 
201                 if (pathSeparator > 0) {
202                     String pathExpression = expression.substring(0, pathSeparator);
203                     value = ReflectionValueExtractor.evaluate(pathExpression, project);
204                     value = value + expression.substring(pathSeparator);
205                 } else {
206                     value = ReflectionValueExtractor.evaluate(expression.substring(1), project);
207                 }
208             } catch (Exception e) {
209                 // TODO don't catch exception
210                 throw new ExpressionEvaluationException(
211                         "Error evaluating plugin parameter expression: " + expression, e);
212             }
213         } else if (expression.equals("repositorySystemSession")) {
214             value = session.getRepositorySession();
215         } else if (expression.equals("mojo") || expression.equals("mojoExecution")) {
216             value = mojoExecution;
217         } else if (expression.startsWith("mojo")) {
218             try {
219                 int pathSeparator = expression.indexOf('/');
220 
221                 if (pathSeparator > 0) {
222                     String pathExpression = expression.substring(1, pathSeparator);
223                     value = ReflectionValueExtractor.evaluate(pathExpression, mojoExecution);
224                     value = value + expression.substring(pathSeparator);
225                 } else {
226                     value = ReflectionValueExtractor.evaluate(expression.substring(1), mojoExecution);
227                 }
228             } catch (Exception e) {
229                 // TODO don't catch exception
230                 throw new ExpressionEvaluationException(
231                         "Error evaluating plugin parameter expression: " + expression, e);
232             }
233         } else if (expression.equals("plugin")) {
234             value = mojoDescriptor.getPluginDescriptor();
235         } else if (expression.startsWith("plugin")) {
236             try {
237                 int pathSeparator = expression.indexOf('/');
238 
239                 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
240 
241                 if (pathSeparator > 0) {
242                     String pathExpression = expression.substring(1, pathSeparator);
243                     value = ReflectionValueExtractor.evaluate(pathExpression, pluginDescriptor);
244                     value = value + expression.substring(pathSeparator);
245                 } else {
246                     value = ReflectionValueExtractor.evaluate(expression.substring(1), pluginDescriptor);
247                 }
248             } catch (Exception e) {
249                 throw new ExpressionEvaluationException(
250                         "Error evaluating plugin parameter expression: " + expression, e);
251             }
252         } else if ("settings".equals(expression)) {
253             value = session.getSettings();
254         } else if (expression.startsWith("settings")) {
255             try {
256                 int pathSeparator = expression.indexOf('/');
257 
258                 if (pathSeparator > 0) {
259                     String pathExpression = expression.substring(1, pathSeparator);
260                     value = ReflectionValueExtractor.evaluate(pathExpression, session.getSettings());
261                     value = value + expression.substring(pathSeparator);
262                 } else {
263                     value = ReflectionValueExtractor.evaluate(expression.substring(1), session.getSettings());
264                 }
265             } catch (Exception e) {
266                 // TODO don't catch exception
267                 throw new ExpressionEvaluationException(
268                         "Error evaluating plugin parameter expression: " + expression, e);
269             }
270         } else if ("basedir".equals(expression)) {
271             value = basedir;
272         } else if (expression.startsWith("basedir")) {
273             int pathSeparator = expression.indexOf('/');
274 
275             if (pathSeparator > 0) {
276                 value = basedir + expression.substring(pathSeparator);
277             }
278         }
279 
280         /*
281          * MNG-4312: We neither have reserved all of the above magic expressions nor is their set fixed/well-known (it
282          * gets occasionally extended by newer Maven versions). This imposes the risk for existing plugins to
283          * unintentionally use such a magic expression for an ordinary property. So here we check whether we
284          * ended up with a magic value that is not compatible with the type of the configured mojo parameter (a string
285          * could still be converted by the configurator so we leave those alone). If so, back off to evaluating the
286          * expression from properties only.
287          */
288         if (value != null && type != null && !(value instanceof String) && !isTypeCompatible(type, value)) {
289             value = null;
290         }
291 
292         if (value == null) {
293             // The CLI should win for defining properties
294 
295             if (properties != null) {
296                 // We will attempt to get nab a property as a way to specify a parameter
297                 // to a plugin. My particular case here is allowing the surefire plugin
298                 // to run a single test so I want to specify that class on the cli as
299                 // a parameter.
300 
301                 value = properties.getProperty(expression);
302             }
303 
304             if ((value == null) && ((project != null) && (project.getProperties() != null))) {
305                 value = project.getProperties().getProperty(expression);
306             }
307         }
308 
309         if (value instanceof String) {
310             // TODO without #, this could just be an evaluate call...
311 
312             String val = (String) value;
313 
314             int exprStartDelimiter = val.indexOf("${");
315 
316             if (exprStartDelimiter >= 0) {
317                 if (exprStartDelimiter > 0) {
318                     value = val.substring(0, exprStartDelimiter) + evaluate(val.substring(exprStartDelimiter));
319                 } else {
320                     value = evaluate(val.substring(exprStartDelimiter));
321                 }
322             }
323         }
324 
325         return value;
326     }
327 
328     private static boolean isTypeCompatible(Class<?> type, Object value) {
329         if (type.isInstance(value)) {
330             return true;
331         }
332         // likely Boolean -> boolean, Short -> int etc. conversions, it's not the problem case we try to avoid
333         return ((type.isPrimitive() || type.getName().startsWith("java.lang."))
334                 && value.getClass().getName().startsWith("java.lang."));
335     }
336 
337     private String stripTokens(String expr) {
338         if (expr.startsWith("${") && (expr.indexOf('}') == expr.length() - 1)) {
339             expr = expr.substring(2, expr.length() - 1);
340         }
341         return expr;
342     }
343 
344     @Override
345     public File alignToBaseDirectory(File file) {
346         // TODO Copied from the DefaultInterpolator. We likely want to resurrect the PathTranslator or at least a
347         // similar component for re-usage
348         if (file != null) {
349             if (file.isAbsolute()) {
350                 // path was already absolute, just normalize file separator and we're done
351             } else if (file.getPath().startsWith(File.separator)) {
352                 // drive-relative Windows path, don't align with project directory but with drive root
353                 file = file.getAbsoluteFile();
354             } else {
355                 // an ordinary relative path, align with project directory
356                 file = new File(new File(basedir, file.getPath()).toURI().normalize()).getAbsoluteFile();
357             }
358         }
359         return file;
360     }
361 }