mavenizing时的Velocity ResourceNotFoundException

时间:2014-07-30 09:42:30

标签: maven-plugin velocity

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

我想将模板路径设置为相对于类路径并执行maven命令。我在stackoverflow查询中尝试了一切,但我想我肯定错过了成功运行 maven 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的片段。

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

我将模板设为

Template body = Velocity.getTemplate(xmlparsepath);

其中xmlparsepath = XMLParse.vm

预期的程序流程:我需要从Executor项目运行** mvn clean exec:java test **,它应该执行Generator项目(在执行程序中生成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.

提前全部谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

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