How to add a dependency configurations listener?

Hi all, is there a way to add a configuration listener (when a dependency is added)? I want to create a task if a given dependency is added?

Niko

I’m not aware of such a listener. Depending on your exact needs, one option might be to iterate over dependencies in ‘project.afterEvaluate {}’.

I have try it and this works successfully but only in my terminal. In my IDE (RSA) the build fails with an exception:

You can't change configuration ..... because it is already resolved!

I don’t want to change the configuration, I want to know whether the configuration contains my dependency or not.

project.configurations."${configuration_name}".findAll {
 it.path.contains(chromeDriverDep.group + File.pathSeparator +
 chromeDriverDep.name + File.pathSeparator +
  chromeDriverDep.version) }.each { File driverZip ->
    createMyTask(project, driverZip, ...)
 }

‘findAll’ operates on the resolved artifact files. Instead you probably want to operate on dependencies (objects of type ‘Dependency’). Now that I think of it, it’s probably better to only check declared dependencies (rather than transitive dependencies), as otherwise you’d again have to resolve the configuration in the configuration phase, which may be undesirable. You can get declared dependencies with ‘project.configurations.foo.allDependencies’. Also, you should probably do this in a ‘project.afterEvaluate {}’ block, to make sure that all dependencies have been declared before you search them.

There is also the bigger question of whether it’s a good idea to only add a task if a certain dependency is present. I don’t know enough about your use case to comment on that, but generally it doesn’t hurt to add a task unconditionally.

Thank you for your recent suggestion. This solves my issue!!!