【问题标题】:Velocity ResourceNotFoundException when mavenizingmavenizing 时的 Velocity ResourceNotFoundException
【发布时间】:2014-07-30 09:42:30
【问题描述】:

我有两个名为 GeneratorExecutor 的项目(都是 maven 项目)。我使用 apache velocity 模板引擎来生成(在 Generator 中)并且生成的代码在 Executor 中执行。我希望代码从 Executor 项目运行。我已将 Generator 添加为依赖项,并且能够通过为执行时所需的模板文件指定绝对路径来成功运行。

我想将模板路径设置为相对于类路径并执行 maven 命令。我尝试了 stackoverflow 查询中所说的一切,但我想我肯定错过了成功运行 ma​​ven clean install 的东西。

VelocityEngine Velocity = new VelocityEngine();

Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); 
Velocity.setProperty("classpath.resource.loader.class",ClasspathResourceLoader.class.getName());

 Velocity.setProperty("classpath.resource.loader.path",templatepath);

上面是设置我的 Velocity Engine 的 sn-p。

templatepath = Templates\\ -> Set relative to classpath ( src/main/resources )

我得到的模板是

Template body = Velocity.getTemplate(xmlparsepath);

其中 xmlparsepath=XMLParse.vm

预期的程序流程:我需要从 Executor 项目运行 **mvn clean exec:java test **,该项目应该执行 Generator 项目(在 executor 中生成 junit 测试用例)并在 Executor 项目中运行测试用例。

我收到 ResourceNotFoundExceptions 说我无法指向模板文件。但是速度日志却另有说明。 Generator 项目中的 Velocity 日志如下。

2014-07-30 10:52:00,219 - Log4JLogChute initialized using file 'velocity.log'
2014-07-30 10:52:00,219 - Initializing Velocity, Calling init()...
2014-07-30 10:52:00,219 - Starting Apache Velocity v1.7 (compiled: 2010-11-19 12:14:37)
2014-07-30 10:52:00,219 - Default Properties File: org\apache\velocity\runtime\defaults\velocity.properties
2014-07-30 10:52:00,219 - Trying to use logger class org.apache.velocity.runtime.log.AvalonLogChute
2014-07-30 10:52:00,219 - Target log system for org.apache.velocity.runtime.log.AvalonLogChute is not available (java.lang.NoClassDefFoundError: org/apache/log/format/Formatter).  Falling back to next log system...
2014-07-30 10:52:00,219 - Trying to use logger class org.apache.velocity.runtime.log.Log4JLogChute
2014-07-30 10:52:00,219 - Using logger class org.apache.velocity.runtime.log.Log4JLogChute
2014-07-30 10:52:00,235 - ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2014-07-30 10:52:00,266 - ResourceCache: initialized (class org.apache.velocity.runtime.resource.ResourceCacheImpl) with class java.util.Collections$SynchronizedMap cache map.
2014-07-30 10:52:00,266 - Loaded System Directive: org.apache.velocity.runtime.directive.Stop
2014-07-30 10:52:00,266 - Loaded System Directive: org.apache.velocity.runtime.directive.Define
2014-07-30 10:52:00,266 - Loaded System Directive: org.apache.velocity.runtime.directive.Break
2014-07-30 10:52:00,266 - Loaded System Directive: org.apache.velocity.runtime.directive.Evaluate
2014-07-30 10:52:00,266 - Loaded System Directive: org.apache.velocity.runtime.directive.Literal
2014-07-30 10:52:00,266 - Loaded System Directive: org.apache.velocity.runtime.directive.Macro
2014-07-30 10:52:00,281 - Loaded System Directive: org.apache.velocity.runtime.directive.Parse
2014-07-30 10:52:00,281 - Loaded System Directive: org.apache.velocity.runtime.directive.Include
2014-07-30 10:52:00,281 - Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
2014-07-30 10:52:00,344 - Created '20' parsers.
2014-07-30 10:52:00,360 - Velocimacro : "velocimacro.library" is not set.  Trying default library: VM_global_library.vm
2014-07-30 10:52:00,360 - Could not load resource 'VM_global_library.vm' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader: ClasspathResourceLoader Error: cannot find resource VM_global_library.vm
2014-07-30 10:52:00,360 - Velocimacro : Default library not found.
2014-07-30 10:52:00,360 - Velocimacro : allowInline = true : VMs can be defined inline in templates
2014-07-30 10:52:00,360 - Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions
2014-07-30 10:52:00,360 - Velocimacro : allowInlineLocal = false : VMs defined inline will be global in scope if allowed.
2014-07-30 10:52:00,360 - Velocimacro : autoload off : VM system will not automatically reload global library macros
2014-07-30 10:52:00,391 - ResourceManager : found Templates\RootTemplate.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2014-07-30 10:52:00,391 - ResourceManager : found Templates\XMLParse.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2014-07-30 10:52:00,391 - ResourceManager : found Templates\XMLHeader.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2014-07-30 10:52:00,391 - ResourceManager : found Templates\XMLFooter.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2014-07-30 10:52:00,391 - ResourceManager : found Templates\\PackagesTemplate.vm with loader 

与 Executor Project 有关的速度日志。

2014-07-30 14:43:48,502 - Log4JLogChute initialized using file 'velocity.log'
2014-07-30 14:43:48,503 - Initializing Velocity, Calling init()...
2014-07-30 14:43:48,503 - Starting Apache Velocity v1.7 (compiled: 2010-11-19 12:14:37)
2014-07-30 14:43:48,503 - Default Properties File: org\apache\velocity\runtime\defaults\velocity.properties
2014-07-30 14:43:48,503 - Trying to use logger class org.apache.velocity.runtime.log.AvalonLogChute
2014-07-30 14:43:48,503 - Target log system for org.apache.velocity.runtime.log.AvalonLogChute is not available (java.lang.NoClassDefFoundError: org/apache/log/format/Formatter).  Falling back to next log system...
2014-07-30 14:43:48,503 - Trying to use logger class org.apache.velocity.runtime.log.Log4JLogChute
2014-07-30 14:43:48,503 - Using logger class org.apache.velocity.runtime.log.Log4JLogChute
2014-07-30 14:43:48,506 - ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2014-07-30 14:43:48,515 - ResourceCache: initialized (class org.apache.velocity.runtime.resource.ResourceCacheImpl) with class java.util.Collections$SynchronizedMap cache map.
2014-07-30 14:43:48,516 - Loaded System Directive: org.apache.velocity.runtime.directive.Stop
2014-07-30 14:43:48,517 - Loaded System Directive: org.apache.velocity.runtime.directive.Define
2014-07-30 14:43:48,518 - Loaded System Directive: org.apache.velocity.runtime.directive.Break
2014-07-30 14:43:48,518 - Loaded System Directive: org.apache.velocity.runtime.directive.Evaluate
2014-07-30 14:43:48,518 - Loaded System Directive: org.apache.velocity.runtime.directive.Literal
2014-07-30 14:43:48,519 - Loaded System Directive: org.apache.velocity.runtime.directive.Macro
2014-07-30 14:43:48,520 - Loaded System Directive: org.apache.velocity.runtime.directive.Parse
2014-07-30 14:43:48,521 - Loaded System Directive: org.apache.velocity.runtime.directive.Include
2014-07-30 14:43:48,522 - Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
2014-07-30 14:43:48,545 - Created '20' parsers.
2014-07-30 14:43:48,549 - Velocimacro : "velocimacro.library" is not set.  Trying default library: VM_global_library.vm
2014-07-30 14:43:48,549 - Could not load resource 'VM_global_library.vm' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader: ClasspathResourceLoader Error: cannot find resource VM_global_library.vm
2014-07-30 14:43:48,549 - Velocimacro : Default library not found.
2014-07-30 14:43:48,549 - Velocimacro : allowInline = true : VMs can be defined inline in templates
2014-07-30 14:43:48,549 - Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions
2014-07-30 14:43:48,549 - Velocimacro : allowInlineLocal = false : VMs defined inline will be global in scope if allowed.
2014-07-30 14:43:48,549 - Velocimacro : autoload off : VM system will not automatically reload global library macros
2014-07-30 14:43:48,552 - ResourceManager : unable to find resource 'RootTemplate.vm' in any resource loader.

提前谢谢大家。

【问题讨论】:

  • 我能够解决这个问题。请在下面找到它。无论如何,谢谢。

标签: maven-plugin velocity


【解决方案1】:

伙计们,我能够解决它。我没有使用 VM_global_library.vm。我们需要将模板加载到该字段中,如下所示

  prop.setProperty("velocimacro.library", "RootTemplate.vm" );

运行时引擎能够以这种方式获取模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多