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