在Custom Plugin中使用Gradle Dependency

时间:2017-04-05 01:25:21

标签: maven gradle groovy

我正在尝试编写一个自定义Gradle插件,使用他们的API调用flyway迁移:

https://flywaydb.org/documentation/api/

这是一个最小的例子:

buildscript {
    repositories.jcenter()
    dependencies.classpath "org.flywaydb:flyway-core:4.1.2"
}

apply plugin: DatabaseHandlerPlugin

class DatabaseHandlerPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.task("databaseHandler").doLast {
            org.flywaydb.Flyway f = new Flyway(); // <= How can I use the above declared dependency here and in my projects?
        }
    }
}

但我的gradle抱怨它无法加载Flyway课程。

1 个答案:

答案 0 :(得分:0)

Flyway类位于org.flywaydb.core包中。你错过了core位。我的完整代码有效:

import org.flywaydb.core.Flyway; // << can import here

buildscript {
    repositories { mavenCentral() }

    dependencies {
        classpath "org.flywaydb:flyway-core:4.1.2"
    }

}
apply plugin: DatabaseHandlerPlugin

class DatabaseHandlerPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.task('databaseHandler') {
            doLast {
                Flyway f = new Flyway()
                println "Flyway: $f"
            }
        }
    }
}

输出:

> gradle databaseHandler    
:databaseHandler
Flyway: org.flywaydb.core.Flyway@7b27e8f4