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