You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2010/09/12 11:22:03 UTC
svn commit: r996275 -
/maven/archetype/trunk/archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java
Author: hboutemy
Date: Sun Sep 12 09:22:02 2010
New Revision: 996275
URL: http://svn.apache.org/viewvc?rev=996275&view=rev
Log:
[ARCHETYPE-325] ordered versions when selecting archetype
Modified:
maven/archetype/trunk/archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java
Modified: maven/archetype/trunk/archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java
URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java?rev=996275&r1=996274&r2=996275&view=diff
==============================================================================
--- maven/archetype/trunk/archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java (original)
+++ maven/archetype/trunk/archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java Sun Sep 12 09:22:02 2010
@@ -20,17 +20,21 @@ package org.apache.maven.archetype.ui;
*/
import org.apache.maven.archetype.catalog.Archetype;
+import org.apache.maven.artifact.versioning.ArtifactVersion;
+import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.codehaus.plexus.components.interactivity.Prompter;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.TreeMap;
/** @plexus.component */
public class DefaultArchetypeSelectionQueryer
@@ -63,10 +67,10 @@ public class DefaultArchetypeSelectionQu
{
StringBuilder query = new StringBuilder( "Choose archetype:\n" );
- Map<String, List<Archetype>> archetypeAnswerMap = new HashMap<String, List<Archetype>>();
- Map<String, String> reversedArchetypeAnswerMap = new HashMap<String, String>();
+ Set<String> archetypeKeys = new HashSet<String>();
List<String> answers = new ArrayList<String>();
- List<Archetype> archetypeVersions;
+ Map<String, Archetype> archetypeAnswerMap = new HashMap<String, Archetype>();
+
int counter = 1;
int defaultSelection = 0;
@@ -76,43 +80,36 @@ public class DefaultArchetypeSelectionQu
for ( Archetype archetype : entry.getValue() )
{
- String mapKey = String.valueOf( counter );
-
String archetypeKey = archetype.getGroupId() + ":" + archetype.getArtifactId();
- if ( reversedArchetypeAnswerMap.containsKey( archetypeKey ) )
+ if ( !archetypeKeys.add( archetypeKey ) )
{
- mapKey = reversedArchetypeAnswerMap.get( archetypeKey );
- archetypeVersions = archetypeAnswerMap.get( mapKey );
+ continue;
}
- else
+
+ String description = archetype.getDescription();
+ if ( description == null )
{
- archetypeVersions = new ArrayList<Archetype>();
- archetypeAnswerMap.put( mapKey, archetypeVersions );
- reversedArchetypeAnswerMap.put( archetypeKey, mapKey );
-
- String description = archetype.getDescription();
- if ( description == null )
- {
- description = "-";
- }
-
- query.append( mapKey + ": " + catalog + " -> " + archetype.getArtifactId() + " ("
- + description + ")\n" );
-
- answers.add( mapKey );
-
- // the version is not tested. This is intentional.
- if ( defaultDefinition != null && archetype.getGroupId().equals( defaultDefinition.getGroupId() )
- && archetype.getArtifactId().equals( defaultDefinition.getArtifactId() ) )
- {
- defaultSelection = counter;
- }
+ description = "-";
+ }
+
+ String answer = String.valueOf( counter );
- counter++;
+ query.append( answer + ": " + catalog + " -> " + archetype.getArtifactId() + " ("
+ + description + ")\n" );
+
+ answers.add( answer );
+
+ archetypeAnswerMap.put( answer, archetype );
+
+ // the version is not tested. This is intentional.
+ if ( defaultDefinition != null && archetype.getGroupId().equals( defaultDefinition.getGroupId() )
+ && archetype.getArtifactId().equals( defaultDefinition.getArtifactId() ) )
+ {
+ defaultSelection = counter;
}
- archetypeVersions.add( archetype );
+ counter++;
}
}
@@ -129,52 +126,68 @@ public class DefaultArchetypeSelectionQu
answer = prompter.prompt( query.toString(), answers, Integer.toString( defaultSelection ) );
}
- archetypeVersions = archetypeAnswerMap.get( answer );
+ Archetype selection = archetypeAnswerMap.get( answer );
- if ( archetypeVersions.size() == 1 )
- {
- return archetypeVersions.get( 0 );
- }
- else
- {
- return selectVersion( archetypeVersions );
- }
+ return selectVersion( catalogs, selection.getGroupId(), selection.getArtifactId() );
}
- private Archetype selectVersion( List<Archetype> archetypes )
+ private Archetype selectVersion( Map<String, List<Archetype>> catalogs, String groupId, String artifactId )
throws PrompterException
{
- StringBuilder query = new StringBuilder( "Choose version: \n" );
+ SortedMap<ArtifactVersion, Archetype> archetypeVersionsMap = new TreeMap<ArtifactVersion, Archetype>();
- Map<String, Archetype> answerMap = new HashMap<String, Archetype>();
- List<String> answers = new ArrayList<String>();
-
- Collections.sort( archetypes, new Comparator<Archetype>()
+ for ( Map.Entry<String, List<Archetype>> entry : catalogs.entrySet() )
{
- public int compare( Archetype a1, Archetype a2 )
+ for ( Archetype archetype : entry.getValue() )
{
- return a1.getVersion().compareTo( a2.getVersion() );
+ if ( !groupId.equals( archetype.getGroupId() ) || !artifactId.equals( archetype.getArtifactId() ) )
+ {
+ continue;
+ }
+
+ ArtifactVersion version = new DefaultArtifactVersion( archetype.getVersion() );
+
+ // don't override the first catalog containing a defined version of the artifact
+ if ( !archetypeVersionsMap.containsKey( version ) )
+ {
+ archetypeVersionsMap.put( version, archetype );
+ }
}
- } );
+ }
+
+ if ( archetypeVersionsMap.size() == 1 )
+ {
+ return archetypeVersionsMap.values().iterator().next();
+ }
+
+ // let the user choose between available versions
+ StringBuilder query = new StringBuilder( "Choose version: \n" );
+
+ List<String> answers = new ArrayList<String>();
+ Map<String, Archetype> answerMap = new HashMap<String, Archetype>();
int counter = 1;
- for ( Archetype archetype : archetypes )
+ String mapKey = null;
+
+ for ( Map.Entry<ArtifactVersion, Archetype> entry : archetypeVersionsMap.entrySet() )
{
- String mapKey = String.valueOf( counter );
- String archetypeVersion = archetype.getVersion();
+ ArtifactVersion version = entry.getKey();
+ Archetype archetype = entry.getValue();
- answerMap.put( mapKey, archetype );
+ mapKey = String.valueOf( counter );
- query.append( mapKey + ": " + archetypeVersion + "\n" );
+ query.append( mapKey + ": " + version + "\n" );
answers.add( mapKey );
+ answerMap.put( mapKey, archetype );
+
counter++;
}
query.append( "Choose a number: " );
- String answer = prompter.prompt( query.toString(), answers );
+ String answer = prompter.prompt( query.toString(), answers, mapKey );
return answerMap.get( answer );
}