简体   繁体   English

IntelliJ IDEA中的Velocity ResourceNotFoundException

[英]Velocity ResourceNotFoundException in IntelliJ IDEA

I have a Velocity template ValueTmpl.vm which can't be found by the Velocity ResourceManager. 我有一个Velocity模板ValueTmpl.vm ,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. generate文件夹位于src目录的根目录中。 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. 打开设置->编译器,然后在资源列表中添加“?*。vm”。 That way it'll be copied into your /out directories. 这样,它将被复制到您的/ out目录中。 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. 确保.vm文件路径的根在您的项目中被标记为源,并且类加载器应找到它。

Move the /generate folder out from under /src; 将/ generate文件夹从/ src下移出; put it at the same level as /src. 将其放在与/ 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. 但是无法阻止自己发布,因为花了我相当长的时间才弄清楚intellij的想法是怎么回事。

With intellj idea, velocity is trying to find the template file in "Project" folder instead of "Module" class path. 用intellj的想法,Velocity正在尝试在“ Project”文件夹中而不是“ Module”类路径中找到模板文件。 I am pretty sure i might be short of some configuration in intellij idea to make it look into module class path. 我很确定我可能在intellij的想法上缺少一些配置,以使其进入模块类路径。 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. 同意那是一件愚蠢的事情,但是到目前为止,还没有找到一种否则配置intelij想法的方法。 Anyone, any pointers ? 任何人,任何指针?

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

I put my .vm under the src/main/resources/templates , then the code is : 我将.vm放在src/main/resources/templates ,那么代码是:

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. 这适用于Web项目。

Okay, now it works after removing everything related to the VelocityEngine, so it's just something like 好的,在删除与VelocityEngine相关的所有内容后,它现在可以工作,所以就像

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

Thanks to Jason Dusek . 感谢Jason Dusek

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Velocity中的ResourceNotFoundException - ResourceNotFoundException in Velocity 快速加载模板时出现ResourceNotFoundException - ResourceNotFoundException while loading a template in velocity ResourceNotFoundException:用于spark-java和velocity - ResourceNotFoundException: for spark-java and velocity 速度问题-与Spring MVC一起使用时出现ResourceNotFoundException - Problem with Velocity - ResourceNotFoundException when using with Spring MVC Spark + Velocity:无法加载资源(ResourceNotFoundException) - Spark + Velocity: Unable to load resources (ResourceNotFoundException) org.apache.velocity.exception.ResourceNotFoundException - org.apache.velocity.exception.ResourceNotFoundException Apache velocity:org.apache.velocity.exception.ResourceNotFoundException - Apache velocity: org.apache.velocity.exception.ResourceNotFoundException Velocity模板-线程“ main”中的异常org.apache.velocity.exception.ResourceNotFoundException:无法找到资源 - Velocity Template - Exception in thread “main” org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 将IntelliJ IDEA 8用于IDEA 6项目 - Using IntelliJ IDEA 8 for IDEA 6 Projects 如何在模式生成期间修复“org.apache.velocity.exception.ResourceNotFoundException” - How to fix “org.apache.velocity.exception.ResourceNotFoundException” during schema generating
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM