运行mybatis-generator-maven-plugin报错An API incompatibility was encountered while executing

Owen Jia 2018年10月17日 4,286次浏览

背景

集成mybatis-generator-maven-plugin时,出现的问题和找到的解决办法分享;

  • spring-boot-2.0.5
  • mybatis-generator-maven-plugin-1.3.7

分析了很多原因,最终推测是API不兼容问题导致;我本人总是喜欢用最新版本的工具,也确实容易出现不兼容问题。

问题现象

[INFO] --- mybatis-generator-maven-plugin:1.3.7:generate (default-cli) @ openapi-core ---
[INFO] Connecting to the Database
[INFO] Introspecting table o_test
[INFO] Generating Record class for table o_test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.091 s
[INFO] Finished at: 2018-10-11T13:18:51+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate (default-cli) on project openapi-core: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate failed: An API incompatibility was encountered while executing org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate: java.lang.AbstractMethodError: tk.mybatis.mapper.generator.MapperCommentGenerator.addModelClassComment(Lorg/mybatis/generator/api/dom/java/TopLevelClass;Lorg/mybatis/generator/api/IntrospectedTable;)V
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/F:/m2/repo/org/mybatis/generator/mybatis-generator-maven-plugin/1.3.7/mybatis-generator-maven-plugin-1.3.7.jar
[ERROR] urls[1] = file:/F:/m2/repo/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar
[ERROR] urls[2] = file:/F:/m2/repo/tk/mybatis/mapper/3.3.6/mapper-3.3.6.jar
[ERROR] urls[3] = file:/F:/m2/repo/javax/persistence/persistence-api/1.0/persistence-api-1.0.jar
[ERROR] urls[4] = file:/F:/m2/repo/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5.jar
[ERROR] urls[5] = file:/F:/m2/repo/javax/enterprise/cdi-api/1.0/cdi-api-1.0.jar
[ERROR] urls[6] = file:/F:/m2/repo/org/eclipse/sisu/org.eclipse.sisu.inject/0.3.3/org.eclipse.sisu.inject-0.3.3.jar
[ERROR] urls[7] = file:/F:/m2/repo/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar
[ERROR] urls[8] = file:/F:/m2/repo/org/codehaus/plexus/plexus-utils/3.1.0/plexus-utils-3.1.0.jar
[ERROR] urls[9] = file:/F:/m2/repo/org/mybatis/generator/mybatis-generator-core/1.3.7/mybatis-generator-core-1.3.7.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[maven.api, parent: null]]
[ERROR] 
[ERROR] -----------------------------------------------------
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException

Process finished with exit code 1

问题本质

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <configurationFile>${basedir}/src/test/resources/generator/generatorConfig.xml</configurationFile>
        <overwrite>true</overwrite>
        <verbose>true</verbose>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>${mapper.version}</version>
        </dependency>
    </dependencies>
</plugin>

解决办法

降低该插件版本
mybatis-generator-maven-plugin_1.3.2

推测是1.3.7版本去除了次方法: tk.mybatis.mapper.generator.MapperCommentGenerator.addModelClassComment;
所以插件运行一直提示:generate failed: An API incompatibility was encountered while executing
意思就是说:执行时出现一个API不兼容

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
</plugin>