orirab
orirab

Reputation: 3353

What are configurations in Gradle?

When working with dependency resolution in gradle, you usually see something like this:

configurations {
    optional
    compile
    runtime.extendsFrom compile
    testCompile.extendsFrom runtime
}

and I wanted to know of what type is optional or compile? Is it a Class? a string? what methods can I call for it?


Besides all this, is there a way to find out these things automatically, similar to ctrl+space when on something in eclipse?

Upvotes: 4

Views: 1744

Answers (3)

Steinar
Steinar

Reputation: 5950

They are classes that implements org.gradle.api.artifacts.Configuration. The Gradle DSL doc also contains more information about the configuration DSL core type.

To find out more info about internal classes etc, which is useful when for instance looking up classes and methods in the Gradle javadoc, it is often as simple as just printing out the class names. Quite often though, you will end up with some internal implementing class instead of the API interface you're interested in, but regardless of that it's a way get started on what to search for. I tend to keep the source code of all open source projects we're using available in the IDE. That way it's easy to jump into the correct class (even when it's not available through context shortcuts) and look around.

To get more information about configurations in your case, you could add a task that simply prints out the relevant info. E.g. something like:

task configInfo << {
    println "configurations.class: ${configurations.class}"
    println "configurations.compile class: ${configurations.compile.class}"
    println "implements ${Configuration} interface? ${configurations.compile instanceof Configuration}"
}

which in my case results in the following output

$ gradle configInfo
:configInfo
configurations.class: class  org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer_Decorated
configurations.compile class: class org.gradle.api.internal.artifacts.configurations.DefaultConfiguration_Decorated
implements interface org.gradle.api.artifacts.Configuration interface? true

Upvotes: 4

Will
Will

Reputation: 14549

I am no Gradle expert, but this seems like a simple getter delegated to another object in a DSL fashion. You could write the same with something like this:

class MyDsl {
  def config = [:].withDefault { false }
  void configure(closure) {
    closure.delegate = this
    closure()
  }

  def getOptional() { config.optional = true }
  def getCompile() { config.compile = true }
  def getTest() { config.test = true }
}

dsl = new MyDsl()

dsl.configure {
  optional
  compile
}

dsl.config.with {
  assert optional
  assert compile
  assert !test
}

You could return some specific object to pass to runtime.extendsFrom() method.

For auto-complete, IIRC that's what groovy-eclipse DSLD (DSL descriptors) are for. You may want to give a try to this gradle DSLD which is in eclipse-integration-gradle plugin.

As per this ticket it has been done long ago.

Upvotes: 4

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27255

The question "what type is optional or compile" isn't really valid. That is kind of like asking what type does "instanceof" have. The instanceof keywword doesn't have a type.

When writing code like you cited, you are taking advantage of a DSL. Treat words like compile and optional as keywords in that DSL. Unless you are writing your own DSL (as opposed to taking advantage of existing one, which is what this question is about), don't think of types being associated with those things.

As for the question about ctrl+space, Eclipse won't do anything special with that in this context unless you are using a plugin which provides support for that. Even with plugin support there will still be limitations because you can define your own configurations. If you were going to define a configuration named "jeffrey" and you typed "jeff" followed by ctrl+space, there is no way for the IDE to know you want it to turn that into "jeffrey".

I hope that helps.

Upvotes: 2

Related Questions