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