Skip to content

Commit ea12a9d

Browse files
Grzegorz Kochańskislawekjaranowski
authored andcommitted
Allow defining complex expression like in interactive mode
1 parent 6071f4f commit ea12a9d

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public class EvaluateMojo extends AbstractHelpMojo {
113113
private String artifact;
114114

115115
/**
116-
* An expression to evaluate instead of prompting. Note that this <i>must not</i> include the surrounding ${...}.
116+
* An expression to evaluate instead of prompting. Note that this <i>may not</i> include the surrounding ${...}.
117117
*/
118118
@Parameter(property = "expression")
119119
private String expression;
@@ -200,7 +200,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
200200
}
201201
}
202202
} else {
203-
handleResponse("${" + expression + "}", output);
203+
if (!expression.contains("${")) {
204+
expression = "${" + expression + "}";
205+
}
206+
handleResponse(expression, output);
204207
}
205208
}
206209

src/test/java/org/apache/maven/plugins/help/EvaluateMojoTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,40 @@ void testEvaluateWithoutExpressionWithOutput(EvaluateMojo mojo) throws Exception
114114
verify(inputHandler, times(2)).readLine();
115115
}
116116

117+
/**
118+
* Tests evaluation of a complex expression.
119+
*
120+
* @throws Exception in case of errors.
121+
*/
122+
@Test
123+
@ResourceLock(Resources.SYSTEM_OUT)
124+
@InjectMojo(goal = "evaluate")
125+
@MojoParameter(name = "forceStdout", value = "true")
126+
@MojoParameter(name = "expression", value = "project_groupId=${project.groupId}")
127+
void testEvaluateForComplexExpression(EvaluateMojo mojo) throws Exception {
128+
when(expressionEvaluator.evaluate(anyString())).thenReturn("project_groupId=org.apache.maven.its.help");
129+
130+
// Quiet mode given on command line.(simulation)
131+
when(log.isInfoEnabled()).thenReturn(false);
132+
133+
setVariableValueToObject(mojo, "evaluator", expressionEvaluator);
134+
135+
PrintStream saveOut = System.out;
136+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
137+
System.setOut(new PrintStream(baos));
138+
139+
try {
140+
mojo.execute();
141+
} finally {
142+
System.setOut(saveOut);
143+
baos.close();
144+
}
145+
146+
String stdResult = baos.toString();
147+
assertEquals("project_groupId=org.apache.maven.its.help", stdResult);
148+
verify(log, times(0)).warn(anyString());
149+
}
150+
117151
/**
118152
* This test will check that only the <code>project.groupId</code> is printed to
119153
* stdout nothing else.

0 commit comments

Comments
 (0)