Skip to content

Commit 012e5de

Browse files
author
Vincent Potucek
committed
try can use automatic resource management
1 parent 4dfeeab commit 012e5de

File tree

7 files changed

+82
-110
lines changed

7 files changed

+82
-110
lines changed

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -486,39 +486,40 @@ public void substitute(UnaryOperator<String> callback) {
486486
* @throws IOException if an error occurs
487487
*/
488488
protected void saveLayout(Writer out, boolean typed) throws IOException {
489-
PropertiesWriter writer = new PropertiesWriter(out, typed);
490-
if (header != null) {
491-
for (String s : header) {
492-
writer.writeln(s);
493-
}
494-
}
489+
try (PropertiesWriter writer = new PropertiesWriter(out, typed)) {
495490

496-
for (String key : storage.keySet()) {
497-
Layout l = layout.get(key);
498-
if (l != null && l.getCommentLines() != null) {
499-
for (String s : l.getCommentLines()) {
491+
if (header != null) {
492+
for (String s : header) {
500493
writer.writeln(s);
501494
}
502495
}
503-
if (l != null && l.getValueLines() != null) {
504-
for (int i = 0; i < l.getValueLines().size(); i++) {
505-
String s = l.getValueLines().get(i);
506-
if (i < l.getValueLines().size() - 1) {
507-
writer.writeln(s + "\\");
508-
} else {
496+
497+
for (String key : storage.keySet()) {
498+
Layout l = layout.get(key);
499+
if (l != null && l.getCommentLines() != null) {
500+
for (String s : l.getCommentLines()) {
509501
writer.writeln(s);
510502
}
511503
}
512-
} else {
513-
writer.writeProperty(key, storage.get(key));
504+
if (l != null && l.getValueLines() != null) {
505+
for (int i = 0; i < l.getValueLines().size(); i++) {
506+
String s = l.getValueLines().get(i);
507+
if (i < l.getValueLines().size() - 1) {
508+
writer.writeln(s + "\\");
509+
} else {
510+
writer.writeln(s);
511+
}
512+
}
513+
} else {
514+
writer.writeProperty(key, storage.get(key));
515+
}
514516
}
515-
}
516-
if (footer != null) {
517-
for (String s : footer) {
518-
writer.writeln(s);
517+
if (footer != null) {
518+
for (String s : footer) {
519+
writer.writeln(s);
520+
}
519521
}
520522
}
521-
writer.flush();
522523
}
523524

524525
/**

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

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

475475
StringWriter sw = new StringWriter();
476-
PrintWriter pw2 = new PrintWriter(sw);
477-
formatter.printHelp(
478-
pw2,
479-
width,
480-
commandLineSyntax(command),
481-
System.lineSeparator() + "Options:",
482-
options,
483-
HelpFormatter.DEFAULT_LEFT_PAD,
484-
HelpFormatter.DEFAULT_DESC_PAD,
485-
System.lineSeparator(),
486-
false);
487-
pw2.flush();
476+
try (PrintWriter writer = new PrintWriter(sw)) {
477+
formatter.printHelp(
478+
writer,
479+
width,
480+
commandLineSyntax(command),
481+
System.lineSeparator() + "Options:",
482+
options,
483+
HelpFormatter.DEFAULT_LEFT_PAD,
484+
HelpFormatter.DEFAULT_DESC_PAD,
485+
System.lineSeparator(),
486+
false);
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: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,7 @@ protected Consumer<String> doDetermineWriter(C context) {
407407
} else {
408408
// Given the terminal creation has been offloaded to a different thread,
409409
// do not pass directly the terminal writer
410-
return msg -> {
411-
PrintWriter pw = context.terminal.writer();
412-
pw.println(msg);
413-
pw.flush();
414-
};
410+
return msg -> context.terminal.writer().println(msg);
415411
}
416412
}
417413

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -485,39 +485,39 @@ 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);
489-
if (header != null) {
490-
for (String s : header) {
491-
writer.writeln(s);
492-
}
493-
}
494-
495-
for (String key : storage.keySet()) {
496-
Layout l = layout.get(key);
497-
if (l != null && l.getCommentLines() != null) {
498-
for (String s : l.getCommentLines()) {
488+
try (PropertiesWriter writer = new PropertiesWriter(out, typed)) {
489+
if (header != null) {
490+
for (String s : header) {
499491
writer.writeln(s);
500492
}
501493
}
502-
if (l != null && l.getValueLines() != null) {
503-
for (int i = 0; i < l.getValueLines().size(); i++) {
504-
String s = l.getValueLines().get(i);
505-
if (i < l.getValueLines().size() - 1) {
506-
writer.writeln(s + "\\");
507-
} else {
494+
495+
for (String key : storage.keySet()) {
496+
Layout l = layout.get(key);
497+
if (l != null && l.getCommentLines() != null) {
498+
for (String s : l.getCommentLines()) {
508499
writer.writeln(s);
509500
}
510501
}
511-
} else {
512-
writer.writeProperty(key, storage.get(key));
502+
if (l != null && l.getValueLines() != null) {
503+
for (int i = 0; i < l.getValueLines().size(); i++) {
504+
String s = l.getValueLines().get(i);
505+
if (i < l.getValueLines().size() - 1) {
506+
writer.writeln(s + "\\");
507+
} else {
508+
writer.writeln(s);
509+
}
510+
}
511+
} else {
512+
writer.writeProperty(key, storage.get(key));
513+
}
513514
}
514-
}
515-
if (footer != null) {
516-
for (String s : footer) {
517-
writer.writeln(s);
515+
if (footer != null) {
516+
for (String s : footer) {
517+
writer.writeln(s);
518+
}
518519
}
519520
}
520-
writer.flush();
521521
}
522522

523523
/**

impl/maven-executor/src/test/java/org/apache/maven/cling/executor/MavenExecutorTestSupport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.junit.jupiter.api.Disabled;
3636
import org.junit.jupiter.api.Test;
3737
import org.junit.jupiter.api.Timeout;
38+
import org.junit.jupiter.api.condition.DisabledOnOs;
39+
import org.junit.jupiter.api.condition.OS;
3840
import org.junit.jupiter.api.io.CleanupMode;
3941
import org.junit.jupiter.api.io.TempDir;
4042

@@ -63,7 +65,7 @@ void mvnenc(
6365
System.out.println(Files.readString(cwd.resolve(logfile)));
6466
}
6567

66-
@Disabled("JUnit on Windows fails to clean up as mvn3 seems does not close log file properly")
68+
@DisabledOnOs(OS.WINDOWS)
6769
@Timeout(15)
6870
@Test
6971
void dump3(
@@ -126,7 +128,7 @@ void version() throws Exception {
126128
mavenVersion(mvn4ExecutorRequestBuilder().build()));
127129
}
128130

129-
@Disabled("JUnit on Windows fails to clean up as mvn3 seems does not close log file properly")
131+
@DisabledOnOs(OS.WINDOWS)
130132
@Timeout(15)
131133
@Test
132134
void defaultFs3x(@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path tempDir) throws Exception {

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: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.File;
2323
import java.io.FileOutputStream;
2424
import java.io.IOException;
25-
import java.io.OutputStream;
2625
import java.io.OutputStreamWriter;
2726

2827
import org.apache.maven.plugin.AbstractMojo;
@@ -37,6 +36,8 @@
3736
*/
3837
public abstract class AbstractLogMojo extends AbstractMojo {
3938

39+
private static final String MAVEN_CORE_IT_LOG = "[MAVEN-CORE-IT-LOG] ";
40+
4041
/**
4142
* The project's base directory, used for manual path translation.
4243
*/
@@ -76,27 +77,16 @@ private File getLogFile() {
7677
*/
7778
protected void append(Object value) throws MojoExecutionException {
7879
File file = getLogFile();
79-
getLog().info("[MAVEN-CORE-IT-LOG] Updating log file: " + file);
80-
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-
}
80+
if (value != null && file.getParentFile().mkdirs()) {
81+
getLog().info(MAVEN_CORE_IT_LOG + "Updating log file: " + file);
82+
getLog().info(MAVEN_CORE_IT_LOG + value);
83+
try (BufferedWriter writer =
84+
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), encoding))) {
85+
writer.write(value.toString());
86+
writer.newLine();
87+
} catch (IOException e) {
88+
throw new MojoExecutionException("Failed to update log file " + logFile, e);
9789
}
98-
} catch (IOException e) {
99-
throw new MojoExecutionException("Failed to update log file " + logFile, e);
10090
}
10191
}
10292

@@ -107,20 +97,16 @@ protected void append(Object value) throws MojoExecutionException {
10797
*/
10898
protected void reset() throws MojoExecutionException {
10999
File file = getLogFile();
110-
getLog().info("[MAVEN-CORE-IT-LOG] Resetting log file: " + file);
111-
try {
100+
if (file.getParentFile().mkdirs()) {
101+
getLog().info(MAVEN_CORE_IT_LOG + "Resetting log file: " + file);
112102
/*
113103
* NOTE: Intentionally don't delete the file but create a new empty one to check the plugin was executed.
114104
*/
115-
file.getParentFile().mkdirs();
116-
OutputStream out = new FileOutputStream(file);
117105
try {
118-
out.close();
119-
} catch (IOException e) {
120-
// just ignore, we tried our best to clean up
106+
new FileOutputStream(file).close();
107+
} catch (IOException ignore) {
108+
// tried our best to clean up
121109
}
122-
} catch (IOException e) {
123-
throw new MojoExecutionException("Failed to reset log file " + logFile, e);
124110
}
125111
}
126112
}

0 commit comments

Comments
 (0)