【问题标题】:Using Gradle Dependency inside of Custom Plugin在自定义插件中使用 Gradle 依赖项
【发布时间】:2017-04-05 01:25:21
【问题描述】:

我正在尝试编写一个自定义 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 类。

【问题讨论】:

标签: maven gradle groovy


【解决方案1】:

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

【讨论】:

  • 感谢您的快速回答,但不幸的是,即使这样它也无法正常工作,与以前相同的问题:在插件任务中找不到该类。
  • @CryNickSystems 你的班级就在build.gradle 文件中吗?您的代码结构如何?
  • 不,不是。它来自依赖项(“org.flywaydb:flyway-core:4.1.2”)
  • @CryNickSystems 不,我的意思是你的班级DatabaseHandlerPlugin
  • 我将它添加到其他子项目/子模块中,如下所示:'apply from 'plugins/databaseHandler.gradle'
猜你喜欢
  • 2012-10-23
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多