Search code examples
gradleflyway

Gradle and Flyway: Unable to obtain inputstream for resource


I have the problem that migrateDb using Flyway in a Gradle project sometimes causes

Unable to obtain inputstream for resource: META-INF/db/mysql/V1__script.sql

This error doesn't occur all the time but only sometimes but if it happens it's quite persistent and also a clean/rebuild of the project doesn't solve it.

The SQL script that is mentioned is contained within a JAR file that is referenced from the project as part of a multi module project.

My researched only brought me to https://github.com/flyway/flyway/issues/702 but this didn't lead me to the right way.

I'm also very confused that flyway is able to find the file during the classpath search but is then not able to get the input stream.

If you should need any further info please ask.

Further debugging: It seems that the error seems to happen as soon as a new file is added to the DB JAR. After rebuild and migrateDb the error occurs. If I remove the script again the error still occurs although the script is not in the generated JAR anymore. So I guess the classpath for searching for scripts and retrieving input stream is different. Does anybody know what differences there might be?

Complete stack trace:

Caused by: org.flywaydb.core.api.FlywayException: Unable to obtain inputstream for resource: META-INF/db/mysql/V1__script.sql
        at org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource.loadAsString(ClassPathResource.java:84)
        at org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver.scanForMigrations(SqlMigrationResolver.java:139)
        at org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver.resolveMigrations(SqlMigrationResolver.java:99)
        at org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver.resolveMigrations(SqlMigrationResolver.java:49)
        at org.flywaydb.core.internal.resolver.CompositeMigrationResolver.collectMigrations(CompositeMigrationResolver.java:122)
        at org.flywaydb.core.internal.resolver.CompositeMigrationResolver.doFindAvailableMigrations(CompositeMigrationResolver.java:104)
        at org.flywaydb.core.internal.resolver.CompositeMigrationResolver.resolveMigrations(CompositeMigrationResolver.java:90)
        at org.flywaydb.core.internal.resolver.CompositeMigrationResolver.resolveMigrations(CompositeMigrationResolver.java:43)
        at org.flywaydb.core.internal.info.MigrationInfoServiceImpl.refresh(MigrationInfoServiceImpl.java:114)
        at org.flywaydb.core.internal.command.DbValidate$2.call(DbValidate.java:164)
        at org.flywaydb.core.internal.command.DbValidate$2.call(DbValidate.java:157)
        at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:75)
        at org.flywaydb.core.internal.command.DbValidate.validate(DbValidate.java:157)
        at org.flywaydb.core.Flyway.doValidate(Flyway.java:1280)
        at org.flywaydb.core.Flyway.access$100(Flyway.java:71)
        at org.flywaydb.core.Flyway$1.execute(Flyway.java:1176)
        at org.flywaydb.core.Flyway$1.execute(Flyway.java:1168)
        at org.flywaydb.core.Flyway.execute(Flyway.java:1650)
        at org.flywaydb.core.Flyway.migrate(Flyway.java:1168)
        at org.flywaydb.gradle.task.FlywayMigrateTask.run(FlywayMigrateTask.java:28)
        at org.flywaydb.gradle.task.AbstractFlywayTask.runTask(AbstractFlywayTask.java:382)

Gradle Task:

// task that migrates the database
task migrateDb(type: org.flywaydb.gradle.task.FlywayMigrateTask) {
    // parse hibernate config
    def hibernateConfig = parseHibernateConfigByStageParameter()

    // set config
    url = hibernateConfig.url
    driver = hibernateConfig.driver
    user = hibernateConfig.username
    password = hibernateConfig.password
    locations = [ "classpath:${flywayDbPath}/${hibernateConfig.dbType}" ]
    table = 'schema_version'
    outOfOrder = true
    ignoreMissingMigrations = true
}

Gradle dependency:

// dependencies
dependencies {
    [...]
    runtime project(':core:db:mysql')
    [...]
}

Solution

  • During further testing I noticed that the error seems to be because of the Gradle Daemon. Adding --no-daemon to the migrateDb call works fine and doesn't trigger the error.

    That's fine for me for now.