【问题标题】:What is the classpath of flywayDb when using Gradle?使用 Gradle 时 flywayDb 的类路径是什么?
【发布时间】:2018-10-24 04:41:59
【问题描述】:

我正在尝试创建一个简单的 gradle 项目,该项目应该仅用于使用 flywayDb 使用存储在另一个目录中的 JAR 文件中的迁移脚本来迁移数据库。该目录与 gradle 构建没有任何关系。它是使用项目中的另一种机制创建和填充的。

(至少对我而言)显而易见的脚本不起作用:

[...]

// flyway plugin
apply plugin: 'org.flywaydb.flyway'

// dependencies
dependencies {
    compile fileTree(dir: '/workspace/WEB-INF/lib', include: '*.jar')
}

// flyway configuration
flyway {
    [...]
    locations = [ 'classpath:/META-INF/db' ]
}

调用 flyway 迁移任务时,我得到的只是

Unable to resolve location classpath:META-INF/db

并且没有任何东西被迁移。如果我提取 JAR 文件并使用 filesystem 位置,它可以正常工作。所以我猜flyway使用的类路径不包括JAR文件。

这让我想到了我的问题:flyway 使用什么类路径来搜索数据库脚本?以及如何将文件系统中的简单 JAR 文件添加到其中?

编辑:

注释后完成 build.gradle:

// add JDBC drivers (for flyway) and flyway itself to classpath
buildscript {
        repositories {
                maven {
                        url "https://plugins.gradle.org/m2/"
                }
        }
        dependencies {
                classpath group: 'gradle.plugin.com.boxfuse.client', name: 'gradle-plugin-publishing', version: '5.0.7'
                classpath group: 'mysql', name: 'mysql-connector-java', version: '5.1.42'
        }
}

// flyway plugin
apply plugin: 'org.flywaydb.flyway'

// configurations
configurations {
        flywayClasspath
}

// dependencies
dependencies {
        flywayClasspath fileTree(dir: '/workspace/WEB-INF/lib', include: '*.jar')
        flywayClasspath files('/workspace/WEB-INF/classes')
}

// get needed data for connection
def dbPassword = System.getenv('MYSQL_ROOT_PASSWORD')
def dbName = System.getenv('PROJECTNAME')

// flyway configuration
flyway {
        configurations = [ 'flywayClasspath' ]
        url = "jdbc:mysql://localhost/${dbName}?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin&useSSL=false"
        driver = 'com.mysql.jdbc.Driver'
        user = 'root'
        password = dbPassword
        locations = [ "classpath:/META-INF/db/mysql" ]
        table = 'schema_version'
        outOfOrder = true
        ignoreMissingMigrations = true
}

【问题讨论】:

    标签: database gradle database-migration flyway


    【解决方案1】:

    我从source code 看到有一个configurations 属性。我猜测默认情况下它使用 buildscript 类路径。如果您想在 flyway 的类路径上编译类,您可以执行类似

    的操作
    apply plugin: 'java'
    apply plugin: 'x.y.flyway'
    
    configurations {
        flyway
    }
    dependencies {
        flyway sourceSets.main.runtimeClasspath
    }
    flyway.configurations = ['flyway']
    flywayMigrate.dependsOn compileJava
    

    【讨论】:

    • 谢谢,但configurations 似乎是只读的:Cannot set the value of read-only property 'configurations' for root project 'database' of type org.gradle.api.Project.
    • 我可以看到扩展对象here上的公共属性。从错误消息来看,我猜你没有设置在 flyway { ... } 闭包内。
    • 它在 flyway 闭包内。我用完整的build.gradle 更新了最初的问题。 flyway 用于配置名称不起作用(可能由任务/插件保留)。因此我将其命名为flywayClasspath
    • 啊,也许flyway.configurationsproject.configurations 发生冲突,这可以解释错误消息。使用flyway.configurations = x查看我的更新
    • 这会导致Could not set unknown property 'configurations' for object of type org.flywaydb.gradle.FlywayExtension.。如果我尝试将其设置为 flywayMigration 而不是 flyway 它会导致相同但类型为 org.flywaydb.gradle.task.FlywayMigrateTask
    猜你喜欢
    • 2017-10-17
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多