Resolve a dependency dynamically inside a gradle task

gradle

Solution

A task can't configure the build model (that's what plugins do). It's fine to create and resolve a detached configuration in a task. If this doesn't work, there is likely a problem with the task's code, or the dependency it tries to resolve. Note that dependencies can only be resolved if the correct repository(s) are defined.

Instead of `new DetaultExternalModuleDependency()` (which is an internal class), `project.dependencies.create()` should be used. Instead of `new Copy().execute()` (`Task#execute` must not be called from user code), `project.copy` should be used.

Problem

I am trying to build a gradle plugin, which does the following: - As part of one its tasks, it creates a new configuration - It adds a DefaultExternalModuleDependency to this configuration - more specifically, it constructs a dependency to the application server zip file (available on Nexus). This information can be overridden by the invoking project as well. - Tries to resolve this newly added dependency and then unpacks the file to a local folder All of this was working well when I had the details hard coded in a build file, but it looks like adding dependencies as part of a task are not treated the same way as having that information available at the parsing time. So my question is, how do I get the project to reload the configurations / dependencies? The code looks like the following: ``` @TaskAction void installAppserver() { Dependency dependency = new DefaultExternalModuleDependency(group,name,version) Configuration configuration = project.configurations.detachedConfiguration(dependency) configuration.setTransitive(false) configuration.files.each { file -> if (file.isFile() && file.name.endsWith('.zip')) { println 'Attempting to unzip: ' + file + ' into folder: ' + appServerFolder new Copy().from(project.zipTree(file)).into(appServerFolder).execute() } } } ``` The problem is that the actual artifacts are not getting resolved!

Original source