MyBatis generator自定义插件或者扩展报Cannot instantiate object of type XXX
错误说明
Mybatis generator确实好用,但是离我们的生产代码还是有差别的。比如缺少toString
hashCode
equals
等方法,或者自定义的一些注释,作者信息等。
官方文档也说可以自定义插件,但是当我们继承或者实现mybatis-generator
的类或者接口时,运行报错:
Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate (default-cli) on project damon: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate failed: Cannot instantiate object of type cn.edu.zua.damon.util.MyDefaultCommentGenerator -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate (default-cli) on project damon: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate failed: Cannot instantiate object of type cn.edu.zua.damon.util.MyDefaultCommentGenerator
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
at org.codehaus.classworlds.Launcher.main (Launcher.java:47)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate failed: Cannot instantiate object of type cn.edu.zua.damon.util.MyDefaultCommentGenerator
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:145)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
经过耐心的搜索和寻找,大致确定了错误。错误就是mybatis-generator
的plugin
有自己的classpath
,我们在项目中直接继承的类和plugin
不属于同一个classpath
。这一点在官方文档有一点提到:http://mybatis.org/generator/running/runningWithMaven.html
Classpath Issues
Initially, the plugin classpath is very limited - it only contains MyBatis generator itself. If you need to add something to the plugin's classpath (for example, a JDBC driver)
但是官方文档并没有明确说明,当我们自定义的插件该放到哪里。
到这里,问题就解决了:我们需要把自己写的插件封装成一个jar,或者安装到本地,或者deploy至项目仓库,然后在plugin中添加依赖
解决方案
方案一
在plugin的pom中添加jar依赖:
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>F:/xxx/lib/xxx.jar</systemPath>
</dependency>
配置示例:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<dependencies>
<dependency>
<groupId>com.taozipay</groupId>
<artifactId>union-pay-dao</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>E:/Library/MavenRepository/com/taozipay/union-pay-dao/1.0.0-SNAPSHOT/union-pay-dao-1.0.0-SNAPSHOT.jar</systemPath>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
方案二
安装jar到本地仓库:
mvn install:install-file -DgroupId=com.taozipay -DartifactId=union-pay-dao -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -Dfile=E:/Library/MavenRepository/union-pay-dao-1.0.0-SNAPSHOT.jar -DgeneratePom=true
然后再在plugin的pom中添加jar依赖:
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0</version>
</dependency>
方案三
安装jar到项目:
mvn install:install-file -DgroupId=com.taozipay -DartifactId=union-pay-dao -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -Dfile=E:\Library\MavenRepository\com\taozipay\union-pay-dao\1.0.0-SNAPSHOT\union-pay-dao-1.0.0-SNAPSHOT.jar -DgeneratePom=true -DlocalRepositoryPath=E:\union-pay\union-pay-dao\src\main\resources\lib2\
然后再在plugin的pom中添加jar依赖:
<build>
<plugins>
<plugin>
<repositories>
<repository>
<id>in-project-repo</id>
<releases>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<url>file://${project.basedir}/repo</url>
</repository>
</repositories>
...
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0</version>
</dependency>
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/25/mybatis-generator-custom-plugin-or-extension-reports-cannot-instantiate-object-of-type-xxx/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论