Skip to content

Commit 2df729c

Browse files
author
Vincent Potucek
committed
use try-with-resources statement in Mojo
1 parent ecfb7c7 commit 2df729c

File tree

7 files changed

+22
-92
lines changed

7 files changed

+22
-92
lines changed

its/core-it-support/core-it-plugins/maven-it-plugin-active-collection/src/main/java/org/apache/maven/plugin/coreit/CheckMojo.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
117117

118118
getLog().info("[MAVEN-CORE-IT-LOG] Creating output file " + outputFile);
119119

120-
OutputStream out = null;
121-
try {
122-
outputFile.getParentFile().mkdirs();
123-
out = new FileOutputStream(outputFile);
120+
outputFile.getParentFile().mkdirs();
121+
try (OutputStream out = new FileOutputStream(outputFile)) {
124122
componentProperties.store(out, "MAVEN-CORE-IT-LOG");
125123
} catch (IOException e) {
126-
throw new MojoExecutionException("Output file could not be created: " + outputFile, e);
127-
} finally {
128-
if (out != null) {
129-
try {
130-
out.close();
131-
} catch (IOException e) {
132-
// just ignore
133-
}
134-
}
124+
throw new MojoExecutionException(e);
135125
}
136126

137127
getLog().info("[MAVEN-CORE-IT-LOG] Created output file " + outputFile);

its/core-it-support/core-it-plugins/maven-it-plugin-active-collection/src/main/java/org/apache/maven/plugin/coreit/CheckThreadSafetyMojo.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,11 @@ public void run() {
133133

134134
getLog().info("[MAVEN-CORE-IT-LOG] Creating output file " + outputFile);
135135

136-
OutputStream out = null;
137-
try {
138-
outputFile.getParentFile().mkdirs();
139-
out = new FileOutputStream(outputFile);
136+
outputFile.getParentFile().mkdirs();
137+
try (OutputStream out = new FileOutputStream(outputFile)) {
140138
componentProperties.store(out, "MAVEN-CORE-IT-LOG");
141139
} catch (IOException e) {
142-
throw new MojoExecutionException("Output file could not be created: " + outputFile, e);
143-
} finally {
144-
if (out != null) {
145-
try {
146-
out.close();
147-
} catch (IOException e) {
148-
// just ignore
149-
}
150-
}
140+
throw new MojoExecutionException(e);
151141
}
152142

153143
getLog().info("[MAVEN-CORE-IT-LOG] Created output file " + outputFile);

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.FileOutputStream;
2323
import java.io.IOException;
2424
import java.io.OutputStream;
25+
import java.nio.file.Files;
2526
import java.util.Properties;
2627

2728
import org.apache.maven.plugin.MojoExecutionException;
@@ -35,21 +36,11 @@
3536
class PropertiesUtil {
3637

3738
public static void write(File outputFile, Properties props) throws MojoExecutionException {
38-
OutputStream out = null;
39-
try {
40-
outputFile.getParentFile().mkdirs();
41-
out = new FileOutputStream(outputFile);
39+
outputFile.getParentFile().mkdirs();
40+
try (OutputStream out = Files.newOutputStream(outputFile.toPath())) {
4241
props.store(out, "MAVEN-CORE-IT-LOG");
4342
} catch (IOException e) {
44-
throw new MojoExecutionException("Output file could not be created: " + outputFile, e);
45-
} finally {
46-
if (out != null) {
47-
try {
48-
out.close();
49-
} catch (IOException e) {
50-
// just ignore
51-
}
52-
}
43+
throw new MojoExecutionException(e);
5344
}
5445
}
5546
}

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,11 @@ public static Properties read(File inputFile) throws MojoExecutionException {
6969
}
7070

7171
public static void write(File outputFile, Properties props) throws MojoExecutionException {
72-
OutputStream os = null;
73-
try {
74-
outputFile.getParentFile().mkdirs();
75-
os = new FileOutputStream(outputFile);
72+
outputFile.getParentFile().mkdirs();
73+
try (OutputStream os = new FileOutputStream(outputFile)) {
7674
props.store(os, "MAVEN-CORE-IT-LOG");
7775
} catch (IOException e) {
78-
throw new MojoExecutionException(
79-
"Output file " + outputFile + " could not be created: " + e.getMessage(), e);
80-
} finally {
81-
if (os != null) {
82-
try {
83-
os.close();
84-
} catch (IOException e) {
85-
// just ignore
86-
}
87-
}
76+
throw new MojoExecutionException(e);
8877
}
8978
}
9079

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
9696

9797
getLog().info("[MAVEN-CORE-IT-LOG] Creating output file: " + outputFile);
9898

99-
OutputStream out = null;
100-
try {
101-
outputFile.getParentFile().mkdirs();
102-
out = new FileOutputStream(outputFile);
99+
outputFile.getParentFile().mkdirs();
100+
try (OutputStream out = new FileOutputStream(outputFile)) {
103101
props.store(out, "MAVEN-CORE-IT-LOG");
104102
} catch (IOException e) {
105-
throw new MojoExecutionException("Output file could not be created: " + outputFile, e);
106-
} finally {
107-
if (out != null) {
108-
try {
109-
out.close();
110-
} catch (IOException e) {
111-
// just ignore
112-
}
113-
}
103+
throw new MojoExecutionException(e);
114104
}
115105

116106
getLog().info("[MAVEN-CORE-IT-LOG] Created output file: " + outputFile);

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,11 @@ public void execute() throws MojoExecutionException {
105105
}
106106
}
107107

108-
OutputStream out = null;
109-
try {
110-
outputFile.getParentFile().mkdirs();
111-
out = new FileOutputStream(outputFile);
108+
outputFile.getParentFile().mkdirs();
109+
try (OutputStream out = new FileOutputStream(outputFile)) {
112110
properties.store(out, "MAVEN-CORE-IT-LOG");
113111
} catch (IOException e) {
114-
throw new MojoExecutionException(e.getMessage(), e);
115-
} finally {
116-
if (out != null) {
117-
try {
118-
out.close();
119-
} catch (IOException e) {
120-
// ignore
121-
}
122-
}
112+
throw new MojoExecutionException(e);
123113
}
124114
}
125115

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,11 @@ public void execute() throws MojoExecutionException {
8787
}
8888
}
8989

90-
OutputStream out = null;
91-
try {
92-
outputFile.getParentFile().mkdirs();
93-
out = new FileOutputStream(outputFile);
90+
outputFile.getParentFile().mkdirs();
91+
try (OutputStream out = new FileOutputStream(outputFile)) {
9492
properties.store(out, "MAVEN-CORE-IT-LOG");
9593
} catch (IOException e) {
96-
throw new MojoExecutionException(e.getMessage(), e);
97-
} finally {
98-
if (out != null) {
99-
try {
100-
out.close();
101-
} catch (IOException e) {
102-
// ignore
103-
}
104-
}
94+
throw new MojoExecutionException(e);
10595
}
10696
}
10797
}

0 commit comments

Comments
 (0)