我应该如何构建这些gradle任务依赖项?

时间:2017-09-18 13:09:57

标签: gradle flyway

我有这些gradle任务:
- startTestDatabaseContainer:构建并启动带有数据库的docker容器 - removeTestDatabaseContainer:停止并移除泊坞窗容器
- flywayValidate:来自org.flywaydb.flyway的任务,用于验证我的迁移文件

我希望按顺序运行这三项任务。阅读this让我想到了这个解决方案:

flywayValidate.dependsOn startTestDatabaseContainer
flywayValidate.finalizedBy removeTestDatabaseContainer

这没关系,但是如果没有调用gradle flywayValidatestartTestDatabaseContainer,我就无法从命令行运行removeTestDatabaseContainer。我希望能够在没有发生这种情况的情况下运行flywayValidate

当我无法在gradle中订购依赖项时,我该怎么做?

我的第一次尝试就是:

task validateMigration {
    dependsOn startTestDatabaseContainer
    dependsOn flywayValidate
    finalizedBy removeTestDatabaseContainer
}

但是失败是因为flywayValidate可以在startTestDatabaseContainer之前运行。

编辑:我在Opal的解决方案上设置了一个示范基地:github.com/stianlagstad/flyway-migration-error-causes-final-gradle-task-to-not-execute。克隆它并运行gradle validateMigration。迁移将失败,最终的gradle任务将不会运行(docker ps将显示容器仍在运行)。如果您修复了迁移文件,那么一切都按预期工作。我确定我误会了什么。任何指针都会有所帮助!

2 个答案:

答案 0 :(得分:1)

以下设置应满足您的所有要求:

task startTestDatabaseContainer {
  doLast {
    println 'startTestDatabaseContainer'
  }
}
task flywayValidate {
  doLast {
    println 'flywayValidate'
  }
}
task removeTestDatabaseContainer {
  doLast {
    println 'removeTestDatabaseContainer'
  }
}

task validateMigration {
    dependsOn startTestDatabaseContainer
    dependsOn flywayValidate
    flywayValidate.mustRunAfter startTestDatabaseContainer
    finalizedBy removeTestDatabaseContainer
}

修改

task removeTestDatabaseContainer {
  doLast {
    println 'removeTestDatabaseContainer'
  }
}

task startTestDatabaseContainer {
  doLast {
    println 'startTestDatabaseContainer'
  }
  finalizedBy removeTestDatabaseContainer
}

task flywayValidate { t->
  doLast {
    println 'flywayValidate'
    throw new TaskExecutionException(t, new RuntimeException('whatever'))
  }
}

task validateMigration {
    dependsOn startTestDatabaseContainer
    dependsOn flywayValidate
    flywayValidate.mustRunAfter startTestDatabaseContainer
}

Here是一个演示。

答案 1 :(得分:0)

task validateMigration {
    dependsOn startTestDatabaseContainer
    dependsOn flywayMigrate
    flywayMigrate.finalizedBy removeTestDatabaseContainer
    flywayMigrate.mustRunAfter startTestDatabaseContainer
}

这就是诀窍!感谢您orzeh for the PR on Github,感谢您Opal的帮助!