Skip to content

Commit 6cd950e

Browse files
author
Vincent Potucek
committed
try can use automatic resource management - current
1 parent 32d9512 commit 6cd950e

File tree

3 files changed

+3
-32
lines changed
  • its/core-it-support/core-it-plugins
    • maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit
    • 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

3 files changed

+3
-32
lines changed

its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/PropertiesUtil.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,11 @@ public static Properties read(File inputFile) throws MojoExecutionException {
4747
Properties props = new Properties();
4848

4949
if (inputFile.exists()) {
50-
InputStream is = null;
51-
try {
52-
is = new FileInputStream(inputFile);
50+
try (InputStream is = new FileInputStream(inputFile)) {
5351
props.load(is);
5452
} catch (IOException e) {
5553
throw new MojoExecutionException(
5654
"Input file " + inputFile + " could not be read: " + e.getMessage(), e);
57-
} finally {
58-
if (is != null) {
59-
try {
60-
is.close();
61-
} catch (IOException e) {
62-
// just ignore
63-
}
64-
}
6555
}
6656
}
6757

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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,11 @@ 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-
6763
writer.flush();
6864
} catch (IOException e) {
6965
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-
}
7866
}
7967
}
8068
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,13 @@ protected void append(Object value) throws MojoExecutionException {
8080
getLog().info("[MAVEN-CORE-IT-LOG] " + value);
8181
try {
8282
file.getParentFile().mkdirs();
83-
OutputStream out = new FileOutputStream(file, true);
84-
try {
83+
try (OutputStream out = new FileOutputStream(file, true)) {
8584
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoding));
8685
if (value != null) {
8786
writer.write(value.toString());
8887
writer.newLine();
8988
writer.flush();
9089
}
91-
} finally {
92-
try {
93-
out.close();
94-
} catch (IOException e) {
95-
// just ignore, we tried our best to clean up
96-
}
9790
}
9891
} catch (IOException e) {
9992
throw new MojoExecutionException("Failed to update log file " + logFile, e);

0 commit comments

Comments
 (0)