Add a dependency from a plugin based on a convention property

I am writing a custom plugin that adds a number of depedencies to the build.

I have defined an extension with a Property<String> field containing the version of one of these dependencies.

How do I add a dependency that includes a reference to this version?

I tried this:

project.dependencies.add('renjinPackager', extension.renjinVersion.map { "org.renjin:renjin-packager:${it}" })

But the argument of type Property<String> is not accepted.

Maybe this will work?

project.configurations.named("renjinPackager") {
    it.dependencies.add(project.dependencies.create("org.renjin:renjin-packager:${extension.renjinVersion.get()}"))
}

But you have to make sure you don’t trigger the creation of the configuration anywhere else, before the user gets a chance to set the property.

Looks like the Closure argument to named() method is invoked immediately, so that fails as well.

I’ve instead used project.afterEvaluate { } and this seems to work.

Thanks!

I tend to avoid Project.afterEvaluate {...} wherever possible. Most domain objects in the Gradle model have events that you can hook into

Eg:

configurations.renjinPackager.incoming.beforeResolve { 
  dependencies.add('renjinPackager', "org.renjin:renjin-packager:${renjinVersion}") 
}