Apply try-with-resources
authorrfscholte <rfscholte@apache.org>
Wed, 9 Jan 2019 21:44:47 +0000 (22:44 +0100)
committerrfscholte <rfscholte@apache.org>
Wed, 9 Jan 2019 21:45:21 +0000 (22:45 +0100)
src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
src/main/java/org/apache/maven/plugins/invoker/InvokerReport.java
src/main/java/org/apache/maven/plugins/invoker/MetadataUtils.java
src/main/java/org/apache/maven/plugins/invoker/PomUtils.java
src/main/java/org/apache/maven/plugins/invoker/VerifyMojo.java
src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java

index df70c7e..e75c2c4 100644 (file)
@@ -1354,17 +1354,10 @@ public abstract class AbstractInvokerMojo
 \r
         SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();\r
 \r
-        FileWriter fileWriter = null;\r
-        try\r
+        \r
+        try ( FileWriter fileWriter = new FileWriter( mergedSettingsFile ) )\r
         {\r
-            fileWriter = new FileWriter( mergedSettingsFile );\r
             settingsWriter.write( fileWriter, mergedSettings );\r
-            fileWriter.close();\r
-            fileWriter = null;\r
-        }\r
-        finally\r
-        {\r
-            IOUtil.close( fileWriter );\r
         }\r
 \r
         if ( getLog().isDebugEnabled() )\r
@@ -1704,20 +1697,12 @@ public abstract class AbstractInvokerMojo
         }\r
 \r
         File reportFile = new File( reportsDirectory, "BUILD-" + safeFileName + ".xml" );\r
