Mybatis generator自定义插件或者扩展报Cannot instantiate object of type XXX

错误说明

Mybatis generator确实好用,但是离我们的生产代码还是有差别的。比如缺少toString hashCode equals等方法,或者自定义的一些注释,作者信息等。

官方文档也说可以自定义插件,但是当我们继承或者实现mybatis-generator的类或者接口时,运行报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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-generatorplugin有自己的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依赖:

1
2
3
4
5
6
7
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>F:/xxx/lib/xxx.jar</systemPath>
</dependency>

配置示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<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到本地仓库:

1
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依赖:

1
2
3
4
5
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0</version>
</dependency>

方案三

安装jar到项目:

1
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依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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>

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :