in impl/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java [130:327]
public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
Object value = null;
if (expr == null) {
return null;
}
String expression = stripTokens(expr);
if (expression.equals(expr)) {
int index = expr.indexOf("${");
if (index >= 0) {
int lastIndex = expr.indexOf('}', index);
if (lastIndex >= 0) {
String retVal = expr.substring(0, index);
if ((index > 0) && (expr.charAt(index - 1) == '$')) {
retVal += expr.substring(index + 1, lastIndex + 1);
} else {
Object subResult = evaluate(expr.substring(index, lastIndex + 1));
if (subResult != null) {
retVal += subResult;
} else {
retVal += "$" + expr.substring(index + 1, lastIndex + 1);
}
}
retVal += evaluate(expr.substring(lastIndex + 1));
return retVal;
}
}
// Was not an expression
return expression.replace("$$", "$");
}
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
if ("localRepository".equals(expression)) {
value = session.getLocalRepository();
} else if ("session".equals(expression)) {
value = session;
} else if (expression.startsWith("session")) {
try {
int pathSeparator = expression.indexOf('/');
if (pathSeparator > 0) {
String pathExpression = expression.substring(0, pathSeparator);
value = ReflectionValueExtractor.evaluate(pathExpression, session);
if (pathSeparator < expression.length() - 1) {
if (value instanceof Path path) {
value = path.resolve(expression.substring(pathSeparator + 1));
} else {
value = value + expression.substring(pathSeparator);
}
}
} else {
value = ReflectionValueExtractor.evaluate(expression, session);
}
} catch (Exception e) {
// TODO don't catch exception
throw new ExpressionEvaluationException(
"Error evaluating plugin parameter expression: " + expression, e);
}
} else if ("reactorProjects".equals(expression)) {
value = session.getProjects();
} else if ("project".equals(expression)) {
value = project;
} else if ("executedProject".equals(expression)) {
value = project.getExecutionProject();
} else if (expression.startsWith("project") || expression.startsWith("pom")) {
try {
int pathSeparator = expression.indexOf('/');
if (pathSeparator > 0) {
String pathExpression = expression.substring(0, pathSeparator);
value = ReflectionValueExtractor.evaluate(pathExpression, project);
value = value + expression.substring(pathSeparator);
} else {
value = ReflectionValueExtractor.evaluate(expression, project);
}
} catch (Exception e) {
// TODO don't catch exception
throw new ExpressionEvaluationException(
"Error evaluating plugin parameter expression: " + expression, e);
}
} else if (expression.equals("repositorySystemSession")) {
value = session.getRepositorySession();
} else if (expression.equals("mojo") || expression.equals("mojoExecution")) {
value = mojoExecution;
} else if (expression.startsWith("mojo")) {
try {
int pathSeparator = expression.indexOf('/');
if (pathSeparator > 0) {
String pathExpression = expression.substring(0, pathSeparator);
value = ReflectionValueExtractor.evaluate(pathExpression, mojoExecution);
value = value + expression.substring(pathSeparator);
} else {
value = ReflectionValueExtractor.evaluate(expression, mojoExecution);
}
} catch (Exception e) {
// TODO don't catch exception
throw new ExpressionEvaluationException(
"Error evaluating plugin parameter expression: " + expression, e);
}
} else if (expression.equals("plugin")) {
value = mojoDescriptor.getPluginDescriptor();
} else if (expression.startsWith("plugin")) {
try {
int pathSeparator = expression.indexOf('/');
PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
if (pathSeparator > 0) {
String pathExpression = expression.substring(0, pathSeparator);
value = ReflectionValueExtractor.evaluate(pathExpression, pluginDescriptor);
value = value + expression.substring(pathSeparator);
} else {
value = ReflectionValueExtractor.evaluate(expression, pluginDescriptor);
}
} catch (Exception e) {
throw new ExpressionEvaluationException(
"Error evaluating plugin parameter expression: " + expression, e);
}
} else if ("settings".equals(expression)) {
value = session.getSettings();
} else if (expression.startsWith("settings")) {
try {
int pathSeparator = expression.indexOf('/');
if (pathSeparator > 0) {
String pathExpression = expression.substring(0, pathSeparator);
value = ReflectionValueExtractor.evaluate(pathExpression, session.getSettings());
value = value + expression.substring(pathSeparator);
} else {
value = ReflectionValueExtractor.evaluate(expression, session.getSettings());
}
} catch (Exception e) {
// TODO don't catch exception
throw new ExpressionEvaluationException(
"Error evaluating plugin parameter expression: " + expression, e);
}
} else if ("basedir".equals(expression)) {
value = basedir;
} else if (expression.startsWith("basedir")) {
int pathSeparator = expression.indexOf('/');
if (pathSeparator > 0) {
value = basedir + expression.substring(pathSeparator);
}
}
/*
* MNG-4312: We neither have reserved all of the above magic expressions nor is their set fixed/well-known (it
* gets occasionally extended by newer Maven versions). This imposes the risk for existing plugins to
* unintentionally use such a magic expression for an ordinary property. So here we check whether we
* ended up with a magic value that is not compatible with the type of the configured mojo parameter (a string
* could still be converted by the configurator so we leave those alone). If so, back off to evaluating the
* expression from properties only.
*/
if (value != null && type != null && !(value instanceof String) && !isTypeCompatible(type, value)) {
value = null;
}
if (value == null) {
// The CLI should win for defining properties
if (properties != null) {
// We will attempt to get nab a property as a way to specify a parameter
// to a plugin. My particular case here is allowing the surefire plugin
// to run a single test so I want to specify that class on the cli as
// a parameter.
value = properties.getProperty(expression);
}
if ((value == null) && ((project != null) && (project.getProperties() != null))) {
value = project.getProperties().getProperty(expression);
}
}
if (value instanceof String val) {
// TODO without #, this could just be an evaluate call...
int exprStartDelimiter = val.indexOf("${");
if (exprStartDelimiter >= 0) {
if (exprStartDelimiter > 0) {
value = val.substring(0, exprStartDelimiter) + evaluate(val.substring(exprStartDelimiter));
} else {
value = evaluate(val.substring(exprStartDelimiter));
}
}
}
return value;
}