Want to deploy your JVM, Node.js and Go apps effortlessly to AWS? Try our service Boxfuse

Gradle Plugin

The Flyway Community Edition and Flyway Pro Edition Gradle plugin support Gradle 3.x and Gradle 4.x running on Java 8, Java 9 or Java 10. The Flyway Enterprise Gradle plugin also supports Java 7.

Installation

plugins {
    id "org.flywaydb.flyway" version "5.1.1"
}

Start by downloading and extracting the Flyway Trial Edition zip. Then execute the installToLocalMavenRepo script in the flyway-5.1.1 directory. This installs all necessary artifacts in your local Maven repository, making it possible to use them in Gradle builds.

buildscript {
    repositories {
        mavenLocal()
    }
    dependencies {
        classpath "org.flywaydb.trial:flyway-gradle-plugin:5.1.1"
    }
}

apply plugin: 'org.flywaydb.flyway'
buildscript {
    repositories {
        maven {
            url "s3://flyway-repo/release"
            credentials(AwsCredentials) {
                accessKey 'your-flyway-pro-user'
                secretKey 'your-flyway-pro-password'
            }
        }
    }
    dependencies {
        classpath "org.flywaydb.pro:flyway-gradle-plugin:5.1.1"
    }
}

apply plugin: 'org.flywaydb.flyway'
buildscript {
    repositories {
        maven {
            url "s3://flyway-repo/release"
            credentials(AwsCredentials) {
                accessKey 'your-flyway-enterprise-user'
                secretKey 'your-flyway-enterprise-password'
            }
        }
    }
    dependencies {
        classpath "org.flywaydb.enterprise:flyway-gradle-plugin:5.1.1"
    }
}

apply plugin: 'org.flywaydb.flyway'

Tasks

Name Description
flywayMigrate Migrates the database
flywayClean Drops all objects in the configured schemas
flywayInfo Prints the details and status information about all the migrations
flywayValidate Validates the applied migrations against the ones available on the classpath
flywayUndo Flyway Pro Undoes the most recently applied versioned migration
flywayBaseline Baselines an existing database, excluding all migrations up to and including baselineVersion
flywayRepair Repairs the schema history table

Configuration

The Flyway Gradle plugin can be configured in a wide variety of following ways, which can all be combined at will.

Build script (single database)

The easiest way is to simply define a Flyway section in your build.gradle:

flyway {
    url = 'jdbc:h2:mem:mydb'
    user = 'myUsr'
    password = 'mySecretPwd'
    schemas = ['schema1', 'schema2', 'schema3']
    placeholders = [
        'keyABC': 'valueXYZ',
        'otherplaceholder': 'value123'
    ]
}

Build script (multiple databases)

To migrate multiple database you have the option to extend the various Flyway tasks in your build.gradle:

task migrateDatabase1(type: org.flywaydb.gradle.task.FlywayMigrateTask) {
    url = 'jdbc:h2:mem:mydb1'
    user = 'myUsr1'
    password = 'mySecretPwd1'
}

task migrateDatabase2(type: org.flywaydb.gradle.task.FlywayMigrateTask) {
    url = 'jdbc:h2:mem:mydb2'
    user = 'myUsr2'
    password = 'mySecretPwd2'
}

Extending the default classpath

By default the Flyway Gradle plugin uses a classpath consisting of the compile, runtime, testCompile and testRuntime Gradle configurations for loading drivers, migrations, resolvers, callbacks, etc.

You can optionally extend this default classpath with your own custom configurations in build.gradle as follows:

// Start by defining a custom configuration like 'provided', 'migration' or similar
configurations {
    flywayMigration
}

// Declare your dependencies as usual for each configuration
dependencies {
    compile "org.flywaydb:flyway-core:${flywayVersion}"
    flywayMigration "com.mygroupid:my-lib:1.2.3"
}

flyway {
    url = 'jdbc:h2:mem:mydb'
    user = 'myUsr'
    password = 'mySecretPwd'
    schemas = ['schema1', 'schema2', 'schema3']
    placeholders = [
        'keyABC': 'valueXYZ',
        'otherplaceholder': 'value123'
    ]
    // Include your custom configuration here in addition to any default ones you want included
    configurations = [ 'compile', 'flywayMigration' ]
}

For details on how to setup and use custom Gradle configurations, see the official Gradle documentation.

Gradle properties

The plugin can also be configured using Gradle properties. Their can be passed either directly via the command-line:

> gradle -Pflyway.user=myUsr -Pflyway.schemas=schema1,schema2 -Pflyway.placeholders.keyABC=valXYZ

or via a gradle.properties file:

flyway.user=myUser
flyway.password=mySecretPwd

# List are defined as comma-separated values
flyway.schemas=schema1,schema2,schema3

# Individual placeholders are prefixed by flyway.placeholders.
flyway.placeholders.keyABC=valueXYZ
flyway.placeholders.otherplaceholder=value123

They can they be accessed as follows from your build.gradle:

project.ext['flyway.user']='myUsr'
project.ext['flyway.password']='mySecretPwd'
project.ext['flyway.schemas']='schema1,schema2,schema3'
project.ext['flyway.placeholders.keyABC']='valueXYZ'
project.ext['flyway.placeholders.otherplaceholder']='value123'

Environment Variables

To make it ease to work with cloud and containerized environments, Flyway also supports configuration via environment variables. Check out the Flyway environment variable reference for details.

System properties

Configuration can also be supplied directly via the command-line using JVM system properties:

> gradle -Dflyway.user=myUser -Dflyway.schemas=schema1,schema2 -Dflyway.placeholders.keyABC=valueXYZ

Config files

Config files are supported by the Flyway Gradle plugin. If you are not familiar with them, check out the Flyway config file structure and settings reference first.

Flyway will search for and automatically load the <user-home>/flyway.conf config file if present.

It is also possible to point Flyway at one or more additional config files. This is achieved by supplying the System property flyway.configFiles as follows:

> gradle -Dflyway.configFiles=path/to/myAlternativeConfig.conf flywayMigrate

To pass in multiple files, separate their names with commas:

> gradle -Dflyway.configFiles=path/to/myAlternativeConfig.conf,other.conf flywayMigrate

Relative paths are relative to the directory containing your build.gradle file.

Alternatively you can also use the FLYWAY_CONFIG_FILES environment variable for this. When set it will take preference over the command-line parameter.

> export FLYWAY_CONFIG_FILES=path/to/myAlternativeConfig.conf,other.conf

By default Flyway loads configuration files using UTF-8. To use an alternative encoding, pass the system property flyway.configFileEncoding as follows:

> gradle -Dflyway.configFileEncoding=ISO-8859-1 flywayMigrate

This is also possible via the flyway section of your build.gradle or via Gradle properties, as described above.

Alternatively you can also use the FLYWAY_CONFIG_FILE_ENCODING environment variable for this. When set it will take preference over the command-line parameter.

> export FLYWAY_CONFIG_FILE_ENCODING=ISO-8859-1

Overriding order

The Flyway Gradle plugin has been carefully designed to load and override configuration in a sensible order.

Settings are loaded in the following order (higher items in the list take precedence over lower ones):

  1. System properties
  2. Environment variables
  3. Custom config files
  4. Gradle properties
  5. Flyway configuration section in build.gradle
  6. <user-home>/flyway.conf
  7. Flyway Gradle plugin defaults

The means that if for example flyway.url is both present in a config file and passed as -Dflyway.url= from the command-line, the JVM system property passed in via the command-line will take precedence and be used.

Gradle: migrate