【问题标题】:Inheriting gradle plugin in subprojects在子项目中继承 gradle 插件
【发布时间】:2017-10-04 06:34:41
【问题描述】:

我想避免冗余,因此我得到了一个“共享”项目,其中包含如下所示:

plugins {
    id "org.flywaydb.flyway" version "4.2.0"
}

repositories {
    mavenCentral()
    jcenter()
}

apply plugin: "java"

dependencies {
    compile "commons-io:commons-io:2.4"

    // ...
}

然后我还有我的常规项目,它们从我的共享项目中继承编译依赖项,如下所示:

apply plugin: "java"

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile project(":shared")
    testCompile project(":shared")
}

有什么方法可以让我的常规项目也继承插件块或实际插件?

【问题讨论】:

    标签: gradle flyway subproject


    【解决方案1】:

    不是继承。在我看来,您可以通过从根项目配置子项目来实现您想要做的事情。基本上在你的根build.gradle(将配置你的根项目的脚本)你可以写:

    subprojects {
      // configuration
    }
    

    您可能可以摆脱您的共享项目并将其放在根项目的build.gradle

    plugins {
      id "org.flywaydb.flyway" version "4.2.0" apply false
    }
    
    subprojects {    
        repositories {
            mavenCentral()
            jcenter()
        }
    
        apply plugin: "java"
        apply plugin: "org.flywaydb.flyway"
    
        dependencies {
            compile "commons-io:commons-io:2.4"
    
            // ...
        }
    }
    

    这样,您的所有子项目都将使用相同的闭包进行配置 - 这相当于将 subprojects 块中的所有内容复制粘贴到您各个子项目的 build.gradle 文件中。与您最初的解决方案相比,优势在于能够应用插件、配置扩展,以及您通常可以做的所有事情。

    附带说明,repositories 块中不需要 jcenter()mavenCentral - jCentermavenCentral 的超集,并且是首选

    【讨论】:

    • 试过了,但我得到了:Error:(57, 0) Could not find method plugins() for arguments [build_44j4lgef9ftzfeyhh4janhtfy$_run_closure3$_closure14@65b80565] on project ':core' of type org.gradle.api.Project." 所以显然不能在子项目中使用 plugins 语句......
    • 你是对的,我已经修复了解决plugins当前限制的答案
    猜你喜欢
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2017-11-26
    • 2014-05-19
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    相关资源
    最近更新 更多