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

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

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

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

最佳答案 是的你可以.你有几个选择.您可以随时进入生命周期.默认情况下,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
点赞