有没有办法让Gradle在构建时调用flywayMigrate?

时间:2016-09-15 13:13:53

标签: gradle flyway

我发现每次运行gradle build时自动调用Flyway's migrate command非常有用。

Spring Boot会在引擎盖下做到这一点,但是Gradle可以自己做吗?我有一个非启动应用程序,我希望能够以相同的方式管理。

我希望它是一些生命周期钩子。 This question很有帮助,但我如何执行flyway pre-build?

1 个答案:

答案 0 :(得分:2)

是的,你可以。你有几个选择。您可以随时进入生命周期。默认情况下,java gradle插件有几个可以挂钩的地方。

$ ./gradlew clean build
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
:check
:build

您可以附加到任何这些点

或者如果您需要先申请,那么您可能需要考虑一个简单的插件。

这是两个例子:

的build.gradle:

apply plugin: 'java'

repositories {
    jcenter()
}

dependencies {
    testCompile 'junit:junit:4.12'
}

task runFlyAwayCommand << {
    // process is type java.lang.Process
    def process = "printf lifecycle hooked task".execute()
    def processExitValue = process.waitFor()
    def processOutput = process.text
    project.logger.lifecycle("Flyaway{ exitValue: $processExitValue output: $processOutput }")
}

// compileJava could be any lifecycle task
tasks.findByName('compileJava').dependsOn tasks.findByName('runFlyAwayCommand')


// if you need to execute earlier you might want to create a plugin
apply plugin: SamplePlugin

class SamplePlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
        def process = "printf plugin apply".execute()
        def processExitValue = process.waitFor()
        def processOutput = process.text
        project.logger.lifecycle("Flyaway{ exitValue: $processExitValue output: $processOutput }")
    }
}

输出:

$ ./gradlew clean build
Configuration on demand is an incubating feature.
Flyaway{ exitValue: 1 output: plugin }
:clean
:runFlyAwayCommand
Flyaway{ exitValue: 1 output: lifecycle }
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

Total time: 1.294 secs