【问题标题】:Moved Velocity jar from Bundle-ClassPath to Import-Package(Plugin dependancies) in MANIFEST.MF Then what should be the place for .vm file?将 Velocity jar 从 Bundle-ClassPath 移动到 MANIFEST.MF 中的 Import-Package(Plugin dependencies) 那么 .vm 文件的位置应该是什么?
【发布时间】:2017-09-12 06:48:28
【问题描述】:

如上图的项目结构。 在代码中,

/* Define velocity engine and template */
VelocityEngine ve = new VelocityEngine();
ve.setProperty("resource.loader", "classpath");
ve.setProperty("classpath.resource.loader.class",ClasspathResourceLoader.class.getName());
ve.init();
Template t = ve.getTemplate("fileTemplates/DCM_Default.vm");

以前,velocity.jar 存在于 /lib 文件夹中。因此,DCM_Default.vm 找到了。MENIFEST.MF 在类路径中有如下条目,

Bundle-ClassPath: ., lib/velocity-1.7-dep.jar

现在,速度。 jar 从类路径中删除,它存在于 MENIFEST.MF 的插件依赖项中,有以下更改-

Import-Package: org.apache.velocity, org.apache.velocity.app, org.apache.velocity.context, org.apache.velocity.exception, org.apache.velocity.runtime, org.apache.velocity.runtime.resource.loader

我无法找到必须放置 .vm 的路径,因为我遇到了以下异常 原因:org.apache.velocity.exception.ResourceNotFoundException:找不到资源'fileTemplates/DCM_Default.vm'。

任何人都可以有任何想法吗?请提出建议。

【问题讨论】:

  • 明显找不到路径中的异常。您可以将fileTemplates 文件夹移动到src 文件夹中。
  • 感谢您的回复。我在 src/ 中移动了 fileTemplates/DCM_Default.vm 文件夹,但它没有用。我使用 RCP 作为客户端。
  • 你在使用maven项目吗?
  • 是的。我正在使用 Maven RCP 作为客户端项目。
  • 检查我的答案。

标签: java apache maven velocity


【解决方案1】:

如果您使用 ClasspathResourceLoader,那么在运行时您必须有一个包含 fileTemplates/DCM_Default.vm 的 jar。将它放在 src 目录下并不能保证它是类路径的一部分,这取决于您的 IDE(哪些文档应该告诉您如何操作)。

如果您知道模板的绝对路径,也可以使用 FileResourceLoader。

【讨论】:

    【解决方案2】:

    由于您符合 Maven 项目,您的 Velocity 文件应该在 resources 文件夹中

    你的结构应该像src-->main-->resources-->fileTemplates

    我总是使用以下配置来加载模板:

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

    【讨论】:

      【解决方案3】:

      我找到了如下解决方案-

             //Define template location 
              Bundle bundle = FrameworkUtil.getBundle(getClass());
              URL fileUrl = FileLocator.toFileURL(FileLocator.find(bundle, new Path('fileTemplates/'), null));`
      
              /* Define velocity engine and template */
              VelocityEngine ve = new VelocityEngine();
              ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, fileUrl.getPath());
              ve.init();
              Template t = ve.getTemplate("DCM_Default.vm");
      

      在路径中,我们需要在运行时计算的文件夹的绝对路径。 这是 RCP 客户端插件项目中的工作。

      【讨论】:

        猜你喜欢
        • 2010-12-24
        • 1970-01-01
        • 1970-01-01
        • 2012-09-08
        • 2015-04-25
        • 2015-12-09
        • 2015-05-19
        • 2019-01-10
        • 1970-01-01
        相关资源
        最近更新 更多