diff --git a/compat/maven-embedder/src/main/java/org/apache/maven/cli/props/MavenProperties.java b/compat/maven-embedder/src/main/java/org/apache/maven/cli/props/MavenProperties.java index 606eb65b5c10..426df8cad14d 100644 --- a/compat/maven-embedder/src/main/java/org/apache/maven/cli/props/MavenProperties.java +++ b/compat/maven-embedder/src/main/java/org/apache/maven/cli/props/MavenProperties.java @@ -486,39 +486,40 @@ public void substitute(UnaryOperator callback) { * @throws IOException if an error occurs */ protected void saveLayout(Writer out, boolean typed) throws IOException { - PropertiesWriter writer = new PropertiesWriter(out, typed); - if (header != null) { - for (String s : header) { - writer.writeln(s); - } - } + try (PropertiesWriter writer = new PropertiesWriter(out, typed)) { - for (String key : storage.keySet()) { - Layout l = layout.get(key); - if (l != null && l.getCommentLines() != null) { - for (String s : l.getCommentLines()) { + if (header != null) { + for (String s : header) { writer.writeln(s); } } - if (l != null && l.getValueLines() != null) { - for (int i = 0; i < l.getValueLines().size(); i++) { - String s = l.getValueLines().get(i); - if (i < l.getValueLines().size() - 1) { - writer.writeln(s + "\\"); - } else { + + for (String key : storage.keySet()) { + Layout l = layout.get(key); + if (l != null && l.getCommentLines() != null) { + for (String s : l.getCommentLines()) { writer.writeln(s); } } - } else { - writer.writeProperty(key, storage.get(key)); + if (l != null && l.getValueLines() != null) { + for (int i = 0; i < l.getValueLines().size(); i++) { + String s = l.getValueLines().get(i); + if (i < l.getValueLines().size() - 1) { + writer.writeln(s + "\\"); + } else { + writer.writeln(s); + } + } + } else { + writer.writeProperty(key, storage.get(key)); + } } - } - if (footer != null) { - for (String s : footer) { - writer.writeln(s); + if (footer != null) { + for (String s : footer) { + writer.writeln(s); + } } } - writer.flush(); } /** diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java index cc914e7d9382..b7041cfe30b4 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java @@ -473,18 +473,18 @@ public void displayHelp(String command, Consumer pw) { pw.accept(""); StringWriter sw = new StringWriter(); - PrintWriter pw2 = new PrintWriter(sw); - formatter.printHelp( - pw2, - width, - commandLineSyntax(command), - System.lineSeparator() + "Options:", - options, - HelpFormatter.DEFAULT_LEFT_PAD, - HelpFormatter.DEFAULT_DESC_PAD, - System.lineSeparator(), - false); - pw2.flush(); + try (PrintWriter writer = new PrintWriter(sw)) { + formatter.printHelp( + writer, + width, + commandLineSyntax(command), + System.lineSeparator() + "Options:", + options, + HelpFormatter.DEFAULT_LEFT_PAD, + HelpFormatter.DEFAULT_DESC_PAD, + System.lineSeparator(), + false); + } for (String s : sw.toString().split(System.lineSeparator())) { pw.accept(s); } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java index c0b8c50b56f3..7a7b6d092489 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java @@ -407,11 +407,7 @@ protected Consumer doDetermineWriter(C context) { } else { // Given the terminal creation has been offloaded to a different thread, // do not pass directly the terminal writer - return msg -> { - PrintWriter pw = context.terminal.writer(); - pw.println(msg); - pw.flush(); - }; + return msg -> context.terminal.writer().println(msg); } } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/props/MavenProperties.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/props/MavenProperties.java index da01bf6a9650..888d15902687 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/props/MavenProperties.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/props/MavenProperties.java @@ -485,39 +485,39 @@ public void substitute(UnaryOperator callback) { * @throws IOException if an error occurs */ protected void saveLayout(Writer out, boolean typed) throws IOException { - PropertiesWriter writer = new PropertiesWriter(out, typed); - if (header != null) { - for (String s : header) { - writer.writeln(s); - } - } - - for (String key : storage.keySet()) { - Layout l = layout.get(key); - if (l != null && l.getCommentLines() != null) { - for (String s : l.getCommentLines()) { + try (PropertiesWriter writer = new PropertiesWriter(out, typed)) { + if (header != null) { + for (String s : header) { writer.writeln(s); } } - if (l != null && l.getValueLines() != null) { - for (int i = 0; i < l.getValueLines().size(); i++) { - String s = l.getValueLines().get(i); - if (i < l.getValueLines().size() - 1) { - writer.writeln(s + "\\"); - } else { + + for (String key : storage.keySet()) { + Layout l = layout.get(key); + if (l != null && l.getCommentLines() != null) { + for (String s : l.getCommentLines()) { writer.writeln(s); } } - } else { - writer.writeProperty(key, storage.get(key)); + if (l != null && l.getValueLines() != null) { + for (int i = 0; i < l.getValueLines().size(); i++) { + String s = l.getValueLines().get(i); + if (i < l.getValueLines().size() - 1) { + writer.writeln(s + "\\"); + } else { + writer.writeln(s); + } + } + } else { + writer.writeProperty(key, storage.get(key)); + } } - } - if (footer != null) { - for (String s : footer) { - writer.writeln(s); + if (footer != null) { + for (String s : footer) { + writer.writeln(s); + } } } - writer.flush(); } /**