将 Velocity jar 从 Bundle-ClassPath 移动到 MANIFEST.MF 中的 Import-Package(插件依赖项) 那么 .vm 文件应该放在哪里?
Moved Velocity jar from Bundle-ClassPath to Import-Package(Plugin dependancies) in MANIFEST.MF Then what should be the place for .vm file?
项目结构如上图。
在代码中,
/* 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 有 found.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'。
有谁知道吗?请提出建议。
如果您使用 ClasspathResourceLoader,那么在运行时您必须有一个包含 fileTemplates/DCM_Default.vm 的 jar。将它放在 src 目录下并不能保证它是类路径的一部分,这取决于你的 IDE(哪个文档应该告诉你如何这样做)。
如果您知道模板的绝对路径,也可以使用 FileResourceLoader。
既然你符合 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");
我找到了如下解决方案-
//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 客户端插件项目中的工作。
/* 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 有 found.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'。
有谁知道吗?请提出建议。
如果您使用 ClasspathResourceLoader,那么在运行时您必须有一个包含 fileTemplates/DCM_Default.vm 的 jar。将它放在 src 目录下并不能保证它是类路径的一部分,这取决于你的 IDE(哪个文档应该告诉你如何这样做)。
如果您知道模板的绝对路径,也可以使用 FileResourceLoader。
既然你符合 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");
我找到了如下解决方案-
//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 客户端插件项目中的工作。