Skip to content

Commit 0154e46

Browse files
Grzegorz KochańskiGrzegorz Kochański
authored andcommitted
Allow defining complex expression like in interactive mode
1 parent f797d78 commit 0154e46

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-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
@@ -112,7 +112,7 @@ public class EvaluateMojo extends AbstractHelpMojo {
112112
private String artifact;
113113

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

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ public void testEvaluateWithoutExpressionWithOutput() throws Exception {
109109
verify(inputHandler, times(2)).readLine();
110110
}
111111

112+
/**
113+
* Tests evaluation of a complex expression.
114+
* @throws Exception in case of errors.
115+
*/
116+
public void testEvaluateForComplexExpression() throws Exception {
117+
File testPom = new File(getBasedir(), "target/test-classes/unit/evaluate/plugin-config-complex.xml");
118+
119+
EvaluateMojo mojo = (EvaluateMojo) lookupMojo("evaluate", testPom);
120+
121+
ExpressionEvaluator expressionEvaluator = mock(PluginParameterExpressionEvaluator.class);
122+
when(expressionEvaluator.evaluate("project_version=${project.version}"))
123+
.thenReturn("project_version=1.0.0-SNAPSHOT");
124+
125+
setUpMojo(mojo, null, expressionEvaluator);
126+
127+
mojo.execute();
128+
129+
String ls = System.getProperty("line.separator");
130+
131+
assertTrue(interceptingLogger.infoLogs.contains(ls + "project_version=1.0.0-SNAPSHOT"));
132+
verify(expressionEvaluator).evaluate("project_version=${project.version}");
133+
}
134+
112135
/**
113136
* This test will check that only the <code>project.groupId</code> is printed to
114137
* stdout nothing else.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<groupId>org.apache.maven.its.help</groupId>
23+
<artifactId>evaluate</artifactId>
24+
<packaging>jar</packaging>
25+
<version>1.0-SNAPSHOT</version>
26+
<url>http://maven.apache.org</url>
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-help-plugin</artifactId>
32+
<configuration>
33+
<expression><![CDATA[project_version=${project.version}]]></expression>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
</project>

0 commit comments

Comments
 (0)