Skip to content

Commit 6a00c04

Browse files
nfalco79bguerin
andauthored
Fix withMaven that fails to recognize the installed maven version (#1329)
* Fix withMaven that fails to recognize the installed maven version Add calculated envs when the step execution launch maven command to retrieve the version * fix unit test and detection with colors --------- Co-authored-by: Benoit GUERIN <benoit.guerin1@free.fr>
1 parent 7c89b83 commit 6a00c04

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,10 @@ private MavenVersion readMavenVersion(String mvnExecPath) {
662662
try (ByteArrayOutputStream stdout = new ByteArrayOutputStream();
663663
ByteArrayOutputStream stderr = new ByteArrayOutputStream()) {
664664
ProcStarter ps = launcher.launch();
665-
Proc p = launcher.launch(
666-
ps.cmds(mvnExecPath, "--version").stdout(stdout).stderr(stderr));
665+
Proc p = launcher.launch(ps.cmds(mvnExecPath, "--version")
666+
.envs(env.overrideAll(envOverride))
667+
.stdout(stdout)
668+
.stderr(stderr));
667669
int exitCode = p.join();
668670
if (exitCode == 0) {
669671
Optional<String> version = stdout.toString(getComputer().getDefaultCharset())
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package org.jenkinsci.plugins.pipeline.maven.util;
22

33
import java.util.function.Predicate;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
46

57
public class MavenVersionUtils {
68

9+
private static final Pattern PATTERN = Pattern.compile(".*Apache Maven (\\d+\\.\\d+\\.\\d+)(.*)( \\(.*\\))?");
10+
711
public static Predicate<String> containsMavenVersion() {
8-
return string -> string != null && string.matches("Apache Maven \\d+\\.\\d+\\.\\d+(.*) \\(.*\\)");
12+
return string -> string != null && PATTERN.matcher(string).matches();
913
}
1014

1115
public static MavenVersion parseMavenVersion(String outputLine) {
12-
int prefixLength = "Apache Maven ".length();
13-
int startHashIndex = outputLine.indexOf("(");
14-
String version = startHashIndex != -1
15-
? outputLine.substring(prefixLength, startHashIndex)
16-
: outputLine.substring(prefixLength);
17-
return MavenVersion.fromString(version.trim());
16+
Matcher m = PATTERN.matcher(outputLine);
17+
return m.matches() && m.groupCount() > 1 ? MavenVersion.fromString(m.group(1)) : null;
1818
}
1919
}

pipeline-maven/src/test/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepITest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ public void tesWithDifferentJavasForBuild(String jdkName, String jdkPath) throws
174174
assertThat(archives).hasSize(1);
175175
assertThat(archives.iterator().next().endsWith("mono-dependency-maven-project-0.1-SNAPSHOT.jar"))
176176
.isTrue();
177+
jenkinsRule.assertLogNotContains(
178+
"[withMaven] WARNING: You are running an old version of Maven (UNKNOWN), you should update to at least 3.8.x",
179+
run);
177180
}
178181
}
179182

pipeline-maven/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/MavenVersionUtilsTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,29 @@ public void should_detect_maven_version() {
1414
assertThat(MavenVersionUtils.containsMavenVersion().test("not a version"))
1515
.isFalse();
1616
assertThat(MavenVersionUtils.containsMavenVersion().test("3.8.6")).isFalse();
17-
assertThat(MavenVersionUtils.containsMavenVersion().test("Apache Maven 3.9.6"))
18-
.isFalse();
1917

18+
assertThat(MavenVersionUtils.containsMavenVersion().test(new String(new byte[] {
19+
27, 91, 49, 109, 65, 112, 97, 99, 104, 101, 32, 77, 97, 118, 101, 110, 32, 51, 46, 56, 46, 55, 27,
20+
91, 109
21+
})))
22+
.isTrue();
23+
assertThat(MavenVersionUtils.containsMavenVersion().test("Apache Maven 3.9.6"))
24+
.isTrue();
2025
assertThat(MavenVersionUtils.containsMavenVersion()
2126
.test("Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)"))
2227
.isTrue();
23-
2428
assertThat(MavenVersionUtils.containsMavenVersion()
2529
.test("Apache Maven 4.0.0-beta-3 (e92f645c2749eb2a4f5a8843cf01e7441e4b559f)"))
2630
.isTrue();
2731
}
2832

2933
@Test
3034
public void should_parse_maven_version() {
35+
assertThat(MavenVersionUtils.parseMavenVersion(new String(new byte[] {
36+
27, 91, 49, 109, 65, 112, 97, 99, 104, 101, 32, 77, 97, 118, 101, 110, 32, 51, 46, 56, 46, 55, 27,
37+
91, 109
38+
})))
39+
.isEqualTo(new MavenVersion(3, 8, 7));
3140
assertThat(MavenVersionUtils.parseMavenVersion("Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)"))
3241
.isEqualTo(new MavenVersion(3, 9, 6));
3342
assertThat(MavenVersionUtils.parseMavenVersion("Apache Maven 3.8.8 (4c87b05d9aedce574290d1acc98575ed5eb6cd39)"))

0 commit comments

Comments
 (0)