You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by pg...@apache.org on 2013/05/13 22:06:09 UTC

svn commit: r1482068 - in /maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency: resolvers/ResolveDependenciesMojo.java utils/DependencyStatusSets.java

Author: pgier
Date: Mon May 13 20:06:08 2013
New Revision: 1482068

URL: http://svn.apache.org/r1482068
Log:
[MDEP-414] Add option to sort output of dependency:resolve

Modified:
    maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/resolvers/ResolveDependenciesMojo.java
    maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/utils/DependencyStatusSets.java

Modified: maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/resolvers/ResolveDependenciesMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/resolvers/ResolveDependenciesMojo.java?rev=1482068&r1=1482067&r2=1482068&view=diff
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/resolvers/ResolveDependenciesMojo.java (original)
+++ maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/resolvers/ResolveDependenciesMojo.java Mon May 13 20:06:08 2013
@@ -59,6 +59,15 @@ public class ResolveDependenciesMojo
     DependencyStatusSets results;
 
     /**
+     * Sort the output list of resolved artifacts alphabetically.
+     * The default ordering matches the classpath order.
+     * 
+     * @since 2.9
+     */
+    @Parameter( property = "sort", defaultValue = "false" )
+    boolean sort;
+
+    /**
      * 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.
@@ -69,7 +78,7 @@ public class ResolveDependenciesMojo
         // get sets of dependencies
         results = this.getDependencySets( false );
 
-        String output = results.getOutput( outputAbsoluteArtifactFilename, outputScope );
+        String output = results.getOutput( outputAbsoluteArtifactFilename, outputScope, sort );
         try
         {
             if ( outputFile == null )

Modified: maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/utils/DependencyStatusSets.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/utils/DependencyStatusSets.java?rev=1482068&r1=1482067&r2=1482068&view=diff
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/utils/DependencyStatusSets.java (original)
+++ maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/utils/DependencyStatusSets.java Mon May 13 20:06:08 2013
@@ -23,7 +23,10 @@ package org.apache.maven.plugin.dependen
  *
  */
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
@@ -141,6 +144,11 @@ public class DependencyStatusSets
 
     public String getOutput( boolean outputAbsoluteArtifactFilename, boolean outputScope )
     {
+        return getOutput(outputAbsoluteArtifactFilename, outputScope, false);
+    }
+
+    public String getOutput( boolean outputAbsoluteArtifactFilename, boolean outputScope, boolean sort )
+    {
         StringBuilder sb = new StringBuilder();
         sb.append( "\n" );
         sb.append( "The following files have been resolved:\n" );
@@ -150,27 +158,7 @@ public class DependencyStatusSets
         }
         else
         {
-            for ( Artifact artifact : resolvedDependencies )
-            {
-                String artifactFilename = null;
-                if ( outputAbsoluteArtifactFilename )
-                {
-                    try
-                    {
-                        // we want to print the absolute file name here
-                        artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
-                    }
-                    catch ( NullPointerException e )
-                    {
-                        // ignore the null pointer, we'll output a null string
-                        artifactFilename = null;
-                    }
-                }
-
-                String id = outputScope ? artifact.toString() : artifact.getId();
-
-                sb.append( "   " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" ) + "\n" );
-            }
+            sb.append( buildArtifactListOutput( resolvedDependencies, outputAbsoluteArtifactFilename, outputScope, sort ) );
         }
 
         if ( this.skippedDependencies != null && !this.skippedDependencies.isEmpty() )
@@ -179,10 +167,7 @@ public class DependencyStatusSets
             sb.append( "The following files were skipped:\n" );
             Set<Artifact> skippedDependencies = new LinkedHashSet<Artifact>();
             skippedDependencies.addAll( this.skippedDependencies );
-            for ( Artifact artifact : skippedDependencies )
-            {
-                sb.append( "   " + artifact.getId() + "\n" );
-            }
+            sb.append( buildArtifactListOutput( skippedDependencies, outputAbsoluteArtifactFilename, outputScope, sort ) );
         }
 
         if ( this.unResolvedDependencies != null && !this.unResolvedDependencies.isEmpty() )
@@ -191,13 +176,46 @@ public class DependencyStatusSets
             sb.append( "The following files have NOT been resolved:\n" );
             Set<Artifact> unResolvedDependencies = new LinkedHashSet<Artifact>();
             unResolvedDependencies.addAll( this.unResolvedDependencies );
-            for ( Artifact artifact : unResolvedDependencies )
-            {
-                sb.append( "   " + artifact.getId() + "\n" );
-            }
+            sb.append( buildArtifactListOutput( unResolvedDependencies, outputAbsoluteArtifactFilename, outputScope, sort ) );
         }
         sb.append( "\n" );
 
         return sb.toString();
     }
+
+    private StringBuilder buildArtifactListOutput(Set<Artifact> artifacts,  boolean outputAbsoluteArtifactFilename, boolean outputScope, boolean sort )
+    {
+        StringBuilder sb = new StringBuilder();
+        List<String> artifactStringList = new ArrayList<String>();
+        for ( Artifact artifact : artifacts )
+        {
+            String artifactFilename = null;
+            if ( outputAbsoluteArtifactFilename )
+            {
+                try
+                {
+                    // we want to print the absolute file name here
+                    artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
+                }
+                catch ( NullPointerException e )
+                {
+                    // ignore the null pointer, we'll output a null string
+                    artifactFilename = null;
+                }
+            }
+
+            String id = outputScope ? artifact.toString() : artifact.getId();
+
+            artifactStringList.add( "   " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" ) + "\n" );
+        }
+        if ( sort )
+        {
+            Collections.sort( artifactStringList );
+        }
+        for (String artifactString : artifactStringList)
+        {
+            sb.append( artifactString );
+        }
+        return sb;
+    }
 }