From 0a299275e3666cee4bfa6ac6a5942344d3280a4b Mon Sep 17 00:00:00 2001 From: Andrey Bruykhov Date: Fri, 10 Feb 2023 11:08:19 +0300 Subject: [PATCH] Close stream with try-with-resources This closes #997 --- .../java/org/apache/maven/cli/MavenCli.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 61b0e60f68..b2454c4e95 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -41,6 +41,7 @@ import java.util.Set; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; import com.google.inject.AbstractModule; import org.apache.commons.cli.CommandLine; @@ -334,14 +335,14 @@ public class MavenCli { File configFile = new File(cliRequest.multiModuleProjectDirectory, MVN_MAVEN_CONFIG); if (configFile.isFile()) { - String[] args = Files.lines(configFile.toPath(), Charset.defaultCharset()) - .filter(arg -> !arg.isEmpty()) - .toArray(size -> new String[size]); - mavenConfig = cliManager.parse(args); - List unrecognized = mavenConfig.getArgList(); - if (!unrecognized.isEmpty()) { - // This file can only contain options, not args (goals or phases) - throw new ParseException("Unrecognized maven.config file entries: " + unrecognized); + try (Stream lines = Files.lines(configFile.toPath(), Charset.defaultCharset())) { + String[] args = lines.filter(arg -> !arg.isEmpty()).toArray(String[]::new); + mavenConfig = cliManager.parse(args); + List unrecognized = mavenConfig.getArgList(); + if (!unrecognized.isEmpty()) { + // This file can only contain options, not args (goals or phases) + throw new ParseException("Unrecognized maven.config file entries: " + unrecognized); + } } } } catch (ParseException e) {