I’m glossing over the details of the classpath here, but the point is that I can inline domain specific Groovy code directly in my build file, with it’s own classpath. How do I do the same in Gradle? It’s trivial to create a configuration, and get it resolved. But how would I run a specific closure in a class loader based on this configuration? I could write a plugin or build up an AntBuilder, but it seems like this is something that could be done natively on a Configuration. I’d like to be able to something like this in Gradle:
I could also pollute the buildscript scope with these dependencies, but then it removes my ability to isolate and customize the classpath for a specific task. Another use case is for testing with different versions, which isn’t possible when using buildscript:
configuration {
V1, V2
}
dependencies {
V1 'netflix:client:1+' // Version 1 of client api
V2 'netflix:client:2+' // Version 2 of client api
}
def clientCall = new Closure() {
import app.client.*;
def client = new Client();
client.call();
}
task(legacyClient) << {
V1.execute clientCall
}
task(modernClient) << {
V2.execute clientCall
}
I can see if this was in place, most of my reasons for using buildSrc would go away. Since I could compile my code into one configuration, then use that code right away.
Doing this as code would be pretty tricky. We’d have to use some pretty complicated compile transforms and detecting what to transform would be tricky. However, executing the code as a string of script is reasonably straightforward. Here’s a quick (working) sketch of how a plugin for this might look: