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