From 1afc7c764c73591609ce30bef86b38355b4f7426 Mon Sep 17 00:00:00 2001 From: sdan Date: Wed, 7 Aug 2019 08:49:17 +0300 Subject: [PATCH] archetype-574: Added fix to use default values on integration-test if parameter value not specified --- .../DefaultFilesetArchetypeGenerator.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java b/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java index b4d22c0aa..d7aa52130 100644 --- a/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java +++ b/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java @@ -132,7 +132,7 @@ public void generateArchetype( ArchetypeGenerationRequest request, File archetyp throw new ArchetypeNotConfigured( exceptionMessage.toString(), missingProperties ); } - Context context = prepareVelocityContext( request ); + Context context = prepareVelocityContext ( request, archetypeDescriptor ); String packageName = request.getPackage(); String artifactId = request.getArtifactId(); @@ -394,7 +394,8 @@ private boolean isArchetypeConfigured( ArchetypeDescriptor archetypeDescriptor, { for ( RequiredProperty requiredProperty : archetypeDescriptor.getRequiredProperties() ) { - if ( StringUtils.isEmpty( request.getProperties().getProperty( requiredProperty.getKey() ) ) ) + if ( StringUtils.isEmpty( request.getProperties().getProperty( requiredProperty.getKey() ) ) + && StringUtils.isEmpty( requiredProperty.getDefaultValue() ) ) { return false; } @@ -408,7 +409,7 @@ private void setParentArtifactId( Context context, String artifactId ) context.put( Constants.PARENT_ARTIFACT_ID, artifactId ); } - private Context prepareVelocityContext( ArchetypeGenerationRequest request ) + private Context prepareVelocityContext( ArchetypeGenerationRequest request, ArchetypeDescriptor archetypeDescript ) { Context context = new VelocityContext(); context.put( Constants.GROUP_ID, request.getGroupId() ); @@ -440,12 +441,21 @@ private Context prepareVelocityContext( ArchetypeGenerationRequest request ) String value = request.getProperties().getProperty( key ); + String defaultValue = getDefaultValue( key, archetypeDescript ); + if ( maybeVelocityExpression( value ) ) { value = evaluateExpression( context, key, value ); } - context.put( key, value ); + if ( StringUtils.isEmpty ( value ) ) + { + context.put( key, defaultValue ); + } + else + { + context.put( key, value ); + } if ( getLogger().isInfoEnabled() ) { @@ -455,6 +465,18 @@ private Context prepareVelocityContext( ArchetypeGenerationRequest request ) return context; } + private String getDefaultValue( String key, ArchetypeDescriptor archetypeDescriptor ) + { + for ( RequiredProperty property : archetypeDescriptor.getRequiredProperties() ) + { + if ( property.getKey().equals( key ) ) + { + return property.getDefaultValue(); + } + } + return null; + } + private boolean maybeVelocityExpression( String value ) { return value != null && value.contains( "${" );