Skip to content

Commit e99e668

Browse files
author
Vincent Potucek
committed
try can use automatic resource management
1 parent f4c3fdd commit e99e668

35 files changed

Lines changed: 51 additions & 161 deletions

File tree

its/core-it-suite/src/test/resources/mng-2135/plugin/src/main/java/coreit/ItMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public class ItMojo extends AbstractMojo {
5252
private File outputFile;
5353

5454
public void execute() throws MojoExecutionException {
55+
outputFile.getParentFile().mkdirs();
5556
try {
56-
outputFile.getParentFile().mkdirs();
5757
outputFile.createNewFile();
5858
} catch (IOException e) {
5959
throw new MojoExecutionException("Failed to create touch file: " + e.getMessage(), e);

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

Lines changed: 2 additions & 12 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) {
126124
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-
}
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: 2 additions & 12 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) {
142140
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-
}
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: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = new FileOutputStream(outputFile)) {
4240
props.store(out, "MAVEN-CORE-IT-LOG");
4341
} catch (IOException e) {
4442
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-
}
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 & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,24 @@ 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

6858
return props;
6959
}
7060

7161
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);
62+
outputFile.getParentFile().mkdirs();
63+
try (OutputStream os = new FileOutputStream(outputFile)) {
7664
props.store(os, "MAVEN-CORE-IT-LOG");
7765
} catch (IOException e) {
7866
throw new MojoExecutionException(
7967
"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-
}
8868
}
8969
}
9070

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-core-stubs/maven-clean-plugin/src/main/java/org/apache/maven/plugin/coreit/CleanMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7272

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

75+
outputFile.getParentFile().mkdirs();
7576
try {
76-
outputFile.getParentFile().mkdirs();
7777
outputFile.createNewFile();
7878
} catch (IOException e) {
7979
throw new MojoExecutionException("Output file could not be created: " + pathname, e);

its/core-it-support/core-it-plugins/maven-it-plugin-core-stubs/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/coreit/CompileMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7272

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

75+
outputFile.getParentFile().mkdirs();
7576
try {
76-
outputFile.getParentFile().mkdirs();
7777
outputFile.createNewFile();
7878
} catch (IOException e) {
7979
throw new MojoExecutionException("Output file could not be created: " + pathname, e);

its/core-it-support/core-it-plugins/maven-it-plugin-core-stubs/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/coreit/TestCompileMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7272

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

75+
outputFile.getParentFile().mkdirs();
7576
try {
76-
outputFile.getParentFile().mkdirs();
7777
outputFile.createNewFile();
7878
} catch (IOException e) {
7979
throw new MojoExecutionException("Output file could not be created: " + pathname, e);

its/core-it-support/core-it-plugins/maven-it-plugin-core-stubs/maven-deploy-plugin/src/main/java/org/apache/maven/plugin/coreit/DeployMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7272

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

75+
outputFile.getParentFile().mkdirs();
7576
try {
76-
outputFile.getParentFile().mkdirs();
7777
outputFile.createNewFile();
7878
} catch (IOException e) {
7979
throw new MojoExecutionException("Output file could not be created: " + pathname, e);

0 commit comments

Comments
 (0)