Skip to content

Commit 86d635b

Browse files
author
Vincent Potucek
committed
try can use automatic resource management 2/2
1 parent e19aa49 commit 86d635b

File tree

2 files changed

+12
-41
lines changed
  • its/core-it-support/core-it-plugins
    • maven-it-plugin-context-passing/src/main/java/org/apache/maven/plugin/coreit
    • maven-it-plugin-log-file/src/main/java/org/apache/maven/plugin/coreit

2 files changed

+12
-41
lines changed

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: 11 additions & 27 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;
@@ -78,25 +77,14 @@ protected void append(Object value) throws MojoExecutionException {
7877
File file = getLogFile();
7978
getLog().info("[MAVEN-CORE-IT-LOG] Updating log file: " + file);
8079
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+
try (BufferedWriter writer =
82+
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), encoding))) {
83+
writer.write(value.toString());
84+
writer.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);
111-
try {
99+
if (file.getParentFile().mkdirs()) {
112100
/*
113101
* NOTE: Intentionally don't delete the file but create a new empty one to check the plugin was executed.
114102
*/
115-
file.getParentFile().mkdirs();
116-
OutputStream out = new FileOutputStream(file);
117103
try {
118-
out.close();
119-
} catch (IOException e) {
120-
// just ignore, we tried our best to clean up
104+
new FileOutputStream(file).close();
105+
} catch (IOException ignore) {
106+
// tried our best to clean up
121107
}
122-
} catch (IOException e) {
123-
throw new MojoExecutionException("Failed to reset log file " + logFile, e);
124108
}
125109
}
126110
}

0 commit comments

Comments
 (0)