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

mybatis-plus-generate中VelocityEngine初始化失败问题_第1张图片
当前逆向工具的类加载器和当前线程的类加载其竟然不是同一个,也是神奇了。

你可能感兴趣的:(Java)