KotlinGradle中的Flyway-无法导入FlywayMigrateTask

vsdwdz23  于 2023-10-19  发布在  Kotlin
关注(0)|答案(2)|浏览(89)

我需要在两个不同的shcema上运行两个不同的flyway迁移,每个shcema都有自己的用户帐户。根据Flyway文档,我只需要为每个FlywayMigrateTask连接设置一个自定义任务。来自使用默认(Groovy)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'
}

我尝试在Kotlingradle(kts)中这样做,但我的项目无法解析对FlywayMigrateTask的引用,即使我在External Libraties中有它:

如何注册任务:

tasks.register(
    "flywayTesting", 
    type = org.flywaydb.gradle.task.FlywayMigrateTask::class.java) {
    // Details hidden
}

我能够解析org.flywaydb.gradle.task包,但无法解析任务文件夹中的FlywayMigrateTask类。我做错了什么?

我的Gradle设置中的亮点:

plugins {
    kotlin("jvm") version "1.6.0"
    id("org.flywaydb.flyway") version "8.5.1"
}

repositories {
    mavenCentral()
}

tasks.withType<Wrapper> {
    gradleVersion = "6.8.2"
    distributionType = Wrapper.DistributionType.BIN
}

dependencies {
    implementation("org.flywaydb:flyway-core:8.5.1")
    implementation("org.flywaydb:flyway-gradle-plugin:8.5.0")
}

kotlin {
    sourceSets["main"].apply {
        kotlin.srcDir("src/main/kotlin")
    }
}
iecba09b

iecba09b1#

我就是这么做的。
首先添加Flyway Gradle插件:

plugins {
    java
    id("org.flywaydb.flyway") version "9.22.0"
}

然后,定义一个自定义任务:

task("flywayMigrateLocal", org.flywaydb.gradle.task.FlywayMigrateTask::class) {
    description = "Migrates the local database"
    url = "jdbc:postgresql://localhost:5432/test"
    user = "test"
    password = "test"
    locations = arrayOf(
        "filesystem:src/main/resources/db/migration",
        "filesystem:src/test/resources/flyway/migrations_local"
    )
}
qco9c6ql

qco9c6ql2#

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

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

相关问题