Skip to content

Commit e1d3214

Browse files
author
Vincent Potucek
committed
use try-with-resources statement in Mojo
1 parent 761a56b commit e1d3214

File tree

7 files changed

+22
-93
lines changed

7 files changed

+22
-93
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
@@ -134,21 +134,11 @@ public void run() {
134134

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

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

154144
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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
package org.apache.maven.plugin.coreit;
2020

2121
import java.io.File;
22-
import java.io.FileOutputStream;
2322
import java.io.IOException;
2423
import java.io.OutputStream;
24+
import java.nio.file.Files;
2525
import java.util.Properties;
2626

2727
import org.apache.maven.plugin.MojoExecutionException;
@@ -35,21 +35,11 @@
3535
class PropertiesUtil {
3636

3737
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);
38+
outputFile.getParentFile().mkdirs();
39+
try (OutputStream out = Files.newOutputStream(outputFile.toPath())) {
4240
props.store(out, "MAVEN-CORE-IT-LOG");
4341
} 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-
}
42+
throw new MojoExecutionException(e);
5343
}
5444
}
5545
}

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)