Question related to configuring arbitrary objects using an external script

As section “14.5. Configuring arbitrary objects using an external script” describes you can assign values to objects from scripts.

I’m trying it this way:

def arr = []
apply from: "dependencies.lock", to: arr
println "arr = ${arr}"

where dependencies.lock looks like:

[
 "commons-lang:commons-lang:2.5",
  "org.apache.commons:commons-lang3:3.0",
 "org.springframework:spring-core:3.2.0.RELEASE",
 "org.springframework:spring-context:3.2.0.RELEASE",
 "org.springframework:spring-webmvc:3.2.0.RELEASE"
]

But this won’t work. The myArr is empty. Note that the example in section 14.5 is slightly different, but I wonder if something like above is possible?

What I want to end up is to do something like:

def myArr = []
apply from: "dependencies.lock", to: arr
configurations.all {
 resolutionStrategy.force myArr
}

In this case I could have a clean and simple dependencies.lock file just containing an array.

I was also trying different techniques like:

ResolutionStrategy rs = configurations.compile.resolutionStrategy
apply from: "dependencies.lock", to: rs

this works if dependencies.lock has:

forcedModules = [
 "commons-lang:commons-lang:2.5",
  "org.apache.commons:commons-lang3:3.0",
 "org.springframework:spring-core:3.2.0.RELEASE",
 "org.springframework:spring-context:3.2.0.RELEASE",
 "org.springframework:spring-webmvc:3.2.0.RELEASE"
]

But this way I cannot use configurations.all as it has no ResolutionStrategy. Maybe I should just loop over all configurations instead.

I’m really spamming this forum with questions at the moment :slight_smile: But we are making good progress on or prototype and feeling more and more confident we will be able to use Gradle in the end!

I’m pretty sure what you could do is:

// build.gradle
configurations.all {
    apply (from: 'dependencies.lock', to: resolutionStrategy)
}
  // dependencies.lock
force ([
// ...
])

Unfortunately, I don’t have time to try this example in particular. I hope this is helps!

What’s your goal here? Why not just put the whole ‘configurations.all { resolutionStrategy.force(…) }’ block into its own script (say ‘dependencies.gradle’)?

@Peter I’m having that at the moment yes. My goal is to have that dependencies.lock to be as easy as possible and if possible Gradle independent.

If you want to be truly Gradle independent, you’ll have to devise your own dependency format and parsing. If you merely want to strip down the syntax as much as possible, then go with Alexander’s suggestion. I don’t think you can get around having a method call like ‘force’ in there. With your ‘to: arr’ approach, you’d have to put ‘add’ in front of every dependency, in order to call ‘List.add’.

OK thanks Peter, if’s fair enough for now (prototyping stage), and I will so which form we will use in the end.