简体   繁体   中英

Velocity ResourceNotFoundException in IntelliJ IDEA

I have a Velocity template ValueTmpl.vm which can't be found by the Velocity ResourceManager. A minimal example:

package generate;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

public class Generate {
    public static void main(String[] args) throws Exception {
        VelocityContext context = new VelocityContext();
        context.put("key", "value");
        Writer writer = new FileWriter(new File("Result.java"));
        createTemplate("generate/templates/ValueTmpl.vm").merge(context, writer);
        writer.close();
    }

    private static Template createTemplate(String vmTemplateFile) {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(Velocity.RESOURCE_LOADER, "class");
        ve.setProperty("class.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        ve.init();
        return ve.getTemplate(vmTemplateFile);
    }
}

The generate folder is in the root of the src directory. I get the following error:

21.03.2011 13:09:01 org.apache.velocity.runtime.log.JdkLogChute log
SEVERE: ResourceManager : unable to find resource 'generate/templates/ValueTmpl.vm' in any resource loader.
Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'generate/templates/ValueTmpl.vm'

Does someone know what the problem can be? Should I change something in the project settings?

Thanks in advance!

I run into this all the time.

Open Settings->Compiler and add "?*.vm" to the list of resources. That way it'll be copied into your /out directories. Make sure the root of the path to the .vm files is marked as source in your project, and the class loader should find it.

Move the /generate folder out from under /src; put it at the same level as /src. Mark it as "source" in your module settings and try it again by starting the access path at "templates".

I know its been asked long back. But couldnt stop myself from posting as it took me quite a while in figuring out whats hapening with intellij idea.

With intellj idea, velocity is trying to find the template file in "Project" folder instead of "Module" class path. I am pretty sure i might be short of some configuration in intellij idea to make it look into module class path. Nevertheless, if you copy the velocity template to intelli idea project folder everything will work.

Agree thats a dumb thing to do but so far couldnt figure out a way to configure intelij idea otherwise. Anyone, any pointers ?

https://stackoverflow.com/a/38812523/8113460

I put my .vm under the src/main/resources/templates , then the code is :

Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init( p );       
VelocityContext context = new VelocityContext();           
Template template = Velocity.getTemplate("templates/my.vm");

this works in web project.

Okay, now it works after removing everything related to the VelocityEngine, so it's just something like

Template t0 = Velocity.getTemplate("src/generate/templates/ValueTmpl.vm");
t0.merge(context, writer);

Thanks to Jason Dusek .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM