How to properly configure Velocity with Spring Boot?

32,715

You should put your breakpoint at VelocityAutoConfiguration to ensure that Velocity is used.

Take a look at spring-boot-sample-velocity. Check the dependencies.

Share:
32,715
wMattDodd
Author by

wMattDodd

Updated on February 01, 2020

Comments

  • wMattDodd
    wMattDodd over 4 years

    I'm new to Spring Boot, and I'm having major trouble with this. There's a short list of Velocity-related Spring Boot properties I can set in application.properties, and those are working fine. But there's a huge number of Velocity properties that I can't configure that way.

    I found this question, that seems to address what I need, but it isn't working for me. When I use breakpoints within Spring Boot during program start-up, I can see the "spring.velocity.properties.*" key/value pairs being read and loaded by Spring Boot correctly--they just don't seem to affect anything. No matter what value I set them to, the Velocity run-time behavior uses the default anyway.

    What am I missing?

    EDIT:

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>docuvore</groupId>
    <artifactId>docuvore-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.BUILD-SNAPSHOT</version>
    </parent>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
    
        <!-- Core Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    
        <!-- Tomcat and Spring Web MVC -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <!-- Spring Security 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        -->
    
        <!-- Spring Data and MongoDB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
    
        <!-- Apache Velocity --> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-velocity</artifactId>
        </dependency>
    
        <!-- Project Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <!-- Additional lines to be added here... -->
    
    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
    </project>
    

    application.properties

    logging.path=/logs
    #spring.velocity.resourceLoaderPath = /templates/
    #spring.velocity.checkTemplateLocation=false
    spring.velocity.properties.template.provide.scope.control = true
    spring.velocity.properties.directive.parse.max.depth = 9
    spring.velocity.properties.runtime.log = C:/logs/velocity.log
    

    Application.java

    package com.github.docuvore.prototype;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration 
    @EnableAutoConfiguration
    @ComponentScan
    public class Application {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    }
    

    ExampleVelocityController.java

    package com.github.docuvore.prototype.examples;
    
    import java.io.StringWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.velocity.Template;
    import org.apache.velocity.VelocityContext;
    import org.apache.velocity.app.VelocityEngine;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ExampleVelocityController {
    
        @RequestMapping("/exampleVelocity")
        String home() {
            String result = null;
    
            VelocityEngine velocity = new VelocityEngine();
            velocity.init();
            Template template = velocity.getTemplate("src/main/resources/templates/general/htmlElement.vm");
    
            VelocityContext context = new VelocityContext();
            context.put("title", "Apache Velocity");
    
            StringWriter writer = new StringWriter();
            template.merge(context, writer);
    
            result = writer.toString();
    
            return result;
        }
    }
    

    htmlElement.vm

    <html>
    #parse ("src/main/resources/templates/general/bodyElement.vm")
    </html>
    

    bodyElement.vm

    <body>
    #parse ("src/main/resources/templates/general/bodyElement.vm")
    </body>
    
  • wMattDodd
    wMattDodd about 9 years
    Velocity is definitely being used, I can work with it just fine, and I can see the direct Spring Boot Velocity properties like spring.velocity.resourceLoaderPath or spring.velocity.checkTemplateLocation being used in the VelocityAutoConfiguration. The only thing I can't do is get Velocity configured by the spring.velocity.properties.[nativeVelocityProperty] style properties. Unfortunately that sample isn't any use, because it's not trying to do the same thing I am.
  • emecas
    emecas over 7 years
    A commit in the 2nd link says "Deprecate support for Velocity". More details here: github.com/spring-projects/spring-boot/commit/…