How can I specify a dependency classifier dynamically?


(Blaine Simpson) #1

I know that I can do this:

dependencies.b (group: 'org.hsqldb', name: 'hsqldb', version: 'fake-2', classifier: 'jdk5')

But I am reading my dependencies from a file (an “ivy.xml” file). If I could dynamically get “dependencies.b”, like with “dependencies[‘b’]”, I could presumably loop through my dependencies, but “b” must apparently be a hard-coded bare word and “dependencies[‘b’]” doesn’t work. I can handle all of the other attributes without trying to reverse engineer like this, because the Gradle API and Ivy API directly support all attributes other than classifier. I see no way in the API spec to pass either the classifier value or a hash like shown above to a dependency. I guess that in the code above, some method or constructor is being called with the given map, but I have no idea what method is being called for what class or instance.


(Adam Murdoch) #2

‘dependencies.b (…)’ adds org.gradle.api.artifacts.Dependency instances to the ‘configurations.b.dependencies’ collection. It also returns the Dependency instance. You can use either of these to get at the dependencies programmatically to do what you need.

A classifier artifact is modelled as a org.gradle.api.artifacts.DependencyArtifact, added to the ‘artifacts’ collection of the Dependency instance (or more correctly, the org.gradle.api.artifacts.ExternalModuleDependency instance).


(Blaine Simpson) #3

That all makes a lot of sense. As you suggest, I want to externalModuleDependency.addArtifact(x) my classifier DependencyArtifact. But I still have no idea how to instantiate a “classifier artifact… modelled as a DependencyArtifact”.

DependencyArtifact is an interface not a class, and the interface is immutable, so what is the factory instantiator or implementation class that I can use to instantiate a classifier DependencyArtifact?


(detelinyordanov) #4

Simply use ModuleDependency.artifact { }, the closure gets a DependencyArtifact instance, which you just need to configure.


(Blaine Simpson) #5

RESOLVED Sincere thanks to Adam and Detelin.

For the benefit of posterity, here’s what I ended up doing. I instantiate my Dependency instances like this:

dependencies { dep = add(mappableConfName, depAttrs) }

where mappableConfName is a String and depAttrs is a String/String map which may have keys ‘group’, ‘name’, ‘version’, ‘classifier’. I use the dep reference afterwards to make other adjustments.