You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2024/08/19 22:12:54 UTC

(maven-archetype) 01/01: [ARCHETYPE-667] Upgrade Velocity from 1.7 to 2.3

This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch ARCHETYPE-667
in repository https://gitbox.apache.org/repos/asf/maven-archetype.git

commit c47078b18ac47c42c617fbce2565806b649be470
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Tue Aug 20 00:12:35 2024 +0200

    [ARCHETYPE-667] Upgrade Velocity from 1.7 to 2.3
---
 archetype-common/pom.xml                           |  2 +-
 .../maven/archetype/VelocityConfigurator.java      | 65 ++++++++++++++++++++++
 .../DefaultFilesetArchetypeGenerator.java          |  7 +--
 .../apache/maven/archetype/old/ArchetypeTest.java  |  5 +-
 maven-archetype-plugin/pom.xml                     |  2 +-
 .../DefaultArchetypeGenerationConfigurator.java    | 17 +++---
 pom.xml                                            | 20 +------
 7 files changed, 87 insertions(+), 31 deletions(-)

diff --git a/archetype-common/pom.xml b/archetype-common/pom.xml
index 27f8bfb9..44d923b0 100644
--- a/archetype-common/pom.xml
+++ b/archetype-common/pom.xml
@@ -115,7 +115,7 @@
     </dependency>
     <dependency>
       <groupId>org.apache.velocity</groupId>
-      <artifactId>velocity</artifactId>
+      <artifactId>velocity-engine-core</artifactId>
     </dependency>
     <dependency>
       <groupId>org.apache.maven.wagon</groupId>
diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/VelocityConfigurator.java b/archetype-common/src/main/java/org/apache/maven/archetype/VelocityConfigurator.java
new file mode 100644
index 00000000..43ebf03d
--- /dev/null
+++ b/archetype-common/src/main/java/org/apache/maven/archetype/VelocityConfigurator.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.archetype;
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import java.util.Properties;
+
+import org.codehaus.plexus.velocity.VelocityComponentConfigurator;
+
+/**
+ * Velocity configurator. Used by {@link org.codehaus.plexus.velocity.VelocityComponent}
+ * <p/>
+ * Preserve compatibility of Velocity 2.2 with Velocity 1.x
+ * <a href="https://velocity.apache.org/engine/2.3/upgrading.html">Velocity Upgrading</a>
+ */
+@Named
+@Singleton
+public class VelocityConfigurator implements VelocityComponentConfigurator {
+
+    @Override
+    public void configure(Properties properties) {
+
+        // # No automatic conversion of methods arguments
+        properties.put("introspector.conversion_handler.class", "none");
+
+        // # Use backward compatible space gobbling
+        properties.put("parser.space_gobbling", "bc");
+
+        // # Have #if($foo) only returns false if $foo is false or null
+        properties.put("directive.if.empty_check", false);
+
+        // # Allow '-' in identifiers (since 2.1)
+        properties.put("parser.allow_hyphen_in_identifiers", true);
+
+        // # Enable backward compatibility mode for Velocimacros
+        properties.put("velocimacro.enable_bc_mode", true);
+
+        // # When using an invalid reference handler, also include quiet references (since 2.2)
+        properties.put("event_handler.invalid_references.quiet", "true");
+
+        // # When using an invalid reference handler, also include null references (since 2.2)
+        properties.put("event_handler.invalid_references.null", true);
+
+        // # When using an invalid reference handler, also include tested references (since 2.2)
+        properties.put("event_handler.invalid_references.tested", true);
+    }
+}
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 3263cf2b..15239199 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
@@ -63,7 +63,6 @@ import org.apache.maven.archetype.metadata.FileSet;
 import org.apache.maven.archetype.metadata.ModuleDescriptor;
 import org.apache.maven.archetype.metadata.RequiredProperty;
 import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.Velocity;
 import org.apache.velocity.context.Context;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 import org.codehaus.plexus.util.FileUtils;
@@ -449,7 +448,7 @@ public class DefaultFilesetArchetypeGenerator extends AbstractLogEnabled impleme
 
     private String evaluateExpression(Context context, String key, String value) {
         try (StringWriter stringWriter = new StringWriter()) {
-            Velocity.evaluate(context, stringWriter, key, value);
+            velocity.getEngine().evaluate(context, stringWriter, key, value);
             return stringWriter.toString();
         } catch (Exception ex) {
             return value;
@@ -705,8 +704,8 @@ public class DefaultFilesetArchetypeGenerator extends AbstractLogEnabled impleme
 
         String localTemplateFileName = templateFileName.replace('/', File.separatorChar);
         if (!templateFileName.equals(localTemplateFileName)
-                && !velocity.getEngine().templateExists(templateFileName)
-                && velocity.getEngine().templateExists(localTemplateFileName)) {
+                && !velocity.getEngine().resourceExists(templateFileName)
+                && velocity.getEngine().resourceExists(localTemplateFileName)) {
             templateFileName = localTemplateFileName;
         }
 
diff --git a/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java b/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java
index e9ab3a78..2ad2d7cc 100644
--- a/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java
+++ b/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java
@@ -162,7 +162,10 @@ public class ArchetypeTest extends PlexusTestCase {
 
             velocity.getEngine()
                     .mergeTemplate(
-                            OldArchetype.ARCHETYPE_RESOURCES + "/" + OldArchetype.ARCHETYPE_POM, context, writer);
+                            OldArchetype.ARCHETYPE_RESOURCES + "/" + OldArchetype.ARCHETYPE_POM,
+                            "utf-8",
+                            context,
+                            writer);
         } finally {
             Thread.currentThread().setContextClassLoader(old);
         }
diff --git a/maven-archetype-plugin/pom.xml b/maven-archetype-plugin/pom.xml
index f2f6aeac..0560ae8a 100644
--- a/maven-archetype-plugin/pom.xml
+++ b/maven-archetype-plugin/pom.xml
@@ -134,7 +134,7 @@
     </dependency>
     <dependency>
       <groupId>org.apache.velocity</groupId>
-      <artifactId>velocity</artifactId>
+      <artifactId>velocity-engine-core</artifactId>
     </dependency>
     <dependency>
       <groupId>commons-collections</groupId>
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 120f3a7d..621aafd2 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
@@ -51,8 +51,8 @@ import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
 import org.apache.maven.artifact.repository.MavenArtifactRepository;
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.Velocity;
 import org.apache.velocity.context.Context;
 import org.apache.velocity.context.InternalContextAdapterImpl;
 import org.apache.velocity.runtime.RuntimeServices;
@@ -64,6 +64,7 @@ import org.apache.velocity.runtime.visitor.BaseVisitor;
 import org.codehaus.plexus.components.interactivity.PrompterException;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.velocity.VelocityComponent;
 
 // TODO: this seems to have more responsibilities than just a configurator
 @Named("default")
@@ -82,6 +83,9 @@ public class DefaultArchetypeGenerationConfigurator extends AbstractLogEnabled
     @Inject
     private ArchetypeGenerationQueryer archetypeGenerationQueryer;
 
+    @Inject
+    private VelocityComponent velocity;
+
     /**
      * Determines whether the layout is legacy or not.
      */
@@ -278,11 +282,10 @@ public class DefaultArchetypeGenerationConfigurator extends AbstractLogEnabled
         request.setProperties(properties);
     }
 
-    private static String expandEmbeddedTemplateExpressions(
-            String originalText, String textDescription, Context context) {
+    private String expandEmbeddedTemplateExpressions(String originalText, String textDescription, Context context) {
         if (StringUtils.contains(originalText, "${")) {
             try (StringWriter target = new StringWriter()) {
-                Velocity.evaluate(context, target, textDescription, originalText);
+                velocity.getEngine().evaluate(context, target, textDescription, originalText);
                 return target.toString();
             } catch (IOException ex) {
                 // closing StringWriter shouldn't actually generate any exception
@@ -349,9 +352,9 @@ public class DefaultArchetypeGenerationConfigurator extends AbstractLogEnabled
                 String defaultValue = archetypeConfiguration.getDefaultValue(propertyName);
                 if (StringUtils.contains(defaultValue, "${")) {
                     try {
-                        final boolean dumpNamespace = false;
-                        SimpleNode node = RuntimeSingleton.parse(
-                                new StringReader(defaultValue), propertyName + ".default", dumpNamespace);
+                        Template template = new Template();
+                        template.setName(propertyName + ".default");
+                        SimpleNode node = RuntimeSingleton.parse(new StringReader(defaultValue), template);
 
                         node.init(velocityContextAdapter, velocityRuntime);
 
diff --git a/pom.xml b/pom.xml
index c4457cfa..f0faca59 100644
--- a/pom.xml
+++ b/pom.xml
@@ -174,21 +174,7 @@
       <dependency>
         <groupId>org.codehaus.plexus</groupId>
         <artifactId>plexus-velocity</artifactId>
-        <version>1.2</version>
-        <exclusions>
-          <exclusion>
-            <groupId>velocity</groupId>
-            <artifactId>velocity</artifactId>
-          </exclusion>
-          <exclusion>
-            <groupId>velocity</groupId>
-            <artifactId>velocity-api</artifactId>
-          </exclusion>
-          <exclusion>
-            <groupId>org.codehaus.plexus</groupId>
-            <artifactId>plexus-container-default</artifactId>
-          </exclusion>
-        </exclusions>
+        <version>2.1.0</version>
       </dependency>
       <dependency>
         <groupId>org.jdom</groupId>
@@ -197,8 +183,8 @@
       </dependency>
       <dependency>
         <groupId>org.apache.velocity</groupId>
-        <artifactId>velocity</artifactId>
-        <version>1.7</version>
+        <artifactId>velocity-engine-core</artifactId>
+        <version>2.3</version>
       </dependency>
       <dependency>
         <groupId>commons-collections</groupId>