Velocity Unable To find Resources

做~自己de王妃 提交于 2019-12-24 00:56:14

问题


This question is already asked but i am trying the same thing that is in accepted answer

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

my velocity file is in

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

i am getting templates by following command

template = velocityEngine.getTemplate("velocity/templates/command/GenericState.vm");

It works locally but when bundled in jar it does not work , I have examined the jar it consist of velocity folder

i am using velocity to generated java code

I am having maven project setup and maven is creating jar


回答1:


try this way it should work.

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();


来源:https://stackoverflow.com/questions/44451069/velocity-unable-to-find-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!