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/12/27 20:39:21 UTC

[maven-dependency-plugin] branch master updated: [MDEP-698] replace code that relies on default encoding (#119)

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 a3f10c5  [MDEP-698] replace code that relies on default encoding (#119)
a3f10c5 is described below

commit a3f10c51381fafcfdae9560e1135bea5ae5363ac
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Sun Dec 27 15:39:10 2020 -0500

    [MDEP-698] replace code that relies on default encoding (#119)
    
    * replace code that relies on default encoding
    read encoding from Maven project
    use @Parameter
---
 .../fromDependencies/BuildClasspathMojo.java       | 40 ++++++++++++-------
 .../resolvers/ResolveDependenciesMojo.java         | 13 ++++--
 .../dependency/resolvers/ResolvePluginsMojo.java   | 14 +++++--
 .../maven/plugins/dependency/tree/TreeMojo.java    | 11 ++++--
 .../plugins/dependency/utils/DependencyUtil.java   | 46 ++++++++++++++++------
 5 files changed, 85 insertions(+), 39 deletions(-)

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 223e725..b391fdd 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
@@ -22,14 +22,17 @@ package org.apache.maven.plugins.dependency.fromDependencies;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -49,7 +52,7 @@ import org.apache.maven.shared.transfer.repository.RepositoryManager;
 import org.codehaus.plexus.util.StringUtils;
 
 /**
- * This goal will output a classpath string of dependencies from the local repository to a file or log.
+ * This goal outputs a classpath string of dependencies from the local repository to a file or log.
  *
  * @author ankostis
  * @since 2.0-alpha-2
@@ -62,6 +65,9 @@ public class BuildClasspathMojo
     implements Comparator<Artifact>
 {
 
+    @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
+    private String outputEncoding;
+    
     /**
      * Strip artifact version during copy (only works if prefix is set)
      */
@@ -239,7 +245,7 @@ public class BuildClasspathMojo
         }
         else
         {
-            if ( regenerateFile || !isUpdToDate( cpString ) )
+            if ( regenerateFile || !isUpToDate( cpString ) )
             {
                 storeClasspathFile( cpString, outputFile );
             }
@@ -301,18 +307,17 @@ public class BuildClasspathMojo
     /**
      * Checks that new classpath differs from that found inside the old classpathFile.
      *
-     * @param cpString
-     * @return true if the specified classpath equals to that found inside the file, false otherwise (including when
-     *         file does not exists but new classpath does).
+     * @return true if the specified classpath equals the one found inside the file, false otherwise (including when
+     *         file does not exist but new classpath does).
      */
-    private boolean isUpdToDate( String cpString )
+    private boolean isUpToDate( String cpString )
     {
         try
         {
             String oldCp = readClasspathFile();
             return ( cpString == null ? oldCp == null : cpString.equals( oldCp ) );
         }
-        catch ( Exception ex )
+        catch ( IOException ex )
         {
             this.getLog().warn( "Error while reading old classpath file '" + outputFile + "' for up-to-date check: "
                 + ex );
@@ -322,18 +327,20 @@ public class BuildClasspathMojo
     }
 
     /**
-     * It stores the specified string into that file.
+     * Stores the specified string into that file.
      *
-     * @param cpString the string to be written into the file.
-     * @throws MojoExecutionException
+     * @param cpString the string to write into the file
      */
     private void storeClasspathFile( String cpString, File out )
         throws MojoExecutionException
     {
         // make sure the parent path exists.
         out.getParentFile().mkdirs();
+        
+        String encoding = Objects.toString( outputEncoding, "UTF-8" );
 
-        try ( Writer w = new BufferedWriter( new FileWriter( out ) ) )
+        try ( Writer w =
+            new BufferedWriter( new OutputStreamWriter( new FileOutputStream( out ), encoding ) ) )
         {
             w.write( cpString );
             getLog().info( "Wrote classpath file '" + out + "'." );
@@ -346,7 +353,7 @@ public class BuildClasspathMojo
     }
 
     /**
-     * Reads into a string the file specified by the mojo param 'outputFile'. Assumes, the instance variable
+     * Reads the file specified by the mojo param 'outputFile' into a string. Assumes the field
      * 'outputFile' is not null.
      * 
      * @return the string contained in the classpathFile, if it exists, or null otherwise
@@ -366,7 +373,10 @@ public class BuildClasspathMojo
             return null;
         }
         StringBuilder sb = new StringBuilder();
-        try ( BufferedReader r = new BufferedReader( new FileReader( outputFile ) ) )
+        String encoding = Objects.toString( outputEncoding, "UTF-8" );
+
+        try ( BufferedReader r =
+            new BufferedReader( new InputStreamReader( new FileInputStream( outputFile ), encoding ) ) )
         {
             for ( String line = r.readLine(); line != null; line = r.readLine() )
             {
diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
index e2131c4..7a6addd 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
@@ -42,6 +42,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
@@ -60,6 +61,9 @@ public class ResolveDependenciesMojo
     extends AbstractResolveMojo
 {
 
+    @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
+    private String outputEncoding;
+
     /**
      * If we should display the scope when resolving
      *
@@ -92,7 +96,7 @@ public class ResolveDependenciesMojo
     /**
      * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
      *
-     * @throws MojoExecutionException with a message if an error occurs.
+     * @throws MojoExecutionException with a message if an error occurs
      */
     @Override
     protected void doExecute()
@@ -109,8 +113,9 @@ public class ResolveDependenciesMojo
                 DependencyUtil.log( output, getLog() );
             }
             else
-            {
-                DependencyUtil.write( output, outputFile, appendOutput, getLog() );
+            {    
+                String encoding = Objects.toString( outputEncoding, "UTF-8" );
+                DependencyUtil.write( output, outputFile, appendOutput, encoding );
             }
         }
         catch ( IOException e )
@@ -120,7 +125,7 @@ public class ResolveDependenciesMojo
     }
 
     /**
-     * @return Returns the results.
+     * @return returns the results
      */
     public DependencyStatusSets getResults()
     {
diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
index 0f45e90..c40c427 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
@@ -21,12 +21,14 @@ package org.apache.maven.plugins.dependency.resolvers;
 
 import java.io.IOException;
 import java.util.LinkedHashSet;
+import java.util.Objects;
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
 import org.apache.maven.project.ProjectBuildingRequest;
 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
@@ -47,6 +49,9 @@ public class ResolvePluginsMojo
     extends AbstractResolveMojo
 {
 
+    @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
+    private String outputEncoding;
+
     /**
      * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
      *
@@ -136,7 +141,8 @@ public class ResolvePluginsMojo
                 }
                 else
                 {
-                    DependencyUtil.write( output, outputFile, appendOutput, getLog() );
+                    String encoding = Objects.toString( outputEncoding, "UTF-8" );
+                    DependencyUtil.write( output, outputFile, appendOutput, encoding );
                 }
             }
         }
@@ -149,9 +155,9 @@ public class ResolvePluginsMojo
     /**
      * This method resolves the plugin artifacts from the project.
      *
-     * @return set of resolved plugin artifacts.
-     * @throws ArtifactFilterException in case of an error.
-     * @throws ArtifactResolverException in case of an error.
+     * @return set of resolved plugin artifacts
+     * @throws ArtifactFilterException in case of an error
+     * @throws ArtifactResolverException in case of an error
      */
     protected Set<Artifact> resolvePluginArtifacts()
         throws ArtifactFilterException, ArtifactResolverException
diff --git a/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java b/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java
index 5f030c5..e2073c1 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java
@@ -67,11 +67,12 @@ import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * Displays the dependency tree for this project. Multiple formats are supported: text (by default), but also
  * <a href="https://en.wikipedia.org/wiki/DOT_language">DOT</a>,
- * <a href="https://en.wikipedia.org/wiki/GraphML">graphml</a> and
+ * <a href="https://en.wikipedia.org/wiki/GraphML">GraphML</a>, and
  * <a href="https://en.wikipedia.org/wiki/Trivial_Graph_Format">TGF</a>.
  *
  * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
@@ -91,6 +92,9 @@ public class TreeMojo
 
     @Parameter( defaultValue = "${session}", readonly = true, required = true )
     private MavenSession session;
+    
+    @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
+    private String outputEncoding;
 
     /**
      * Contains the full list of projects in the reactor.
@@ -286,7 +290,8 @@ public class TreeMojo
 
             if ( outputFile != null )
             {
-                DependencyUtil.write( dependencyTreeString, outputFile, this.appendOutput, getLog() );
+                String encoding = Objects.toString( outputEncoding, "UTF-8" );
+                DependencyUtil.write( dependencyTreeString, outputFile, this.appendOutput, encoding );
 
                 getLog().info( "Wrote dependency tree to: " + outputFile );
             }
@@ -301,7 +306,7 @@ public class TreeMojo
         }
         catch ( IOException exception )
         {
-            throw new MojoExecutionException( "Cannot serialise project dependency graph", exception );
+            throw new MojoExecutionException( "Cannot serialize project dependency graph", exception );
         }
     }
 
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 eacedbf..4c6b528 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
@@ -21,9 +21,11 @@ package org.apache.maven.plugins.dependency.utils;
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.StringReader;
+import java.io.Writer;
 import java.util.Objects;
 
 import org.apache.maven.artifact.Artifact;
@@ -32,7 +34,7 @@ import org.apache.maven.plugin.logging.Log;
 import org.codehaus.plexus.util.StringUtils;
 
 /**
- * Utility class with static helper methods
+ * Utility class with static helper methods.
  * 
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
  * @version $Id$
@@ -71,13 +73,13 @@ public final class DependencyUtil
 
     /**
      * Builds the file name. If removeVersion is set, then the file name must be reconstructed from the groupId (if
-     * <b>prependGroupId</b> is true) artifactId, Classifier (if used) and Type. Otherwise, this method returns the
+     * <b>prependGroupId</b> is true) artifactId, Classifier (if used), and Type. Otherwise, this method returns the
      * artifact file name.
      * 
-     * @param artifact File to be formatted.
-     * @param removeVersion Specifies if the version should be removed from the file name.
-     * @param prependGroupId Specifies if the groupId should be prepended to the file name.
-     * @param useBaseVersion Specifies if the baseVersion of the artifact should be used instead of the version.
+     * @param artifact file to be formatted
+     * @param removeVersion Specifies if the version should be removed from the file name
+     * @param prependGroupId Specifies if the groupId should be prepended to the file name
+     * @param useBaseVersion Specifies if the baseVersion of the artifact should be used instead of the version
      * @return Formatted file name in the format [groupId].artifactId-[version]-[classifier].[type]
      */
     public static String getFormattedFileName( Artifact artifact, boolean removeVersion, boolean prependGroupId,
@@ -219,16 +221,33 @@ public final class DependencyUtil
      * 
      * @param string the string to write
      * @param file the file to write to
-     * @param append append to existing file or not.
-     * @param log where to send the logging output.
+     * @param append append to existing file or not
+     * @param log ignored
      * @throws IOException if an I/O error occurs
+     * @deprecated specify an encoding instead of a log
      */
+    @Deprecated
     public static synchronized void write( String string, File file, boolean append, Log log )
         throws IOException
     {
+        write( string, file, append, "UTF-8" );
+    }
+    
+    /**
+     * Writes the specified string to the specified file.
+     * 
+     * @param string the string to write
+     * @param file the file to write to
+     * @param append append to existing file or not
+     * @param encoding character set name
+     * @throws IOException if an I/O error occurs
+     */
+    public static synchronized void write( String string, File file, boolean append, String encoding )
+        throws IOException
+    {
         file.getParentFile().mkdirs(); 
 
-        try ( FileWriter writer = new FileWriter( file, append ) )
+        try ( Writer writer = new OutputStreamWriter( new FileOutputStream( file, append ), encoding ) )
         {
             writer.write( string );
         }
@@ -257,9 +276,10 @@ public final class DependencyUtil
     }
 
     /**
-     * mainly used to parse excludes,includes configuration
-     * @param str The string to be split.
-     * @return The result items.
+     * Mainly used to parse excludes, includes configuration.
+     * 
+     * @param str the string to split
+     * @return the result items
      */
     public static String[] tokenizer( String str )
     {