Skip to content

Commit 64e3eeb

Browse files
author
Vincent Potucek
committed
try can use automatic resource management
1 parent 32d9512 commit 64e3eeb

File tree

8 files changed

+30
-62
lines changed

8 files changed

+30
-62
lines changed

compat/maven-embedder/src/main/java/org/apache/maven/cli/props/MavenProperties.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.Set;
4545
import java.util.function.UnaryOperator;
4646

47+
import ch.qos.logback.core.Layout;
4748
import org.apache.maven.impl.model.DefaultInterpolator;
4849

4950
/**
@@ -486,7 +487,9 @@ public void substitute(UnaryOperator<String> callback) {
486487
* @throws IOException if an error occurs
487488
*/
488489
protected void saveLayout(Writer out, boolean typed) throws IOException {
489-
PropertiesWriter writer = new PropertiesWriter(out, typed);
490+
try(PropertiesWriter writer = new PropertiesWriter(out, typed)) {
491+
492+
490493
if (header != null) {
491494
for (String s : header) {
492495
writer.writeln(s);
@@ -518,7 +521,7 @@ protected void saveLayout(Writer out, boolean typed) throws IOException {
518521
writer.writeln(s);
519522
}
520523
}
521-
writer.flush();
524+
}
522525
}
523526

524527
/**

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ public void displayHelp(String command, Consumer<String> pw) {
473473
pw.accept("");
474474

475475
StringWriter sw = new StringWriter();
476-
PrintWriter pw2 = new PrintWriter(sw);
476+
try (PrintWriter writer = new PrintWriter(sw)) {
477477
formatter.printHelp(
478-
pw2,
478+
writer,
479479
width,
480480
commandLineSyntax(command),
481481
System.lineSeparator() + "Options:",
@@ -484,7 +484,7 @@ public void displayHelp(String command, Consumer<String> pw) {
484484
HelpFormatter.DEFAULT_DESC_PAD,
485485
System.lineSeparator(),
486486
false);
487-
pw2.flush();
487+
}
488488
for (String s : sw.toString().split(System.lineSeparator())) {
489489
pw.accept(s);
490490
}

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,9 @@ protected Consumer<String> doDetermineWriter(C context) {
408408
// Given the terminal creation has been offloaded to a different thread,
409409
// do not pass directly the terminal writer
410410
return msg -> {
411-
PrintWriter pw = context.terminal.writer();
411+
try (PrintWriter pw = context.terminal.writer()) {
412412
pw.println(msg);
413-
pw.flush();
413+
}
414414
};
415415
}
416416
}

impl/maven-cli/src/main/java/org/apache/maven/cling/props/MavenProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public void substitute(UnaryOperator<String> callback) {
485485
* @throws IOException if an error occurs
486486
*/
487487
protected void saveLayout(Writer out, boolean typed) throws IOException {
488-
PropertiesWriter writer = new PropertiesWriter(out, typed);
488+
try (PropertiesWriter writer = new PropertiesWriter(out, typed)) {
489489
if (header != null) {
490490
for (String s : header) {
491491
writer.writeln(s);
@@ -517,7 +517,7 @@ protected void saveLayout(Writer out, boolean typed) throws IOException {
517517
writer.writeln(s);
518518
}
519519
}
520-
writer.flush();
520+
}
521521
}
522522

523523
/**

impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/ConsoleMavenTransferListener.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ public ConsoleMavenTransferListener(
5050

5151
@Override
5252
public void transferInitiated(TransferEvent event) {
53-
overridePreviousTransfer(event);
53+
overridePreviousTransfer();
5454

5555
super.transferInitiated(event);
5656
}
5757

5858
@Override
5959
public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
60-
overridePreviousTransfer(event);
60+
overridePreviousTransfer();
6161

6262
super.transferCorrupted(event);
6363
}
@@ -123,20 +123,20 @@ private void pad(StringBuilder buffer, int spaces) {
123123
@Override
124124
public void transferSucceeded(TransferEvent event) {
125125
transfers.remove(new TransferResourceIdentifier(event.getResource()));
126-
overridePreviousTransfer(event);
126+
overridePreviousTransfer();
127127

128128
super.transferSucceeded(event);
129129
}
130130

131131
@Override
132132
public void transferFailed(TransferEvent event) {
133133
transfers.remove(new TransferResourceIdentifier(event.getResource()));
134-
overridePreviousTransfer(event);
134+
overridePreviousTransfer();
135135

136136
super.transferFailed(event);
137137
}
138138

139-
private void overridePreviousTransfer(TransferEvent event) {
139+
private void overridePreviousTransfer() {
140140
if (lastLength > 0) {
141141
pad(buffer, lastLength);
142142
buffer.append('\r');

impl/maven-executor/src/main/java/org/apache/maven/cling/executor/forked/ForkedMavenExecutor.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,8 @@ protected CountDownLatch pump(Process p, ExecutorRequest executorRequest) {
163163
CountDownLatch latch = new CountDownLatch(3);
164164
String suffix = "-pump-" + p.pid();
165165
Thread stdoutPump = new Thread(() -> {
166-
try {
167-
OutputStream stdout = executorRequest.stdOut().orElse(OutputStream.nullOutputStream());
166+
try (OutputStream stdout = executorRequest.stdOut().orElse(OutputStream.nullOutputStream())){
168167
p.getInputStream().transferTo(stdout);
169-
stdout.flush();
170168
} catch (IOException e) {
171169
throw new UncheckedIOException(e);
172170
} finally {
@@ -176,10 +174,8 @@ protected CountDownLatch pump(Process p, ExecutorRequest executorRequest) {
176174
stdoutPump.setName("stdout" + suffix);
177175
stdoutPump.start();
178176
Thread stderrPump = new Thread(() -> {
179-
try {
180-
OutputStream stderr = executorRequest.stdErr().orElse(OutputStream.nullOutputStream());
177+
try (OutputStream stderr = executorRequest.stdErr().orElse(OutputStream.nullOutputStream())){
181178
p.getErrorStream().transferTo(stderr);
182-
stderr.flush();
183179
} catch (IOException e) {
184180
throw new UncheckedIOException(e);
185181
} finally {
@@ -189,10 +185,8 @@ protected CountDownLatch pump(Process p, ExecutorRequest executorRequest) {
189185
stderrPump.setName("stderr" + suffix);
190186
stderrPump.start();
191187
Thread stdinPump = new Thread(() -> {
192-
try {
193-
OutputStream in = p.getOutputStream();
188+
try (OutputStream in = p.getOutputStream()){
194189
executorRequest.stdIn().orElse(InputStream.nullInputStream()).transferTo(in);
195-
in.flush();
196190
} catch (IOException e) {
197191
throw new UncheckedIOException(e);
198192
} finally {

its/core-it-support/core-it-plugins/maven-it-plugin-context-passing/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,10 @@ public void execute() throws MojoExecutionException {
5858

5959
File outfile = new File(outDir, value);
6060

61-
Writer writer = null;
62-
try {
63-
writer = new FileWriter(outfile);
64-
61+
try (Writer writer = new FileWriter(outfile)) {
6562
writer.write(value);
66-
67-
writer.flush();
6863
} catch (IOException e) {
6964
throw new MojoExecutionException("Cannot write output file: " + outfile, e);
70-
} finally {
71-
if (writer != null) {
72-
try {
73-
writer.close();
74-
} catch (IOException e) {
75-
// ignore
76-
}
77-
}
7865
}
7966
}
8067
}

its/core-it-support/core-it-plugins/maven-it-plugin-log-file/src/main/java/org/apache/maven/plugin/coreit/AbstractLogMojo.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,13 @@ protected void append(Object value) throws MojoExecutionException {
7878
File file = getLogFile();
7979
getLog().info("[MAVEN-CORE-IT-LOG] Updating log file: " + file);
8080
getLog().info("[MAVEN-CORE-IT-LOG] " + value);
81-
try {
82-
file.getParentFile().mkdirs();
83-
OutputStream out = new FileOutputStream(file, true);
84-
try {
85-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoding));
86-
if (value != null) {
87-
writer.write(value.toString());
88-
writer.newLine();
89-
writer.flush();
90-
}
91-
} finally {
92-
try {
93-
out.close();
94-
} catch (IOException e) {
95-
// just ignore, we tried our best to clean up
96-
}
81+
if (value != null && file.getParentFile().mkdirs()) {
82+
try (BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), encoding))) {
83+
w.write(value.toString());
84+
w.newLine();
85+
} catch (IOException e) {
86+
throw new MojoExecutionException("Failed to update log file " + logFile, e);
9787
}
98-
} catch (IOException e) {
99-
throw new MojoExecutionException("Failed to update log file " + logFile, e);
10088
}
10189
}
10290

@@ -108,19 +96,15 @@ protected void append(Object value) throws MojoExecutionException {
10896
protected void reset() throws MojoExecutionException {
10997
File file = getLogFile();
11098
getLog().info("[MAVEN-CORE-IT-LOG] Resetting log file: " + file);
99+
if(file.getParentFile().mkdirs()){
111100
try {
112101
/*
113102
* NOTE: Intentionally don't delete the file but create a new empty one to check the plugin was executed.
114103
*/
115-
file.getParentFile().mkdirs();
116-
OutputStream out = new FileOutputStream(file);
117-
try {
118-
out.close();
119-
} catch (IOException e) {
120-
// just ignore, we tried our best to clean up
121-
}
104+
new FileOutputStream(file).close();
122105
} catch (IOException e) {
123106
throw new MojoExecutionException("Failed to reset log file " + logFile, e);
124107
}
108+
}
125109
}
126110
}

0 commit comments

Comments
 (0)