mybatis-plus-generate中VelocityEngine初始化失败问题

mybatis-plus-generate包中提供了逆向数据库的功能,其中用到了velocity模板引擎技术。但是发现直接main函数中运行逆向代码完全没问题,一旦我想打成jar发布运行就抛出如下异常。

[Thread-5] ERROR org.apache.velocity - The specified class for ResourceManager (org.apache.velocity.runtime.resource.ResourceManagerImpl) does not implement org.apache.velocity.runtime.resource.ResourceManager; Velocity is not initialized correctly.
[Thread-5] ERROR org.apache.velocity - Could not auto-initialize Velocity
org.apache.velocity.exception.VelocityException: The specified class for ResourceManager (org.apache.velocity.runtime.resource.ResourceManagerImpl) does not implement org.apache.velocity.runtime.resource.ResourceManager; Velocity is not initialized correctly.
	at org.apache.velocity.runtime.RuntimeInstance.initializeResourceManager(RuntimeInstance.java:762)
	at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:240)
	at org.apache.velocity.runtime.RuntimeInstance.requireInitialization(RuntimeInstance.java:327)
	at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1551)
	at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:320)
	at com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine.writer(VelocityTemplateEngine.java:67)
	at com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine.batchOutput(AbstractTemplateEngine.java:99)
	at com.baomidou.mybatisplus.generator.AutoGenerator.execute(AutoGenerator.java:99)
	at com.advance.mybatis.plugin.reverse.MpGenerator.generate(MpGenerator.java:240)
	at com.advance.mybatis.plugin.ui.GenerateDetailConfigDialog$3.run(GenerateDetailConfigDialog.java:254)
	at java.lang.Thread.run(Thread.java:745)
[Thread-5] ERROR com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 无法创建文件,请检查配置信息!
java.lang.RuntimeException: Velocity could not be initialized!
	at org.apache.velocity.runtime.RuntimeInstance.requireInitialization(RuntimeInstance.java:332)
	at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1551)
	at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:320)
	at com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine.writer(VelocityTemplateEngine.java:67)
	at com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine.batchOutput(AbstractTemplateEngine.java:99)
	at com.baomidou.mybatisplus.generator.AutoGenerator.execute(AutoGenerator.java:99)
	at com.advance.mybatis.plugin.reverse.MpGenerator.generate(MpGenerator.java:240)
	at com.advance.mybatis.plugin.ui.GenerateDetailConfigDialog$3.run(GenerateDetailConfigDialog.java:254)
	at java.lang.Thread.run(Thread.java:745)

可以发现并不是模板文件找不到,而是初始化就已经失败。
最终在stackoverflow找到了类似的问题ResourceManager class mismatch when using Velocity as OSGi bundle
按照这个解决方案在逆向前后设置类加载器,问题就解决了。

final ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(MpGenerator.class.getClassLoader());
        
mpg.execute();

Thread.currentThread().setContextClassLoader(oldContextClassLoader);

在这里插入图片描述
当前逆向工具的类加载器和当前线程的类加载其竟然不是同一个,也是神奇了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值