diff --git a/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java b/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java index 39b2d7226..c79a61e76 100644 --- a/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java +++ b/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java @@ -76,6 +76,7 @@ import java.util.Optional; import java.util.UUID; import java.util.function.Function; +import java.util.function.Predicate; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -642,36 +643,35 @@ private String obtainMvnExecutableFromMavenInstallation(String mavenInstallation private MavenVersion readMavenVersion(String mvnExecPath) { try { - Optional version = new FilePath(ws.getChannel(), mvnExecPath) - .act(new MasterToSlaveFileCallable>() { - - private static final long serialVersionUID = -1064011914865943982L; - - @Override - public Optional invoke(File f, VirtualChannel channel) - throws IOException, InterruptedException { - ProcessBuilder builder = new ProcessBuilder(); - if (Platform.current() == Platform.WINDOWS) { - builder.command("cmd.exe", "/c", "\"" + f.getAbsolutePath() + "\" --version"); - } else { - builder.command("sh", "-c", "\"" + f.getAbsolutePath() + "\" --version"); - } - Process process = builder.start(); - try (InputStreamReader is = new InputStreamReader(process.getInputStream(), "UTF-8"); - BufferedReader reader = new BufferedReader(is)) { - Optional versionLine = reader.lines() - .filter(MavenVersionUtils.containsMavenVersion()) - .findFirst(); - int exitCode = process.waitFor(); - if (exitCode != 0) { - console.trace("[withMaven] failed to read Maven version (" + exitCode + "): " - + new String( - process.getErrorStream().readAllBytes(), "UTF-8")); - } - return exitCode == 0 ? versionLine : Optional.empty(); - } + String result = new FilePath(ws.getChannel(), mvnExecPath).act(new MasterToSlaveFileCallable() { + + private static final long serialVersionUID = -1064011914865943982L; + + @Override + public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { + ProcessBuilder builder = new ProcessBuilder(); + if (Platform.current() == Platform.WINDOWS) { + builder.command("cmd.exe", "/c", "\"" + f.getAbsolutePath() + "\" --version"); + } else { + builder.command("sh", "-c", "\"" + f.getAbsolutePath() + "\" --version"); + } + Process process = builder.start(); + try (InputStreamReader is = new InputStreamReader(process.getInputStream(), "UTF-8"); + BufferedReader reader = new BufferedReader(is)) { + Optional versionLine = reader.lines() + .filter(MavenVersionUtils.containsMavenVersion()) + .findFirst(); + int exitCode = process.waitFor(); + if (exitCode != 0) { + console.trace("[withMaven] failed to read Maven version (" + exitCode + "): " + + new String(process.getErrorStream().readAllBytes(), "UTF-8")); + return null; } - }); + return versionLine.orElse(""); + } + } + }); + Optional version = Optional.ofNullable(result).filter(Predicate.not(String::isBlank)); console.trace("[withMaven] found Maven version: " + version.orElse("none")); return version.map(MavenVersionUtils::parseMavenVersion).orElse(MavenVersion.UNKNOWN); } catch (Exception ex) {