From cea9c459d85dc8de58beda28c36dd57bcb2083e4 Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 12 Sep 2024 13:03:08 +0300 Subject: [PATCH] try with closed resources --- .../java/io/jenkins/plugins/jfrog/JfStep.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index da7df388..37b1873d 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -329,19 +329,20 @@ Version getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, In if (this.currentCliVersion != null) { return this.currentCliVersion; } - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ArgumentListBuilder builder = new ArgumentListBuilder(); - builder.add(jfrogBinaryPath).add("-v"); - int exitCode = launcher - .cmds(builder) - .pwd(launcher.pwd()) - .stdout(outputStream) - .join(); - if (exitCode != 0) { - throw new IOException("Failed to get JFrog CLI version: " + outputStream.toString(StandardCharsets.UTF_8)); + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){ + ArgumentListBuilder builder = new ArgumentListBuilder(); + builder.add(jfrogBinaryPath).add("-v"); + int exitCode = launcher + .cmds(builder) + .pwd(launcher.pwd()) + .stdout(outputStream) + .join(); + if (exitCode != 0) { + throw new IOException("Failed to get JFrog CLI version: " + outputStream.toString(StandardCharsets.UTF_8)); + } + String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim(); + String version = StringUtils.substringAfterLast(versionOutput, " "); + return new Version(version); } - String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim(); - String version = StringUtils.substringAfterLast(versionOutput, " "); - return new Version(version); } }