Using a variable for a configuration name with dependancies?


(david.hill) #1

Can you use a variable to declare a dependencies name?

I being hit with my dependencies being ‘resolved’ due to an overlap in config names across the projects so would like to create a configuration with a variable name.

I know can add a named configuration like this in 1.4 (yes I saw the note on it being deprecated in 1.6 so would takes hints on that too)

def String avar = “MyConfig” configurations.add(avar)

but fails in dependencies when I try:

dependencies {

avar group: “javafx”, name: basename, version: ver, ext: ext

} with and without the $

Any hints on how to proceed ?

This is obviously being used to grab an ivy external dep that we need early on, and then a different one later, hopefully with a single method.

I have tried a few things related to getting a Configuration - but nothing seems syntactically to work.


(Peter Niederwieser) #2

You can use ‘dependencies.add(avar, …)’. Instead of ‘configurations.add’, you should use ‘configurations.create’. (Doesn’t the deprecation message say this?)


(david.hill) #3

Thanks Peter. Double checking the DSL doc for configurations.create - don’t see it there. Just see .add with the deprecation in 1.6 (http://www.gradle.org/docs/current/dsl/org.gradle.api.artifacts.ConfigurationContainer.html)

My project is currently bound to gradle 1.4 so I did not see any deprecation warning message.

The dependencies.add(avar, …) syntax was baffling me. I tried a wide variety of things and poured over the documentation trying to noodle out what ‘dependencyNotation’ would work.

This is what worked before, but did not provide for a variable configuration name.

dependencies {

tools group: “mypkg”, name: basename, version: ver, ext: ext

}

I tried this which failed, because I found I needed to specify the extension (tgz).

dependencies.add(cname, “mypkg:$basename:$ver” )

I tried this which failed, my closure was not applied (because it is incorrect I guess)

dependencies.add(cname, “mypkg:$basename:$ver”, { ext: ext } ) I tried several variations on the closure with no seeming effect. Guess I did not understand how to do the map in the closure here.

and then noticed something else in an example and tried this:

dependencies.add(cname, “:$basename:$ver@$ext” ) which finally worked !

The @extension did not see to be documented except in one example. The “group:name:version:classifier” maybe should be expanded on :slight_smile:

Lastly with my new found understanding (buahaha, the power !) I tried this which also worked for me:

dependencies.add(cname, “:$basename:$ver”, {

artifact {

name = basename

version = ver

type = ext

}

})

Anyway - I am solved now. Thanks for the .create suggestion.


(Peter Niederwieser) #4

I’ve added ‘ConfigurationContainer.create’ to the DSL docs. Thanks for pointing this out.

The ‘dependencies.add’ method supports exactly the same dependency notations as the ‘dependencies {}’ block. For example:

dependencies.add(cname, group: "mypkg", name: basename, version: ver, ext: ext)

(david.hill) #5

Peter: dependencies.add(cname, group: “mypkg”, name: basename, version: ver, ext: ext)

produced for me (with 1.4 and 1.6) > No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.add() is applicable for argument types: (java.util.LinkedHashMap, java.lang.String) values: [[group:mypkg, name:sdk, version:01, ext:tgz], …]

Possible solutions: add(java.lang.String, java.lang.Object), any(), add(java.lang.String, java.lang.Object, groovy.lang.Closure), wait(), find(), any(groovy.lang.Closure)

So while I thought it should work too… I could not get it to do so.


(Peter Niederwieser) #6

Sorry, my fault. Map notation doesn’t work here.


(david.hill) #7

Bummer - was hoping you were trying it on the latest source :slight_smile:

I am working so thanks for the hints.