Flyway integrates with Maven and Gradle via official plugins that expose Flyway commands as build goals/tasks (migrate, validate, info, baseline, repair, clean), and by letting you configure Flyway the same way you would in app runtime: URL/user/password, locations, schemas, placeholders, config files, etc.
Maven integration (Flyway Maven Plugin)
How you use it
- Add the Flyway plugin to
pom.xml - Run goals like:
mvn flyway:migratemvn flyway:validatemvn flyway:infomvn flyway:baselinemvn flyway:repair
The plugin is designed so configuration can come from multiple places with a clear precedence order (system properties/env vars/config files/Maven properties/plugin config/settings.xml creds/~/.flyway.conf).
Typical Maven setup (conceptually)
- Put SQL migrations under the usual default:
src/main/resources/db/migration(or configurelocations) - Configure DB connection in plugin config or via properties (often from CI secrets)
- Call
flyway:migratein CI, or during a dedicated pipeline stage (not necessarily on every local build)
Gradle integration (Flyway Gradle Plugin)
How you use it
- Apply plugin
org.flywaydb.flyway(from Gradle Plugin Portal) - Run tasks like:
gradle flywayMigrategradle flywayValidategradle flywayInfogradle flywayBaselinegradle flywayRepairgradle flywayClean(dangerous outside local/dev)
Config and config files
- You can configure via
flyway { ... }inbuild.gradle(.kts)or use config files. - The Gradle plugin supports
~/.flyway.confand can load additional config files via-Dflyway.configFiles=....
Common patterns in real projects
1) “CI runs migrations” (most common for services)
- CI stage:
validate→migrate - App startup doesn’t need to run migrations (or you keep it enabled, but you must be intentional about it).
2) “App runs migrations on startup” (common with Spring Boot)
- Add
flyway-coredependency and Flyway runs automatically at startup when configured. - Still keep the plugin for local/CI tooling if useful.
3) Separate DB user for migrations
- Use elevated “migration user” for Flyway runs, limited “app user” at runtime.