简体   繁体   English

在子项目中继承gradle插件

[英]Inheriting gradle plugin in subprojects

I want to avoid redundancy and therefore I got one "shared" project that contains looks like this: 我想避免冗余,因此我得到一个包含以下内容的“共享”项目:

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

repositories {
    mavenCentral()
    jcenter()
}

apply plugin: "java"

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

    // ...
}

Then I also have my regular projects that inherit the compile dependencies from my shared project like this: 然后我还有我的常规项目,从我的共享项目继承编译依赖项,如下所示:

apply plugin: "java"

repositories {
    mavenCentral()
    jcenter()
}

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

Is there any way I can make my regular projects inherit the plugin block or the actual plugin as well? 有什么办法可以让我的常规项目继承插件块或实际的插件吗?

Not inherit as such. 继承本身。 It seems to me that what you're trying to do can be achieved by configuring the subprojects from the root project. 在我看来,您可以通过从根项目配置子项目来实现您要做的事情。 Basically in your root build.gradle (which script that will configure your root project) you can write: 基本上在您的根build.gradle (将配置您的根项目的脚本)中,您可以编写:

subprojects {
  // configuration
}

You can probably get rid of your shared project and have this in root project's build.gradle : 你可以摆脱你的共享项目,并在root项目的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"

        // ...
    }
}

This way all your subprojects will be configured using the same closure - this is equivalent to copy-pasting everything in subprojects block to your individual subprojects' build.gradle files. 这样,所有子项目都将使用相同的闭包进行配置 - 这相当于将subprojects块中的所有内容复制粘贴到各个子项目的build.gradle文件中。 The advantage over your initial solution is ability to also apply plugins, configure extensions, everything you normally can do. 与初始解决方案相比,它的优势还在于能够应用插件,配置扩展,以及您通常可以执行的所有操作。

As a side note, you don't need both jcenter() and mavenCentral in repositories block - jCenter is a superset of mavenCentral and is the preferred one 作为一个侧面说明,你不需要两个jcenter()mavenCentralrepositories块- jCenter是的超集mavenCentral ,是最好的一个

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

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