【问题标题】:Defining sets of dependencies in Gradle在 Gradle 中定义依赖项集
【发布时间】:2014-02-07 14:45:57
【问题描述】:

基本上我想做的是定义一组依赖项,然后在各个构建脚本中调用一个函数或类似的东西来添加它们。基本上是这样的:

/build.gradle

apply plugin: 'base' // To add "clean" task to the root project.

subprojects {
    apply from: rootProject.file('common-deps.gradle')
}

/settings.gradle

include ":sub-project"

/common-deps.gradle

def addHttpComponents() {
    dependencies {
        compile     group: 'org.apache.httpcomponents', name: 'httpcore',   version: '4.3'
        compile     group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.1'
    }
}

那么,如果我想将 HttpComponents 添加到我的构建中。我希望能够将我的子项目的构建文件编写为:

/sub-project/build.gradle

apply maven
apply java

addHttpComponents()

由于上面的文件无法运行,有没有办法做到这一点。还是我完全走错了路。

【问题讨论】:

    标签: dependencies gradle dependency-management


    【解决方案1】:

    首先,为什么你的方法不起作用:

    apply from: 将 gradle 脚本应用于项目对象。这意味着您可以在脚本中配置项目,但在该脚本中定义的本地方法不会神奇地在其他脚本中可用。

    您可以做的是将您的方法设置为项目对象的扩展属性。然后,您的方法将通过项目对象对其他脚本可用。

    /common-deps.gradle 更改为

    ext.addHttpComponents = {
      dependencies {
        compile     group: 'org.apache.httpcomponents', name: 'httpcore',   version: '4.3'
        compile     group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.1'
     }
    }
    

    事情应该会更好。


    接下来,无论如何,您的方法对我来说似乎有点令人费解。为什么不只是:

    common-deps.gradle

    dependencies {
        compile     group: 'org.apache.httpcomponents', name: 'httpcore',   version: '4.3'
        compile     group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.1'
    }
    

    然后在 /sub-project/build.gradle

     apply from: rootProject.file('common-deps.gradle')
    

    最后,我想你是说

    apply plugin: 'maven'
    apply plugin: 'java'
    

    /sub-project/build.gradle

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 2014-08-23
      • 1970-01-01
      • 2014-07-17
      • 2018-02-10
      • 1970-01-01
      相关资源
      最近更新 更多