-        try\r
+        try ( FileOutputStream fos = new FileOutputStream( reportFile );\r
+             Writer osw = new OutputStreamWriter( fos, buildJob.getModelEncoding() ) )\r
         {\r
-            FileOutputStream fos = new FileOutputStream( reportFile );\r
-            try\r
-            {\r
-                Writer osw = new OutputStreamWriter( fos, buildJob.getModelEncoding() );\r
-                BuildJobXpp3Writer writer = new BuildJobXpp3Writer();\r
-                writer.write( osw, buildJob );\r
-                osw.close();\r
-            }\r
-            finally\r
-            {\r
-                fos.close();\r
-            }\r
+            BuildJobXpp3Writer writer = new BuildJobXpp3Writer();\r
+            writer.write( osw, buildJob );\r
+            osw.close();\r
         }\r
         catch ( IOException e )\r
         {\r
@@ -2020,25 +2005,17 @@ public abstract class AbstractInvokerMojo
 \r
         if ( propertiesFile != null && propertiesFile.isFile() )\r
         {\r
-            InputStream fin = null;\r
-            try\r
+            \r
+            try ( InputStream fin = new FileInputStream( propertiesFile ) )\r
             {\r
-                fin = new FileInputStream( propertiesFile );\r
-\r
                 Properties loadedProperties = new Properties();\r
                 loadedProperties.load( fin );\r
-                fin.close();\r
-                fin = null;\r
                 collectedTestProperties.putAll( loadedProperties );\r
             }\r
             catch ( IOException e )\r
             {\r
                 throw new MojoExecutionException( "Error reading system properties from " + propertiesFile );\r
             }\r
-            finally\r
-            {\r
-                IOUtil.close( fin );\r
-            }\r
         }\r
 \r
         return collectedTestProperties;\r
@@ -2441,24 +2418,16 @@ public abstract class AbstractInvokerMojo
         throws IOException\r
     {\r
         List<String> result = new ArrayList<String>();\r
+        \r
+        Map<String, Object> composite = getInterpolationValueSource( false );\r
 \r
-        BufferedReader reader = null;\r
-        try\r
+        try ( BufferedReader reader =\r
+            new BufferedReader( new InterpolationFilterReader( newReader( tokenFile ), composite ) ) )\r
         {\r
-            Map<String, Object> composite = getInterpolationValueSource( false );\r
-            reader = new BufferedReader( new InterpolationFilterReader( newReader( tokenFile ), composite ) );\r
-\r
             for ( String line = reader.readLine(); line != null; line = reader.readLine() )\r
             {\r
                 result.addAll( collectListFromCSV( line ) );\r
             }\r
-\r
-            reader.close();\r
-            reader = null;\r
-        }\r
-        finally\r
-        {\r
-            IOUtil.close( reader );\r
         }\r
 \r
         return result;\r
@@ -2509,36 +2478,20 @@ public abstract class AbstractInvokerMojo
         {\r
             String xml;\r
 \r
-            Reader reader = null;\r
-            try\r
-            {\r
-                // interpolation with token @...@\r
-                Map<String, Object> composite = getInterpolationValueSource( true );\r
-                reader =\r
-                    new InterpolationFilterReader( ReaderFactory.newXmlReader( originalFile ), composite, "@", "@" );\r
+            Map<String, Object> composite = getInterpolationValueSource( true );\r
 \r
-                xml = IOUtil.toString( reader );\r
-\r
-                reader.close();\r
-                reader = null;\r
-            }\r
-            finally\r
+            // interpolation with token @...@\r
+            try ( Reader reader =\r
+                new InterpolationFilterReader( ReaderFactory.newXmlReader( originalFile ), composite, "@", "@" ) )\r
             {\r
-                IOUtil.close( reader );\r
+                xml = IOUtil.toString( reader );\r
             }\r
-\r
-            Writer writer = null;\r
-            try\r
+            \r
+            try ( Writer writer = WriterFactory.newXmlWriter( interpolatedFile ) )\r
             {\r
                 interpolatedFile.getParentFile().mkdirs();\r
-                writer = WriterFactory.newXmlWriter( interpolatedFile );\r
+                \r
                 writer.write( xml );\r
-                writer.close();\r
-                writer = null;\r
-            }\r
-            finally\r
-            {\r
-                IOUtil.close( writer );\r
             }\r
         }\r
         catch ( IOException e )\r
index 95caf97..35eb5b2 100644 (file)
@@ -41,6 +41,7 @@ import org.apache.maven.reporting.MavenReportException;
 import org.codehaus.plexus.i18n.I18N;\r
 import org.codehaus.plexus.util.ReaderFactory;\r
 import org.codehaus.plexus.util.StringUtils;\r
+import org.codehaus.plexus.util.xml.XmlStreamReader;\r
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;\r
 \r
 /**\r
@@ -136,13 +137,14 @@ public class InvokerReport
             return;\r
         }\r
 \r
+        BuildJobXpp3Reader buildJobReader = new BuildJobXpp3Reader();\r
+\r
         List<BuildJob> buildJobs = new ArrayList<BuildJob>( reportFiles.length );\r
         for ( File reportFile : reportFiles )\r
         {\r
-            try\r
+            try ( XmlStreamReader xmlReader = ReaderFactory.newXmlReader( reportFile ) )\r
             {\r
-                BuildJobXpp3Reader reader = new BuildJobXpp3Reader();\r
-                buildJobs.add( reader.read( ReaderFactory.newXmlReader( reportFile ) ) );\r
+                buildJobs.add( buildJobReader.read( xmlReader ) );\r
             }\r
             catch ( XmlPullParserException e )\r
             {\r
index 23ce866..726af15 100644 (file)
@@ -31,7 +31,6 @@ import java.util.Set;
 import java.util.TimeZone;\r
 \r
 import org.apache.maven.artifact.Artifact;\r
-import org.codehaus.plexus.util.IOUtil;\r
 import org.codehaus.plexus.util.ReaderFactory;\r
 import org.codehaus.plexus.util.WriterFactory;\r
 import org.codehaus.plexus.util.xml.Xpp3Dom;\r
@@ -144,24 +143,15 @@ class MetadataUtils
         {\r
             return null;\r
         }\r
-\r
-        Reader reader = null;\r
-        try\r
+        \r
+        try ( Reader reader = ReaderFactory.newXmlReader( metadataFile ) )\r
         {\r
-            reader = ReaderFactory.newXmlReader( metadataFile );\r
-            final Xpp3Dom xpp3Dom = Xpp3DomBuilder.build( reader );\r
-            reader.close();\r
-            reader = null;\r
-            return xpp3Dom;\r
+            return Xpp3DomBuilder.build( reader );\r
         }\r
         catch ( XmlPullParserException e )\r
         {\r
             throw (IOException) new IOException( e.getMessage() ).initCause( e );\r
         }\r
-        finally\r
-        {\r
-            IOUtil.close( reader );\r
-        }\r
     }\r
 \r
     private static void writeMetadata( File metadataFile, Xpp3Dom metadata )\r
@@ -169,17 +159,9 @@ class MetadataUtils
     {\r
         metadataFile.getParentFile().mkdirs();\r
 \r
-        Writer writer = null;\r
-        try\r
+        try ( Writer writer = WriterFactory.newXmlWriter( metadataFile ) )\r
         {\r
-            writer = WriterFactory.newXmlWriter( metadataFile );\r
             Xpp3DomWriter.write( writer, metadata );\r
-            writer.close();\r
-            writer = null;\r
-        }\r
-        finally\r
-        {\r
-            IOUtil.close( writer );\r
         }\r
     }\r
 \r
index 5130947..83eba4e 100644 (file)
@@ -26,7 +26,6 @@ import java.io.Reader;
 import org.apache.maven.model.Model;\r
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;\r
 import org.apache.maven.plugin.MojoExecutionException;\r
-import org.codehaus.plexus.util.IOUtil;\r
 import org.codehaus.plexus.util.ReaderFactory;\r
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;\r
 \r
@@ -48,14 +47,9 @@ class PomUtils
     public static Model loadPom( File pomFile )\r
         throws MojoExecutionException\r
     {\r
-        Reader reader = null;\r
-        try\r
+        try ( Reader reader = ReaderFactory.newXmlReader( pomFile ) )\r
         {\r
-            reader = ReaderFactory.newXmlReader( pomFile );\r
-            final Model model = new MavenXpp3Reader().read( reader, false );\r
-            reader.close();\r
-            reader = null;\r
-            return model;\r
+            return new MavenXpp3Reader().read( reader, false );\r
         }\r
         catch ( XmlPullParserException e )\r
         {\r
@@ -65,10 +59,6 @@ class PomUtils
         {\r
             throw new MojoExecutionException( "Failed to read POM: " + pomFile, e );\r
         }\r
-        finally\r
-        {\r
-            IOUtil.close( reader );\r
-        }\r
     }\r
 \r
 }\r
index 6feb0a4..5592f3d 100644 (file)
@@ -31,6 +31,7 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 \r
 import java.io.File;\r
 import java.io.IOException;\r
+import java.io.Reader;\r
 \r
 /**\r
  * Checks the results of maven-invoker-plugin based integration tests and fails the build if any tests failed.\r
@@ -111,13 +112,15 @@ public class VerifyMojo
             return;\r
         }\r
 \r
+        BuildJobXpp3Reader reader = new BuildJobXpp3Reader();\r
+\r
         InvokerSession invokerSession = new InvokerSession();\r
+\r
         for ( File reportFile : reportFiles )\r
         {\r
-            try\r
+            try ( Reader xmlReader = ReaderFactory.newXmlReader( reportFile ) )\r
             {\r
-                BuildJobXpp3Reader reader = new BuildJobXpp3Reader();\r
-                invokerSession.addJob( reader.read( ReaderFactory.newXmlReader( reportFile ) ) );\r
+                invokerSession.addJob( reader.read( xmlReader ) );\r
             }\r
             catch ( XmlPullParserException e )\r
             {\r
index 8740338..b58766c 100644 (file)
@@ -268,6 +268,12 @@ public class InvokerPropertiesTest
         assertFalse( facade.isInvocationDefined( 1 ) );\r
         assertTrue( facade.isInvocationDefined( 2 ) );\r
         assertFalse( facade.isInvocationDefined( 3 ) );\r
+\r
+        props.setProperty( "invoker.goals.3", "install" );\r
+        assertFalse( facade.isInvocationDefined( 1 ) );\r
+        assertTrue( facade.isInvocationDefined( 2 ) );\r
+        assertTrue( facade.isInvocationDefined( 3 ) );\r
+        assertFalse( facade.isInvocationDefined( 4 ) );\r
     }\r
     \r
     public void testIsSelectedDefined()\r