1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin;
20
21 import java.io.File;
22 import java.util.Properties;
23 import org.apache.maven.execution.MavenSession;
24 import org.apache.maven.plugin.descriptor.MojoDescriptor;
25 import org.apache.maven.plugin.descriptor.PluginDescriptor;
26 import org.apache.maven.project.MavenProject;
27 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
28 import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
29 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public class PluginParameterExpressionEvaluator implements TypeAwareExpressionEvaluator {
73 private MavenSession session;
74
75 private MojoExecution mojoExecution;
76
77 private MavenProject project;
78
79 private String basedir;
80
81 private Properties properties;
82
83 public PluginParameterExpressionEvaluator(MavenSession session) {
84 this(session, null);
85 }
86
87 public PluginParameterExpressionEvaluator(MavenSession session, MojoExecution mojoExecution) {
88 this.session = session;
89 this.mojoExecution = mojoExecution;
90 this.properties = new Properties();
91 this.project = session.getCurrentProject();
92
93
94
95
96
97 this.properties.putAll(session.getUserProperties());
98 this.properties.putAll(session.getSystemProperties());
99
100 String basedir = null;
101
102 if (project != null) {
103 File projectFile = project.getBasedir();
104
105
106 if (projectFile != null) {
107 basedir = projectFile.getAbsolutePath();
108 }
109 }
110
111 if (basedir == null) {
112 basedir = session.getExecutionRootDirectory();
113 }
114
115 if (basedir == null) {
116 basedir = System.getProperty("user.dir");
117 }
118
119 this.basedir = basedir;
120 }
121
122 @Override
123 public Object evaluate(String expr) throws ExpressionEvaluationException {
124 return evaluate(expr, null);
125 }
126
127 @Override
128 @SuppressWarnings("checkstyle:methodlength")
129 public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
130 Object value = null;
131
132 if (expr == null) {
133 return null;
134 }
135
136 String expression = stripTokens(expr);
137 if (expression.equals(expr)) {
138 int index = expr.indexOf("${");
139 if (index >= 0) {
140 int lastIndex = expr.indexOf('}', index);
141 if (lastIndex >= 0) {
142 String retVal = expr.substring(0, index);
143
144 if ((index > 0) && (expr.charAt(index - 1) == '$')) {
145 retVal += expr.substring(index + 1, lastIndex + 1);
146 } else {
147 Object subResult = evaluate(expr.substring(index, lastIndex + 1));
148
149 if (subResult != null) {
150 retVal += subResult;
151 } else {
152 retVal += "$" + expr.substring(index + 1, lastIndex + 1);
153 }
154 }
155
156 retVal += evaluate(expr.substring(lastIndex + 1));
157 return retVal;
158 }
159 }
160
161
162 if (expression.contains("$$")) {
163 return expression.replaceAll("\\$\\$", "\\$");
164 } else {
165 return expression;
166 }
167 }
168
169 MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
170
171 if ("localRepository".equals(expression)) {
172 value = session.getLocalRepository();
173 } else if ("session".equals(expression)) {
174 value = session;
175 } else if (expression.startsWith("session")) {
176 try {
177 int pathSeparator = expression.indexOf('/');
178
179 if (pathSeparator > 0) {
180 String pathExpression = expression.substring(1, pathSeparator);
181 value = ReflectionValueExtractor.evaluate(pathExpression, session);
182 value = value + expression.substring(pathSeparator);
183 } else {
184 value = ReflectionValueExtractor.evaluate(expression.substring(1), session);
185 }
186 } catch (Exception e) {
187
188 throw new ExpressionEvaluationException(
189 "Error evaluating plugin parameter expression: " + expression, e);
190 }
191 } else if ("reactorProjects".equals(expression)) {
192 value = session.getProjects();
193 } else if ("project".equals(expression)) {
194 value = project;
195 } else if ("executedProject".equals(expression)) {
196 value = project.getExecutionProject();
197 } else if (expression.startsWith("project") || expression.startsWith("pom")) {
198 try {
199 int pathSeparator = expression.indexOf('/');
200
201 if (pathSeparator > 0) {
202 String pathExpression = expression.substring(0, pathSeparator);
203 value = ReflectionValueExtractor.evaluate(pathExpression, project);
204 value = value + expression.substring(pathSeparator);
205 } else {
206 value = ReflectionValueExtractor.evaluate(expression.substring(1), project);
207 }
208 } catch (Exception e) {
209
210 throw new ExpressionEvaluationException(
211 "Error evaluating plugin parameter expression: " + expression, e);
212 }
213 } else if (expression.equals("repositorySystemSession")) {
214 value = session.getRepositorySession();
215 } else if (expression.equals("mojo") || expression.equals("mojoExecution")) {
216 value = mojoExecution;
217 } else if (expression.startsWith("mojo")) {
218 try {
219 int pathSeparator = expression.indexOf('/');
220
221 if (pathSeparator > 0) {
222 String pathExpression = expression.substring(1, pathSeparator);
223 value = ReflectionValueExtractor.evaluate(pathExpression, mojoExecution);
224 value = value + expression.substring(pathSeparator);
225 } else {
226 value = ReflectionValueExtractor.evaluate(expression.substring(1), mojoExecution);
227 }
228 } catch (Exception e) {
229
230 throw new ExpressionEvaluationException(
231 "Error evaluating plugin parameter expression: " + expression, e);
232 }
233 } else if (expression.equals("plugin")) {
234 value = mojoDescriptor.getPluginDescriptor();
235 } else if (expression.startsWith("plugin")) {
236 try {
237 int pathSeparator = expression.indexOf('/');
238
239 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
240
241 if (pathSeparator > 0) {
242 String pathExpression = expression.substring(1, pathSeparator);
243 value = ReflectionValueExtractor.evaluate(pathExpression, pluginDescriptor);
244 value = value + expression.substring(pathSeparator);
245 } else {
246 value = ReflectionValueExtractor.evaluate(expression.substring(1), pluginDescriptor);
247 }
248 } catch (Exception e) {
249 throw new ExpressionEvaluationException(
250 "Error evaluating plugin parameter expression: " + expression, e);
251 }
252 } else if ("settings".equals(expression)) {
253 value = session.getSettings();
254 } else if (expression.startsWith("settings")) {
255 try {
256 int pathSeparator = expression.indexOf('/');
257
258 if (pathSeparator > 0) {
259 String pathExpression = expression.substring(1, pathSeparator);
260 value = ReflectionValueExtractor.evaluate(pathExpression, session.getSettings());
261 value = value + expression.substring(pathSeparator);
262 } else {
263 value = ReflectionValueExtractor.evaluate(expression.substring(1), session.getSettings());
264 }
265 } catch (Exception e) {
266
267 throw new ExpressionEvaluationException(
268 "Error evaluating plugin parameter expression: " + expression, e);
269 }
270 } else if ("basedir".equals(expression)) {
271 value = basedir;
272 } else if (expression.startsWith("basedir")) {
273 int pathSeparator = expression.indexOf('/');
274
275 if (pathSeparator > 0) {
276 value = basedir + expression.substring(pathSeparator);
277 }
278 }
279
280
281
282
283
284
285
286
287
288 if (value != null && type != null && !(value instanceof String) && !isTypeCompatible(type, value)) {
289 value = null;
290 }
291
292 if (value == null) {
293
294
295 if (properties != null) {
296
297
298
299
300
301 value = properties.getProperty(expression);
302 }
303
304 if ((value == null) && ((project != null) && (project.getProperties() != null))) {
305 value = project.getProperties().getProperty(expression);
306 }
307 }
308
309 if (value instanceof String) {
310
311
312 String val = (String) value;
313
314 int exprStartDelimiter = val.indexOf("${");
315
316 if (exprStartDelimiter >= 0) {
317 if (exprStartDelimiter > 0) {
318 value = val.substring(0, exprStartDelimiter) + evaluate(val.substring(exprStartDelimiter));
319 } else {
320 value = evaluate(val.substring(exprStartDelimiter));
321 }
322 }
323 }
324
325 return value;
326 }
327
328 private static boolean isTypeCompatible(Class<?> type, Object value) {
329 if (type.isInstance(value)) {
330 return true;
331 }
332
333 return ((type.isPrimitive() || type.getName().startsWith("java.lang."))
334 && value.getClass().getName().startsWith("java.lang."));
335 }
336
337 private String stripTokens(String expr) {
338 if (expr.startsWith("${") && (expr.indexOf('}') == expr.length() - 1)) {
339 expr = expr.substring(2, expr.length() - 1);
340 }
341 return expr;
342 }
343
344 @Override
345 public File alignToBaseDirectory(File file) {
346
347
348 if (file != null) {
349 if (file.isAbsolute()) {
350
351 } else if (file.getPath().startsWith(File.separator)) {
352
353 file = file.getAbsoluteFile();
354 } else {
355
356 file = new File(new File(basedir, file.getPath()).toURI().normalize()).getAbsoluteFile();
357 }
358 }
359 return file;
360 }
361 }