【问题标题】:Why the Configuration and Dependency in Gradle are not ExtensionAware?为什么 Gradle 中的配置和依赖不是 ExtensionAware?
【发布时间】:2012-12-17 12:56:06
【问题描述】:

根据此页面, http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtensionAware.html

Gradle 中的很多领域对象都支持扩展,这意味着可以通过以下方式设置扩展属性 ext.key = value,或者其他方式

页面说配置和依赖是扩展感知的,他们应该是,但他们不是, 我看不到。

两个证据:

首先,这段代码 sn-p 应该在依赖对象上设置一个属性

dependencies {
    testCompile(group: 'junit', name: 'junit', version: '4.9') {
        provided = true
        ext.provided = true
    }
}

但我无法从其他地方获得价值。

task test  { task ->
    task.project.configurations.testCompile.each { 
        println it
        println it.provided
        println it.ext.provided
        println it.hasProperty('provided')
    }
}

什么都没有出现。

虽然当我设置provided=true 时,Gradle 警告我动态属性已被弃用,请使用扩展属性,顺便说一下,动态属性似乎也不起作用。

第二,根据Gradle的源码, Configuration 和 Dependency 对象不是 ExtensionAware。

https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/groovy/org/gradle/api/artifacts/Dependency.java

所以我错过了一些要点吗?或者这个功能目前还没有实现。 我认为这是一个重要的功能。

【问题讨论】:

    标签: groovy build gradle


    【解决方案1】:

    ConfigurationDependency ExtensionAware。该接口是在运行时动态添加的。 project.configurations.testCompile.each 遍历已解析的文件。要遍历声明的依赖项,请使用project.configurations.testCompile.(all)Dependencies.all

    【讨论】:

      【解决方案2】:

      我知道这个问题真的很老,但我今天遇到了这个问题,使用复制和分离的配置,这些配置不是 ExtensionAware,即使我希望它们是并且认为它们会基于 OP 问题中引用的文档。这出现在我的谷歌查询的顶部,所以我想我会分享以防其他人遇到这个问题(没有足够的代表来回复 Peter Niederwieser 的回答)......彼得,这是一个错误(至少在文档中) ?)

      $ cat build.gradle
      apply plugin: 'base'
      
      configurations {
          ericwashere
      }
      
      task test << {
          println configurations.ericwashere instanceof ExtensionAware
          println configurations.ericwashere.copy() instanceof ExtensionAware
          println configurations.detachedConfiguration() instanceof ExtensionAware
      }
      
      
      $ ./gradlew --version
      
      ------------------------------------------------------------
      Gradle 2.11
      ------------------------------------------------------------
      
      Build time:   2016-02-08 07:59:16 UTC
      Build number: none
      Revision:     584db1c7c90bdd1de1d1c4c51271c665bfcba978
      
      Groovy:       2.4.4
      Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
      JVM:          1.8.0_31 (Oracle Corporation 25.31-b07)
      OS:           Windows 7 6.1 amd64
      
      
      $ ./gradlew test
      :test
      true
      false
      false
      
      BUILD SUCCESSFUL
      
      Total time: 2.433 secs
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-17
        • 1970-01-01
        • 2021-02-24
        • 2017-11-11
        • 2021-07-05
        相关资源
        最近更新 更多