[maven-dependency-plugin] branch master updated: use try with resources (#62)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[maven-dependency-plugin] branch master updated: use try with resources (#62)

elharo
This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 7309af7  use try with resources (#62)
7309af7 is described below

commit 7309af7666c18553f5720facac3ec5a95ef18988
Author: Elliotte Rusty Harold <[hidden email]>
AuthorDate: Tue Jun 30 17:55:53 2020 -0400

    use try with resources (#62)
   
    * use try with resources
   
    * order imports
---
 .../dependency/analyze/AnalyzeDuplicateMojo.java   | 17 ++++---------
 .../fromDependencies/BuildClasspathMojo.java       | 28 ++++------------------
 .../plugins/dependency/utils/DependencyUtil.java   | 25 ++++++-------------
 3 files changed, 16 insertions(+), 54 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeDuplicateMojo.java b/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeDuplicateMojo.java
index ee06f57..be1735d 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeDuplicateMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeDuplicateMojo.java
@@ -19,6 +19,7 @@ package org.apache.maven.plugins.dependency.analyze;
  * under the License.
  */
 
+import java.io.IOException;
 import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -37,8 +38,8 @@ import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.ReaderFactory;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
 /**
  * Analyzes the <code>&lt;dependencies/&gt;</code> and <code>&lt;dependencyManagement/&gt;</code> tags in the
@@ -85,21 +86,13 @@ public class AnalyzeDuplicateMojo
 
         MavenXpp3Reader pomReader = new MavenXpp3Reader();
         Model model = null;
-        Reader reader = null;
-        try
+        try ( Reader reader = ReaderFactory.newXmlReader( project.getFile() ) )
         {
-            reader = ReaderFactory.newXmlReader( project.getFile() );
             model = pomReader.read( reader );
-            reader.close();
-            reader = null;
         }
-        catch ( Exception e )
+        catch ( IOException | XmlPullParserException e )
         {
-            throw new MojoExecutionException( "IOException: " + e.getMessage(), e );
-        }
-        finally
-        {
-            IOUtil.close( reader );
+            throw new MojoExecutionException( "Exception: " + e.getMessage(), e );
         }
 
         Set<String> duplicateDependencies = Collections.emptySet();
diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java
index ea14b73..d055ad8 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java
@@ -45,7 +45,6 @@ import org.apache.maven.plugins.dependency.utils.DependencyUtil;
 import org.apache.maven.project.MavenProjectHelper;
 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
 import org.apache.maven.shared.transfer.repository.RepositoryManager;
-import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
 
 /**
@@ -332,13 +331,9 @@ public class BuildClasspathMojo
         // make sure the parent path exists.
         out.getParentFile().mkdirs();
 
-        Writer w = null;
-        try
+        try ( Writer w = new BufferedWriter( new FileWriter( out ) ) )
         {
-            w = new BufferedWriter( new FileWriter( out ) );
             w.write( cpString );
-            w.close();
-            w = null;
             getLog().info( "Wrote classpath file '" + out + "'." );
         }
         catch ( IOException ex )
@@ -346,18 +341,14 @@ public class BuildClasspathMojo
             throw new MojoExecutionException( "Error while writing to classpath file '" + out + "': " + ex.toString(),
                                               ex );
         }
-        finally
-        {
-            IOUtil.close( w );
-        }
     }
 
     /**
      * Reads into a string the file specified by the mojo param 'outputFile'. Assumes, the instance variable
      * 'outputFile' is not null.
      *
-     * @return the string contained in the classpathFile, if exists, or null otherwise.
-     * @throws IOException in case of an error.
+     * @return the string contained in the classpathFile, if it exists, or null otherwise
+     * @throws IOException in case of an error
      */
     protected String readClasspathFile()
         throws IOException
@@ -373,26 +364,15 @@ public class BuildClasspathMojo
             return null;
         }
         StringBuilder sb = new StringBuilder();
-        BufferedReader r = null;
-
-        try
+        try ( BufferedReader r = new BufferedReader( new FileReader( outputFile ) ) )
         {
-            r = new BufferedReader( new FileReader( outputFile ) );
-
             for ( String line = r.readLine(); line != null; line = r.readLine() )
             {
                 sb.append( line );
             }
 
-            r.close();
-            r = null;
-
             return sb.toString();
         }
-        finally
-        {
-            IOUtil.close( r );
-        }
     }
 
     /**
diff --git a/src/main/java/org/apache/maven/plugins/dependency/utils/DependencyUtil.java b/src/main/java/org/apache/maven/plugins/dependency/utils/DependencyUtil.java
index 5af5a42..eacedbf 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/utils/DependencyUtil.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/utils/DependencyUtil.java
@@ -29,7 +29,6 @@ import java.util.Objects;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.ArtifactUtils;
 import org.apache.maven.plugin.logging.Log;
-import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
 
 /**
@@ -227,22 +226,11 @@ public final class DependencyUtil
     public static synchronized void write( String string, File file, boolean append, Log log )
         throws IOException
     {
-        file.getParentFile().mkdirs();
+        file.getParentFile().mkdirs();
 
-        FileWriter writer = null;
-
-        try
+        try ( FileWriter writer = new FileWriter( file, append ) )
         {
-            writer = new FileWriter( file, append );
-
             writer.write( string );
-
-            writer.close();
-            writer = null;
-        }
-        finally
-        {
-            IOUtil.close( writer );
         }
     }
 
@@ -250,7 +238,7 @@ public final class DependencyUtil
      * Writes the specified string to the log at info level.
      *
      * @param string the string to write
-     * @param log where to log information.
+     * @param log where to log information
      * @throws IOException if an I/O error occurs
      */
     public static synchronized void log( String string, Log log )
@@ -279,9 +267,10 @@ public final class DependencyUtil
     }
 
     /**
-     * clean up configuration string before it can be tokenized
-     * @param str The str which should be cleaned.
-     * @return cleaned up string.
+     * Clean up configuration string before it can be tokenized.
+     *
+     * @param str the string which should be cleaned
+     * @return cleaned up string
      */
     public static String cleanToBeTokenizedString( String str )
     {