Why the Configuration and Dependency are actually not ExtensionAware?


(Shiva Wu) #1

According to this page,

http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtensionAware.html

A lot of domain objects in Gradle are Extension aware, which means one can set extension properties on it by

‘ext.key = value’, or by other ways

The page says Configuration and Dependency are extension aware, and they should be, but they’re not,

not I can see.

Two pieces of evidence:

First, this code snippet should set a property on a dependency object

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

But I cannot get the value from elsewhere.

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

Nothing shows up.

Although when i set

provided=true

, Gradle will warn me that dynamic properties are deprecated, use extension property, which by the way, dynamic properties don’t seem to work either.

Second, According to Gradle’s source code, the Configuration and Dependency object ARE NOT ExtensionAware. https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/groovy/org/gradle/api/artifacts/Dependency.java

So am I missing some points? Or this feature has not been implemented so far. I think this is an important feature.


(Peter Niederwieser) #2

‘Configuration’ and ‘Dependency’ are ‘ExtensionAware’. The interface is added dynamically at runtime. ‘project.configurations.testCompile.each’ iterates over the resolved files. To iterate over the declared dependencies, use ‘project.configurations.testCompile.(all)Dependencies.all’.


(Shiva Wu) #3

Yes! It worked! Thank you very much. I do have one more question,

when i do ‘project.configurations.testCompile.dependencies.each(or all)’ and print every item, there seems to be a lot of duplicates(a lot of junit), why and how to resolve this?


(Shiva Wu) #4

Yes, it works! Thank you for fast response!