You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2020/06/30 16:49:15 UTC

[maven-dependency-plugin] 01/01: use try with resources

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

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

commit 1cf70340cc7761e2ccb34cc31cee60427070e0ce
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Tue Jun 30 12:48:52 2020 -0400

    use try with resources
---
 .../dependency/analyze/AnalyzeDuplicateMojo.java   | 18 +++++---------
 .../fromDependencies/BuildClasspathMojo.java       | 28 ++++------------------
 .../plugins/dependency/utils/DependencyUtil.java   | 25 ++++++-------------
 3 files changed, 17 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..79cb9ca 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
@@ -1,5 +1,7 @@
 package org.apache.maven.plugins.dependency.analyze;
 
+import java.io.IOException;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -37,8 +39,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 +87,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 )
-        {
-            throw new MojoExecutionException( "IOException: " + e.getMessage(), e );
         }
-        finally
+        catch ( IOException | XmlPullParserException e )
         {
-            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 )
     {