Gradle中Flyway插件无法执行SQL文件,数据库表未生成
Hey there, let's dig into why your Flyway migrations aren't creating tables in the mydb1 database even though your build succeeds. Here are the key areas to check step by step:
1. Verify Flyway Plugin Setup in build.gradle
First, make sure you've properly added the Flyway plugin with a version compatible with Spring Boot 2.0.1.RELEASE (stick to Flyway 5.x for this Spring Boot version). Your plugins block should look something like this:
plugins { id 'org.springframework.boot' version '2.0.1.RELEASE' id 'java' id 'org.flywaydb.flyway' version '5.2.4' // Match this version to your Spring Boot release }
Also, double-check that Flyway dependencies aren't excluded anywhere in your build configuration.
2. Confirm Database Connection Configuration
Flyway needs valid credentials to connect to mydb1—verify these settings are correct. You can configure them either directly in build.gradle or via your Spring Boot properties file:
Option 1: Configure in build.gradle
flyway { url = 'jdbc:mysql://localhost:3306/mydb1?useSSL=false&serverTimezone=UTC' user = 'your_database_username' password = 'your_database_password' locations = ['filesystem:src/main/resources/db/migration'] // Explicitly set migration file path }
Option 2: Configure in application.properties
spring.flyway.url=jdbc:mysql://localhost:3306/mydb1?useSSL=false&serverTimezone=UTC spring.flyway.user=db_username spring.flyway.password=db_password spring.flyway.enabled=true
Important Note: Flyway won't create the mydb1 database itself—you need to manually create it using your database client first.
3. Check Migration File Naming & Location
Flyway strictly enforces file naming rules to detect valid migrations. Your SQL files must follow this pattern:V<version_number>__<description>.sql
Example: V1__Create_user_table.sql
- Use double underscores (
__) to separate the version number and description—single underscores will cause Flyway to ignore the file. - Ensure files are placed in the default location (
src/main/resources/db/migration) or the custom path you specified in thelocationsconfig. - Verify the files are being copied to your build output: check
build/resources/main/db/migrationafter running a build to confirm your SQL files are present.
4. Ensure Flyway Migrate Runs During Build
By default, the flywayMigrate task doesn't automatically run with the build task. You have two options:
- Manually trigger migrations with
./gradlew flywayMigrate, or - Bind the task to your build process in
build.gradle:
build.dependsOn flywayMigrate
This way, migrations will execute automatically every time you run ./gradlew build.
5. Inspect Flyway Logs for Clues
Run your build with verbose logging to see exactly what Flyway is doing:
./gradlew build --info
Search for Flyway-related logs to spot issues:
- If you see
Found 0 migrations, Flyway isn't detecting your SQL files. - If you see
Successfully applied X migrations, migrations ran but there might be an issue with your SQL logic. - Look for error messages about database connectivity or invalid migration file formatting.
6. Check if Flyway is Accidentally Disabled
Make sure Flyway isn't turned off in your configuration. In application.properties, confirm this setting is set to true:
spring.flyway.enabled=true
内容的提问来源于stack exchange,提问作者Dharita Chokshi

