简体   繁体   English

在自定义插件中使用Gradle依赖关系

[英]Using Gradle Dependency inside of Custom Plugin

I am trying to write a custom Gradle Plugin that invokes the flyway migration using their API: 我正在尝试编写一个自定义的Gradle插件,该插件使用其API调用飞行路线迁移:

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

This is a minimal example: 这是一个最小的示例:

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?
        }
    }
}

But my gradle complains that it cannot load the Flyway class. 但是我的毕业生抱怨说它无法加载Flyway类。

The Flyway class is in the org.flywaydb.core package. org.flywaydb.core类位于org.flywaydb.core包中。 You missed the core bit. 您错过了core My full code that works: 我的完整代码有效:

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"
            }
        }
    }
}

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM