Velocity无法找到资源

时间:2017-06-09 06:46:24

标签: apache maven velocity

这个问题已经被问到了,但我正在尝试接受答案的同样的事情

protected static final String RESOURCE_LOADER = classpath.resource.loader.class";    

static {
    System.out.println("Velocity Initialization Started");
    velocityEngine = new VelocityEngine();
    velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    velocityEngine.setProperty(RESOURCE_LOADER,ClasspathResourceLoader.class.getName());
    try {
        velocityEngine.init();
    } catch (Exception e) {

        LOG.error("Failed to load velocity templates e={}", e); 
    }
}

我的速度文件在

src/main/resources/velocity/templates/command/name.vm

我按照命令

获取模板
template = velocityEngine.getTemplate("velocity/templates/command/GenericState.vm");

它在本地工作但是当它在jar中捆绑它不起作用时,我检查了它由velocity文件夹组成的jar

我正在使用velocity来生成java代码

我正在进行maven项目设置,而maven正在创建jar

1 个答案:

答案 0 :(得分:3)

尝试这种方式它应该有用。

velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "class,file");
    velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
    velocityEngine.setProperty("runtime.log.logsystem.log4j.logger", "VELLOGGER");
    velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    velocityEngine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
    velocityEngine.init();