You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2012/02/26 19:45:24 UTC

svn commit: r1293908 - /maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java

Author: olamy
Date: Sun Feb 26 18:45:23 2012
New Revision: 1293908

URL: http://svn.apache.org/viewvc?rev=1293908&view=rev
Log:
[ARCHETYPE-397] Required property default value is not displayed correctly if a Velocity expression
Submitted by Emmanuel Hugonnet.

Modified:
    maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java

Modified: maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java
URL: http://svn.apache.org/viewvc/maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java?rev=1293908&r1=1293907&r2=1293908&view=diff
==============================================================================
--- maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java (original)
+++ maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java Sun Feb 26 18:45:23 2012
@@ -32,15 +32,18 @@ import org.apache.maven.archetype.ui.Arc
 import org.apache.maven.archetype.ui.ArchetypeDefinition;
 import org.apache.maven.archetype.ui.ArchetypeFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
-
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.context.Context;
 import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.component.annotations.Requirement;
 import org.codehaus.plexus.components.interactivity.PrompterException;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
 
 import java.io.IOException;
-
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
@@ -101,9 +104,8 @@ public class DefaultArchetypeGenerationC
         }
         if ( request.getArchetypeRepository() != null )
         {
-            archetypeRepository =
-                archetypeRegistryManager.createRepository( request.getArchetypeRepository(),
-                                                           ad.getArtifactId() + "-repo" );
+            archetypeRepository = archetypeRegistryManager.createRepository( request.getArchetypeRepository(),
+                                                                             ad.getArtifactId() + "-repo" );
             repositories.add( archetypeRepository );
         }
         if ( request.getRemoteArtifactRepositories() != null )
@@ -114,8 +116,9 @@ public class DefaultArchetypeGenerationC
         if ( !archetypeArtifactManager.exists( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(),
                                                archetypeRepository, localRepository, repositories ) )
         {
-            throw new UnknownArchetype( "The desired archetype does not exist (" + ad.getGroupId() + ":"
-                + ad.getArtifactId() + ":" + ad.getVersion() + ")" );
+            throw new UnknownArchetype(
+                "The desired archetype does not exist (" + ad.getGroupId() + ":" + ad.getArtifactId() + ":"
+                    + ad.getVersion() + ")" );
         }
 
         request.setArchetypeVersion( ad.getVersion() );
@@ -150,7 +153,10 @@ public class DefaultArchetypeGenerationC
         if ( interactiveMode.booleanValue() )
         {
             boolean confirmed = false;
-
+            Context context = new VelocityContext();
+            context.put( Constants.GROUP_ID, ad.getGroupId() );
+            context.put( Constants.ARTIFACT_ID, ad.getArtifactId() );
+            context.put( Constants.VERSION, ad.getVersion() );
             while ( !confirmed )
             {
                 List<String> propertiesRequired = archetypeConfiguration.getRequiredProperties();
@@ -170,32 +176,41 @@ public class DefaultArchetypeGenerationC
                                 // use its default and if not defined,
                                 // use the 'groupId' property value.
                                 String packageDefault = archetypeConfiguration.getDefaultValue( requiredProperty );
-                                packageDefault =
-                                    ( null == packageDefault || "".equals( packageDefault ) ) ? archetypeConfiguration.getProperty( "groupId" )
-                                                    : archetypeConfiguration.getDefaultValue( requiredProperty );
-
-                                String value = getTransitiveDefaultValue( packageDefault, archetypeConfiguration );
+                                packageDefault = ( null == packageDefault || "".equals( packageDefault ) )
+                                    ? archetypeConfiguration.getProperty( "groupId" )
+                                    : archetypeConfiguration.getDefaultValue( requiredProperty );
+
+                                String value =
+                                    getTransitiveDefaultValue( packageDefault, archetypeConfiguration, requiredProperty,
+                                                               context );
 
                                 value = archetypeGenerationQueryer.getPropertyValue( requiredProperty, value );
 
                                 archetypeConfiguration.setProperty( requiredProperty, value );
+
+                                context.put( Constants.PACKAGE, value );
                             }
                             else
                             {
                                 String value = archetypeConfiguration.getDefaultValue( requiredProperty );
 
-                                value = getTransitiveDefaultValue( value, archetypeConfiguration );
+                                value = getTransitiveDefaultValue( value, archetypeConfiguration, requiredProperty,
+                                                                   context );
 
                                 value = archetypeGenerationQueryer.getPropertyValue( requiredProperty, value );
 
                                 archetypeConfiguration.setProperty( requiredProperty, value );
+
+                                context.put( requiredProperty, value );
                             }
                         }
                         else
                         {
                             getLogger().info(
-                                              "Using property: " + requiredProperty + " = "
-                                                  + archetypeConfiguration.getProperty( requiredProperty ) );
+                                "Using property: " + requiredProperty + " = " + archetypeConfiguration.getProperty(
+                                    requiredProperty ) );
+                            archetypeConfiguration.setProperty( requiredProperty, archetypeConfiguration.getProperty(
+                                requiredProperty ) );
                         }
                     }
                 }
@@ -205,8 +220,8 @@ public class DefaultArchetypeGenerationC
                     for ( String requiredProperty : propertiesRequired )
                     {
                         getLogger().info(
-                                          "Using property: " + requiredProperty + " = "
-                                              + archetypeConfiguration.getProperty( requiredProperty ) );
+                            "Using property: " + requiredProperty + " = " + archetypeConfiguration.getProperty(
+                                requiredProperty ) );
                     }
                 }
 
@@ -234,11 +249,11 @@ public class DefaultArchetypeGenerationC
             {
                 for ( String requiredProperty : archetypeConfiguration.getRequiredProperties() )
                 {
-                    if ( !archetypeConfiguration.isConfigured( requiredProperty )
-                        && ( archetypeConfiguration.getDefaultValue( requiredProperty ) != null ) )
+                    if ( !archetypeConfiguration.isConfigured( requiredProperty ) && (
+                        archetypeConfiguration.getDefaultValue( requiredProperty ) != null ) )
                     {
-                        archetypeConfiguration.setProperty( requiredProperty,
-                                                            archetypeConfiguration.getDefaultValue( requiredProperty ) );
+                        archetypeConfiguration.setProperty( requiredProperty, archetypeConfiguration.getDefaultValue(
+                            requiredProperty ) );
                     }
                 }
 
@@ -263,8 +278,7 @@ public class DefaultArchetypeGenerationC
                             exceptionMessage.append( requiredProperty );
                             missingProperties.add( requiredProperty );
                             exceptionMessage.append( " is missing." );
-                            getLogger().warn(
-                                              "Property " + requiredProperty + " is missing. Add -D" + requiredProperty
+                            getLogger().warn( "Property " + requiredProperty + " is missing. Add -D" + requiredProperty
                                                   + "=someValue" );
                         }
                     }
@@ -287,7 +301,8 @@ public class DefaultArchetypeGenerationC
         request.setProperties( properties );
     }
 
-    private String getTransitiveDefaultValue( String defaultValue, ArchetypeConfiguration archetypeConfiguration )
+    private String getTransitiveDefaultValue( String defaultValue, ArchetypeConfiguration archetypeConfiguration,
+                                              String requiredProperty, Context context )
     {
         String result = defaultValue;
         if ( null == result )
@@ -298,13 +313,38 @@ public class DefaultArchetypeGenerationC
         {
             if ( result.indexOf( "${" + property + "}" ) >= 0 )
             {
+
                 result = StringUtils.replace( result, "${" + property + "}",
                                               archetypeConfiguration.getProperty( property ) );
             }
         }
+        if ( result.contains( "${" ) )
+        {
+            result = evaluateProperty( context, requiredProperty, defaultValue );
+        }
         return result;
     }
 
+
+    private String evaluateProperty( Context context, String property, String value )
+    {
+        StringWriter stringWriter = new StringWriter();
+        try
+        {
+            Velocity.evaluate( context, stringWriter, property, value );
+            return stringWriter.toString();
+        }
+        catch ( Exception ex )
+        {
+            return value;
+        }
+        finally
+        {
+            IOUtil.close( stringWriter );
+        }
+    }
+
+
     private void restoreCommandLineProperties( ArchetypeConfiguration archetypeConfiguration,
                                                Properties executionProperties )
     {