LoginSignup
6
7

More than 5 years have passed since last update.

GradleでMybatisGenerator実行した際にMybatisのカスタムプラグインを適用する

Last updated at Posted at 2017-07-01

GradleでMybatisGeneratorを実行する際に自前で用意したMybatisのカスタムプラグインを使おうとしたとき、かなりハマったので同じ問題で悩んでる方の参考になれば

前提

  • Gradle
  • マルチプロジェクト(シングルプロジェクトはまだ解決方法わかりません。。。)

前置き

MybatisGeneratorでカスタムプラグインを適用する前にGradleでMybatisGeneratorどうやってやんの???って方は先にこちらをどうぞ -> GradleからMybatisGenerator実行する

gradle + mybatisGeneratorでカスタムプラグインを指定して実行した結果

/* スタックトレース省略 */

:project-core:mybatisGenerator FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/username/Git/project/build.gradle' line: 259

* What went wrong:
Execution failed for task ':project-core:mybatisGenerator'.
> Cannot instantiate object of type project.core.plugin.mybatis.CustomPlugin

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.747 secs
Cannot instantiate object of type project.core.plugin.mybatis.CustomPlugin
17:01:38: External task execution finished 'mybatisGenerator'.

エラー内容が

Cannot instantiate object of type project.core.plugin.mybatis.CustomPlugin

と、出たのでスタックトレースを見てみると根本的は原因はjava.lang.ClassNotFoundException:project.core.plugin.mybatis.CustomPluginでした。
プラグインはあるはずなのにClassNotFoundExceptionがでてるので調べた結果どうやらPluginはjarである必要がある?みたいです。

カスタムプラグイン適用方

1. プラグイン専用のプロジェクトを作成する

  • なんでもいいのでプラグイン置き場のプロジェクトを作成しましょう。今回はproject-pluginというプロジェクトにしておきます。
  • 適用したプラグインを全てこのプロジェクトに移動します。

2. Mybatis Generator Config のXMLファイル修正

  • Pluginタグのtype属性に指定するパッケージを移動したプラグインのパッケージに修正します。

3. gradle.buildの設定

build.gradle
project(':project-core') {
    /* 中略 */

    configurations {
        mybatisGenerator
    }

    dependencies {
        mybatisGenerator group: 'org.mybatis.generator', name: 'mybatis-generator-core', version: mybatisGeneratorVersion
        mybatisGenerator group: 'mysql', name: 'mysql-connector-java', version: mysqlConnectorJavaVersion
        mybatisGenerator group: 'tk.mybatis', name: 'mapper', version: mybatisMapperVersion
        // 先ほど作成したカスタムプラグインのあるプロジェクトを mybatisGenerator タスクの依存関係として追加する
        mybatisGenerator project(':project-plugin')
    }

    /* mybatisGenerator のタスク処理省略 */
}

// カスタムプラグインのあるプロジェクト
project(':project-plugin') {
    dependencies {
        compile group: 'org.mybatis.generator', name: 'mybatis-generator-maven-plugin', version: mybatisGeneratorVersion
    }
}

最後に

これでmybatisGeneratorタスクが実行されたときにproject-pluginが依存関係で追加されているので、カスタムプラグインが適用されているはずです!
「俺シングルプロジェクトでカスタムプラグインを適用する方法知ってる!!!」って人がいたら是非教えてくださいーーー

6
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7