Skip to content

Commit

Permalink
[ARCHETYPE-667] Upgrade Velocity from 1.7 to 2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Aug 19, 2024
1 parent cc90667 commit 04247f1
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 31 deletions.
2 changes: 1 addition & 1 deletion archetype-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
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;
Expand Down Expand Up @@ -449,7 +448,7 @@ private boolean maybeVelocityExpression(String value) {

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;
Expand Down Expand Up @@ -705,8 +704,8 @@ private boolean processTemplate(

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ public void testArchetype() throws Exception {

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);
}
Expand Down
2 changes: 1 addition & 1 deletion maven-archetype-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
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;
Expand All @@ -64,6 +64,7 @@
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")
Expand All @@ -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.
*/
Expand Down Expand Up @@ -278,11 +282,10 @@ public void configureArchetype(
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
Expand Down Expand Up @@ -349,9 +352,9 @@ private Map<String, Set<String>> computePropertyReferences() {
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);

Expand Down
20 changes: 3 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit 04247f1

Please sign in to comment.