From b96cb787b6d0f7c865388554e1a6df6b1c89eb3a Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Wed, 9 Oct 2024 19:56:49 +0200 Subject: [PATCH] [ARCHETYPE-688] Allow hyphen in requiredProperty --- .../DefaultArchetypeGenerationConfigurator.java | 10 ++++++---- .../generation/RequiredPropertyComparatorTest.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java b/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java index 4e1caf07..fc766d32 100644 --- a/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java +++ b/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java @@ -50,8 +50,7 @@ import org.apache.velocity.VelocityContext; import org.apache.velocity.context.Context; import org.apache.velocity.context.InternalContextAdapterImpl; -import org.apache.velocity.runtime.RuntimeServices; -import org.apache.velocity.runtime.RuntimeSingleton; +import org.apache.velocity.runtime.RuntimeInstance; import org.apache.velocity.runtime.parser.ParseException; import org.apache.velocity.runtime.parser.node.ASTNegateNode; import org.apache.velocity.runtime.parser.node.ASTReference; @@ -326,7 +325,10 @@ private Map> computePropertyReferences() { final InternalContextAdapterImpl velocityContextAdapter = new InternalContextAdapterImpl(new VelocityContext()); - final RuntimeServices velocityRuntime = RuntimeSingleton.getRuntimeServices(); + final RuntimeInstance velocityRuntime = new RuntimeInstance(); + Properties properties = new Properties(); + properties.put("parser.allow_hyphen_in_identifiers", true); + velocityRuntime.setProperties(properties); for (String propertyName : requiredProperties) { final Set referencedPropertyNames = new LinkedHashSet<>(); @@ -336,7 +338,7 @@ private Map> computePropertyReferences() { try { Template template = new Template(); template.setName(propertyName + ".default"); - SimpleNode node = RuntimeSingleton.parse(new StringReader(defaultValue), template); + SimpleNode node = velocityRuntime.parse(new StringReader(defaultValue), template); node.init(velocityContextAdapter, velocityRuntime); diff --git a/maven-archetype-plugin/src/test/java/org/apache/maven/archetype/ui/generation/RequiredPropertyComparatorTest.java b/maven-archetype-plugin/src/test/java/org/apache/maven/archetype/ui/generation/RequiredPropertyComparatorTest.java index 000648c5..ab2484db 100644 --- a/maven-archetype-plugin/src/test/java/org/apache/maven/archetype/ui/generation/RequiredPropertyComparatorTest.java +++ b/maven-archetype-plugin/src/test/java/org/apache/maven/archetype/ui/generation/RequiredPropertyComparatorTest.java @@ -77,4 +77,17 @@ public void testTransitivePropertyReferences() { requiredProperties.sort(requiredPropertyComparator); assertEquals(Arrays.asList("baz", "bar", "foo"), requiredProperties); } + + @Test + public void testPropertiesWithHyphen() { + archetypeConfiguration.addRequiredProperty("description"); + archetypeConfiguration.setDefaultProperty("description", "${acronym-app} project-arch: ${component-name}"); + archetypeConfiguration.addRequiredProperty("component-name"); + archetypeConfiguration.addRequiredProperty("acronym-app"); + + List requiredProperties = archetypeConfiguration.getRequiredProperties(); + assertEquals(Arrays.asList("description", "component-name", "acronym-app"), requiredProperties); + requiredProperties.sort(requiredPropertyComparator); + assertEquals(Arrays.asList("component-name", "acronym-app", "description"), requiredProperties); + } }