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.HashMap;
25  import java.util.Map;
26  import java.util.Optional;
27  import java.util.Properties;
28  
29  import org.apache.maven.api.MojoExecution;
30  import org.apache.maven.api.Project;
31  import org.apache.maven.api.Session;
32  import org.apache.maven.model.interpolation.reflection.ReflectionValueExtractor;
33  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
34  import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
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></td></tr>
43   * <tr><td><code>project.*</code></td>         <td></td>               <td></td></tr>
44   * <tr><td><code>settings.*</code></td>        <td></td>               <td></td></tr>
45   * <tr><td><code>mojo.*</code></td>            <td></td>               <td>the actual {@link MojoExecution}</td></tr>
46   * <tr><td><code>*</code></td>                 <td></td>               <td>user properties</td></tr>
47   * <tr><td><code>*</code></td>                 <td></td>               <td>system properties</td></tr>
48   * <tr><td><code>*</code></td>                 <td></td>               <td>project properties</td></tr>
49   * </table>
50   *
51   * @see Session
52   * @see Project
53   * @see org.apache.maven.api.settings.Settings
54   * @see MojoExecution
55   */
56  public class PluginParameterExpressionEvaluatorV4 implements TypeAwareExpressionEvaluator {
57      private Session session;
58  
59      private MojoExecution mojoExecution;
60  
61      private Project project;
62  
63      private Path basedir;
64  
65      private Properties properties;
66  
67      public PluginParameterExpressionEvaluatorV4(Session session, Project project) {
68          this(session, project, null);
69      }
70  
71      public PluginParameterExpressionEvaluatorV4(Session session, Project project, MojoExecution mojoExecution) {
72          this.session = session;
73          this.mojoExecution = mojoExecution;
74          this.properties = new Properties();
75          this.project = project;
76  
77          //
78          // Maven4: We may want to evaluate how this is used but we add these separate as the
79          // getExecutionProperties is deprecated in MavenSession.
80          //
81          this.properties.putAll(session.getUserProperties());
82          this.properties.putAll(session.getSystemProperties());
83  
84          Path basedir = null;
85  
86          if (project != null) {
87              Optional<Path> projectFile = project.getBasedir();
88  
89              // this should always be the case for non-super POM instances...
90              if (projectFile.isPresent()) {
91                  basedir = projectFile.get().toAbsolutePath();
92              }
93          }
94  
95          if (basedir == null) {
96              basedir = session.getTopDirectory();
97          }
98  
99          if (basedir == null) {
100             basedir = Paths.get(System.getProperty("user.dir"));
101         }
102 
103         this.basedir = basedir;
104     }
105 
106     @Override
107     public Object evaluate(String expr) throws ExpressionEvaluationException {
108         return evaluate(expr, null);
109     }
110 
111     @Override
112     @SuppressWarnings("checkstyle:methodlength")
113     public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
114         Object value = null;
115 
116         if (expr == null) {
117             return null;
118         }
119 
120         String expression = stripTokens(expr);
121         if (expression.equals(expr)) {
122             int index = expr.indexOf("${");
123             if (index >= 0) {
124                 int lastIndex = expr.indexOf('}', index);
125                 if (lastIndex >= 0) {
126                     String retVal = expr.substring(0, index);
127 
128                     if ((index > 0) && (expr.charAt(index - 1) == '$')) {
129                         retVal += expr.substring(index + 1, lastIndex + 1);
130                     } else {
131                         Object subResult = evaluate(expr.substring(index, lastIndex + 1));
132 
133                         if (subResult != null) {
134                             retVal += subResult;
135                         } else {
136                             retVal += "$" + expr.substring(index + 1, lastIndex + 1);
137                         }
138                     }
139 
140                     retVal += evaluate(expr.substring(lastIndex + 1));
141                     return retVal;
142                 }
143             }
144 
145             // Was not an expression
146             return expression.replace("$$", "$");
147         }
148 
149         Map<String, Object> objects = new HashMap<>();
150         objects.put("session.", session);
151         objects.put("project.", project);
152         objects.put("mojo.", mojoExecution);
153         objects.put("settings.", session.getSettings());
154         for (Map.Entry<String, Object> ctx : objects.entrySet()) {
155             if (expression.startsWith(ctx.getKey())) {
156                 try {
157                     int pathSeparator = expression.indexOf('/');
158                     if (pathSeparator > 0) {
159                         String pathExpression = expression.substring(0, pathSeparator);
160                         value = ReflectionValueExtractor.evaluate(pathExpression, ctx.getValue());
161                         if (pathSeparator < expression.length() - 1) {
162                             if (value instanceof Path) {
163                                 value = ((Path) value).resolve(expression.substring(pathSeparator + 1));
164                             } else {
165                                 value = value + expression.substring(pathSeparator);
166                             }
167                         }
168                     } else {
169                         value = ReflectionValueExtractor.evaluate(expression, ctx.getValue());
170                     }
171                     break;
172                 } catch (Exception e) {
173                     // TODO don't catch exception
174                     throw new ExpressionEvaluationException(
175                             "Error evaluating plugin parameter expression: " + expression, e);
176                 }
177             }
178         }
179 
180         /*
181          * MNG-4312: We neither have reserved all of the above magic expressions nor is their set fixed/well-known (it
182          * gets occasionally extended by newer Maven versions). This imposes the risk for existing plugins to
183          * unintentionally use such a magic expression for an ordinary property. So here we check whether we
184          * ended up with a magic value that is not compatible with the type of the configured mojo parameter (a string
185          * could still be converted by the configurator so we leave those alone). If so, back off to evaluating the
186          * expression from properties only.
187          */
188         if (value != null && type != null && !(value instanceof String) && !isTypeCompatible(type, value)) {
189             value = null;
190         }
191 
192         if (value == null) {
193             // The CLI should win for defining properties
194 
195             if (properties != null) {
196                 // We will attempt to get nab a property as a way to specify a parameter
197                 // to a plugin. My particular case here is allowing the surefire plugin
198                 // to run a single test so I want to specify that class on the cli as
199                 // a parameter.
200 
201                 value = properties.getProperty(expression);
202             }
203 
204             if ((value == null) && ((project != null) && (project.getModel().getProperties() != null))) {
205                 value = project.getModel().getProperties().get(expression);
206             }
207         }
208 
209         if (value instanceof String) {
210             // TODO without #, this could just be an evaluate call...
211 
212             String val = (String) value;
213 
214             int exprStartDelimiter = val.indexOf("${");
215 
216             if (exprStartDelimiter >= 0) {
217                 if (exprStartDelimiter > 0) {
218                     value = val.substring(0, exprStartDelimiter) + evaluate(val.substring(exprStartDelimiter));
219                 } else {
220                     value = evaluate(val.substring(exprStartDelimiter));
221                 }
222             }
223         }
224 
225         return value;
226     }
227 
228     private static boolean isTypeCompatible(Class<?> type, Object value) {
229         if (type.isInstance(value)) {
230             return true;
231         }
232         // likely Boolean -> boolean, Short -> int etc. conversions, it's not the problem case we try to avoid
233         return ((type.isPrimitive() || type.getName().startsWith("java.lang."))
234                 && value.getClass().getName().startsWith("java.lang."));
235     }
236 
237     private String stripTokens(String expr) {
238         if (expr.startsWith("${") && (expr.indexOf('}') == expr.length() - 1)) {
239             expr = expr.substring(2, expr.length() - 1);
240         }
241         return expr;
242     }
243 
244     @Override
245     public File alignToBaseDirectory(File file) {
246         // TODO Copied from the DefaultInterpolator. We likely want to resurrect the PathTranslator or at least a
247         // similar component for re-usage
248         if (file != null) {
249             if (file.isAbsolute()) {
250                 // path was already absolute, just normalize file separator and we're done
251             } else if (file.getPath().startsWith(File.separator)) {
252                 // drive-relative Windows path, don't align with project directory but with drive root
253                 file = file.getAbsoluteFile();
254             } else {
255                 // an ordinary relative path, align with project directory
256                 file = basedir.resolve(file.getPath())
257                         .normalize()
258                         .toAbsolutePath()
259                         .toFile();
260             }
261         }
262         return file;
263     }
264 }