Flyway Gradle plugin - Circular dependency

2019-07-04 11:33发布

问题:

I have a project that uses gradle, flyway gradle plugin, mybatis generator and postgres. In my build.gradle, I have:

  compileJava.dependsOn('myBatisGenerator')

I would like to run the flywayMigrate task before myBatisGenerator runs. So I did the following:

        myBatisGenerator.dependsOn('flywayMigrate')

And when I try to run the build using gradle test, I get the following error:

FAILURE: Build failed with an exception.

* What went wrong:
Circular dependency between the following tasks:
:classes
+--- :compileGroovy
|    \--- :compileJava
|         \--- :myBatisGenerator
|              \--- :flywayMigrate
|                   \--- :testClasses
|                        +--- :compileTestGroovy
|                        |    +--- :classes (*)
|                        |    \--- :compileTestJava
|                        |         \--- :classes (*)
|                        \--- :compileTestJava (*)
\--- :compileJava (*)

(*) - details omitted (listed previously)

I am not sure why compileTestJava is being called from within the flywayMigrate plugin. Any ideas how to work around the issue and still have the flyway plugin run before mybatis generator?

回答1:

I took a look at the flyway gradle plugin code (https://github.com/flyway/flyway/tree/master/flyway-gradle-plugin) and my guess is that the flyway tasks depend on the compile tasks in order to support migrations written using the flyway Java api.

The flyway plugin seems to assume that that if the project is a java project then you are using the Java api.

Reading between the lines, it seems that flyway expects you to have a separate gradle sub-project for your migrations.

So, move your migrations to a sub-project called, say, 'migrations'. Then you can do

myBatisGenerator.dependsOn(':migrations:flywayMigrate')

and ':migrations:flywayMigrate' will only depend on ':migrations:compileTestJava' rather than your main ':compileTestJava' (and even then only if 'migrations' is a java project)



回答2:

Alternate workaround: https://github.com/flyway/flyway/issues/775

project.afterEvaluate {
    flywayClean.dependsOn -= testClasses
    flywayMigrate.dependsOn = [processResources, processTestResources]
}