Use Flyway in a multi-module gradle project

Requirements

  • Some of the sub-projects need to run flyway as a gradle task
  • We want a single command line to do the flyway migrations for all the sub-projects mentioned above

Solution

  • Apply flyway plugin in these sub-projects
  • Config flyway in these sub-projects
  • Create a root-project task to call the flyway tasks in the sub-projects

Code

buildscript {
    dependencies {
        classpath 'org.postgresql:postgresql:42.2.10' //or any other jdbc driver
}

plugins {
    id "org.flywaydb.flyway" version "6.2.4" apply false //get it in but donot apply it for root project
}

task dbMigrate(){ //root project task
    dependsOn "foo:flywayMigrate"
}

...
project(':foo') {  //a sub project
    apply plugin: "org.flywaydb.flyway" 
    def dbHost = findProperty('fooDbHost')

    flyway{
        url = "jdbc:postgresql://${dbHost}:5432/xxx"
        user = findProperty('fooDbUsername')
        password = findProperty('fooDbPassword')
    }
...
}

...
project(':bar') {  //a sub project
    apply plugin: "org.flywaydb.flyway" 
    def dbHost = findProperty('barDbHost')

    flyway{
        url = "jdbc:postgresql://${barDbHost}:5432/xxx"
        user = findProperty('barDbUsername')
        password = findProperty('barDbPassword')
    }
...
}

Now run it on the root level of your project:

./gradlew dbMigrate -PfooDbHost=xxx -PfooDbUsername=xxx -PfooDbPassword=xxx  -PbarDbHost=xxx -PbarDbUsername=xxx -PbarDbPassword=xxx





Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.